To launch Chromium (or Google Chrome) in headless mode via the command line, you can use the --headless
argument. This mode allows you to run the browser without a graphical user interface, which is especially useful for automated tasks, testing, and scenarios where no display is necessary, such as on a server.
Here are the basic command-line arguments to launch headless Chromium:
chromium --headless --disable-gpu --no-sandbox --remote-debugging-port=9222 http://example.com
Let's break down these arguments:
--headless
: Runs the browser in headless mode.--disable-gpu
: This flag is used to disable GPU hardware acceleration. It was necessary to overcome some early bugs in headless mode, although it may not be required anymore, it's often kept for compatibility reasons.--no-sandbox
: Disables the sandbox for all process types that are normally sandboxed. Note that using this flag is not recommended as it can make the browser less secure.--remote-debugging-port=9222
: Starts the Chrome Remote Debugging Protocol on the specified port, allowing you to control the headless browser programmatically.http://example.com
: The URL to navigate to when the browser starts. You can omit this if you're going to control the browser via the debugging protocol.
Remember to replace chromium
with chrome
or google-chrome
depending on how the Google Chrome browser is named in your system. The availability of the Chromium or Google Chrome binary and its name may vary depending on the operating system and how it was installed.
On Windows, you might need to specify the full path to the Chromium or Chrome executable if it's not in your system's PATH environment variable, like so:
"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --headless --disable-gpu --no-sandbox --remote-debugging-port=9222 http://example.com
For macOS, the command might look like this:
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --headless --disable-gpu --no-sandbox --remote-debugging-port=9222 http://example.com
On Linux, it would typically be:
google-chrome --headless --disable-gpu --no-sandbox --remote-debugging-port=9222 http://example.com
or
chromium-browser --headless --disable-gpu --no-sandbox --remote-debugging-port=9222 http://example.com
Remember that running with --no-sandbox
is insecure and should be avoided if possible. It's better to configure your environment correctly to run Chromium with a sandbox.
If you're planning to use headless Chromium for web scraping or automated testing, you might want to use a higher-level library or framework like Puppeteer for Node.js or Selenium with a WebDriver for Chromium to control the browser programmatically. These tools handle the command line arguments for you and provide a more user-friendly API to interact with the headless browser.