Install Headless Chrome for Browser Automation
In this article
Installing Headless Chrome allows you to run the Google Chrome browser without a graphical user interface (GUI), enabling you to automate web tasks, scrape data, and run tests on a server or in a CI/CD pipeline. By removing the visual overhead, the browser consumes fewer resources and executes faster, making it the industry standard for programmatic web interaction via tools like Puppeteer, Selenium, or Playwright.
What You'll Need
- A computer or server running Windows, macOS, or Linux (Ubuntu/Debian is recommended for servers).
- Administrative or sudo privileges to install software packages.
- Node.js installed (if using Puppeteer or Playwright) or Python installed (if using Selenium).
- A stable internet connection to download the browser binaries and drivers.
- Basic familiarity with the command line or terminal.
Step-by-Step
- Choose Your Automation Framework: Before installing the browser, decide which library you will use to control it. Puppeteer is the most common for Node.js as it comes bundled with a compatible version of Chromium. Selenium is the standard for Python and Java. Playwright is a modern alternative that supports multiple browser engines.
- Install Google Chrome or Chromium:
- Windows/macOS: Download and install the standard Google Chrome browser from the official website. Headless mode is a flag triggered within the browser, so the standard installation is sufficient.
- Linux (Ubuntu/Debian): Use the terminal to install Chromium, which is the open-source base for Chrome. Run
sudo apt updatefollowed bysudo apt install chromium-browser.
- Install the Corresponding WebDriver: If you are using Selenium, you must install ChromeDriver. The version of ChromeDriver must match the version of the Chrome browser installed on your system.
- Check your Chrome version in
Settings > About Chrome. - Download the matching driver from the Chrome for Testing availability dashboard.
- Add the driver executable to your system's PATH so your code can find it regardless of the directory.
- Check your Chrome version in
- Install System Dependencies (Linux Only): Headless Chrome requires several shared libraries to run on Linux servers that lack a desktop environment. Install the necessary dependencies using:
sudo apt-get install -y libnss3 libatk1.0-0 libatk-bridge2.0-0 libcups2 libdrm2 libxkbcommon0 libxcomposite1 libxdamage1 libxrandr2 libgbm1 libasound2. - Configure the Headless Flag in Your Code: To actually run the browser in headless mode, you must pass a specific argument in your script.
- Puppeteer: Use
const browser = await puppeteer.launch({ headless: "new" });. - Selenium (Python): Use
options = webdriver.ChromeOptions()followed byoptions.add_argument("--headless=new").
- Puppeteer: Use
- Run a Test Script: Create a simple script to navigate to a website and take a screenshot or print the page title. This confirms that the browser is launching correctly without a window appearing.
Common Mistakes
- Version Mismatch: The most frequent error is using a ChromeDriver version that does not match the installed Chrome browser version. This results in a "session not created" error.
- Missing Linux Libraries: Attempting to run Headless Chrome on a bare Linux server without installing the required
libnss3orlibgbm1libraries will cause the browser to crash immediately on launch. - Forgetting the Sandbox Flag: In some Linux environments (especially when running as root in Docker), Chrome will fail to start unless the
--no-sandboxargument is added to the launch options. - Using Outdated Headless Flags: Older versions of Chrome used
--headless, but newer versions recommend--headless=newto ensure the browser behaves exactly like the headed version.
Tips
- Use a Custom User Agent: Some websites detect headless browsers by checking the User-Agent string. Set a real browser User-Agent in your options to avoid being blocked.
- Set a Window Size: Even in headless mode, Chrome defaults to a small window size. Use
--window-size=1920,1080to ensure elements are rendered as they would be on a standard desktop. - Manage Memory: Headless Chrome can leak memory over time if you don't close browser instances. Always use a
try...finallyblock or anawait browser.close()command to terminate the process. - Use DevTools Protocol: For advanced automation, utilize the Chrome DevTools Protocol (CDP) to intercept network requests or simulate geolocation.
Troubleshooting
If the browser fails to launch, start by checking the console error logs. If you see an error regarding "shared libraries," refer back to the Linux dependencies step. If the error mentions "DevToolsActivePort file doesn't exist," try adding the --remote-debugging-port=9222 and --disable-dev-shm-usage flags to your launch configuration. The latter is particularly important for Docker containers where the shared memory space (/dev/shm) is too small for Chrome.
If the browser launches but the page content is missing or different from what you see in a normal browser, disable the headless mode temporarily (set headless: false) to visually inspect what is happening. This often reveals that a pop-up, cookie banner, or CAPTCHA is blocking the automation.
Frequently Asked Questions
Is Headless Chrome the same as Chromium?
Chromium is the open-source project that serves as the foundation for Google Chrome. Headless Chrome is simply a mode of running either Chrome or Chromium without a visible window.
Can I run Headless Chrome in a Docker container?
Yes, but you must install the necessary Linux dependencies and use the --no-sandbox flag because Docker containers typically run as root, which Chrome's security sandbox dislikes.
Does Headless Chrome use less RAM than regular Chrome?
Yes, because it does not have to render the graphical user interface or manage window painting, it consumes significantly fewer system resources.
How do I handle CAPTCHAs in headless mode?
CAPTCHAs are designed to stop headless browsers. To bypass them, you may need to use stealth plugins (like puppeteer-extra-plugin-stealth) or integrate a third-party CAPTCHA solving service.
Can I take screenshots in headless mode?
Yes, taking screenshots is one of the primary uses of Headless Chrome. Most frameworks provide a page.screenshot() method that captures the rendered page as an image file.