Fix Chrome Startup Failures with Selenium Headless Chrome
In this article
Fixing Chrome startup failures in Selenium headless mode typically requires aligning the Chrome browser version with the ChromeDriver version and applying specific ChromeOptions to bypass environment restrictions, such as the --no-sandbox and --disable-dev-shm-usage flags. These failures often manifest as "SessionNotCreatedException" or "WebDriverException: unknown error: Chrome failed to start," usually because the browser cannot initialize in a restricted environment like a Docker container, a Linux server, or a CI/CD pipeline where a graphical user interface (GUI) is absent.
What You'll Need
- Python installed on your system (or Java/C# if using those bindings).
- The Selenium library installed via pip (
pip install selenium). - Google Chrome browser installed on the host machine.
- ChromeDriver that matches the exact version of your installed Chrome browser.
- Administrative or sudo privileges to install dependencies on Linux systems.
Step-by-Step
-
Verify Version Compatibility:
The most frequent cause of startup failure is a version mismatch. Open your Chrome browser, go to
chrome://settings/help, and note the version number (e.g., 120.0.6099.109). Visit the Chrome for Testing availability dashboard or the ChromeDriver downloads page and download the driver that matches this major version. If you are using Selenium 4.6.0 or newer, Selenium Manager handles this automatically, but manual intervention is still required if you are using a custom binary path or an older environment. -
Configure Basic Headless Options:
Import the
Optionsclass fromselenium.webdriver.chrome.options. Create an instance ofOptions()and add the--headlessargument. For newer versions of Chrome (v109+), it is recommended to use--headless=new, as the legacy headless mode behaves differently from the actual Chrome browser and is more likely to be detected by websites or fail to render complex JavaScript elements. -
Disable the Sandbox:
Add the
--no-sandboxargument to your options. The Chrome sandbox is a security feature that isolates the browser process from the rest of the operating system. However, in many Linux environments—especially Docker containers running as root—the sandbox cannot initialize, causing the browser to crash immediately upon startup. Disabling it allows the process to launch in these restricted environments. -
Manage Shared Memory:
Add the
--disable-dev-shm-usageargument. By default, Chrome uses/dev/shm(shared memory) for temporary data. In many containerized environments, this partition is too small (often limited to 64MB), leading to "out of memory" crashes during page loads. This flag forces Chrome to use the/tmpdirectory instead, which typically has more available space. -
Set a User-Agent String:
Headless Chrome often sends a User-Agent header that explicitly contains the word "HeadlessChrome." Many servers block this to prevent bot scraping. Use
options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36")to mimic a standard desktop browser and avoid being blocked during the startup handshake. -
Initialize the WebDriver:
Pass the configured options object into the
webdriver.Chrome()constructor. Ensure that the path to the ChromeDriver is correctly specified in theServiceobject if you are not relying on the system PATH or Selenium Manager.
Common Mistakes
One of the most common mistakes is using the outdated --headless flag instead of --headless=new. The original headless mode was a separate browser implementation that didn't support all CSS and JS features, leading to "element not found" errors that looked like startup failures but were actually rendering failures. Switching to the new headless mode ensures the browser engine is identical to the headed version.
Another frequent error is neglecting to install the necessary system dependencies on Linux. Many developers assume that installing the Chrome binary is enough, but Chrome requires several X11 and Mesa libraries to function, even in headless mode. If these are missing, the driver will report that Chrome failed to start without specifying which library is missing. You must install packages like libnss3, libgconf-2-4, and libatk1.0-0 via the package manager.
Finally, developers often forget to close the browser session. When a script crashes before calling driver.quit(), "zombie" Chrome processes remain in the background. Over time, these processes consume all available RAM and CPU, eventually causing new startup attempts to fail because the system cannot allocate more resources to a new browser instance.
Tips
To improve stability, implement a "Retry Logic" wrapper around your WebDriver initialization. Network glitches or temporary resource spikes can cause a startup failure. Wrapping the initialization in a try-except block with a loop that attempts to launch the browser three times with a short delay between attempts can significantly reduce flaky CI/CD pipelines.
For debugging startup failures, temporarily disable headless mode if you have access to a GUI, or use the --remote-debugging-port=9222 flag. This allows you to connect to the headless instance via another browser to see exactly what is happening on the screen. Additionally, using the --disable-gpu flag is often helpful on Windows machines where hardware acceleration may conflict with the headless driver.
If you are running Selenium in a high-concurrency environment, avoid using a single user profile. Use --user-data-dir=/tmp/chrome-profile-1 to give each instance its own isolated profile directory. This prevents file-locking conflicts where two Chrome instances try to access the same preferences file simultaneously, which frequently leads to startup timeouts.
Troubleshooting
If you encounter a SessionNotCreatedException, the first step is to check the version of Chrome and ChromeDriver. Run google-chrome --version in your terminal and compare it to the version of the chromedriver binary. If they differ in the major version (e.g., Chrome 121 vs Driver 120), you must update the driver. If you are using Selenium Manager and it still fails, try manually specifying the binary location using options.binary_location = "/path/to/chrome".
When faced with a WebDriverException: unknown error: Chrome failed to start on Linux, check the system logs or run the Chrome binary manually from the command line with the --headless flag. This will often print the specific missing .so library file to the terminal. For example, if it mentions libatk-1.0.so.0, you know exactly which system package to install using apt-get or yum.
If the browser starts but pages fail to load or time out, check your memory limits. In Docker, ensure you are running the container with --shm-size=2gb. While --disable-dev-shm-usage helps, giving the container more shared memory is a more performant solution for memory-intensive websites. If you are on a VPS with very low RAM (e.g., 1GB), create a swap file to prevent the Linux OOM (Out of Memory) killer from terminating the Chrome process during startup.
Frequently Asked Questions
Why does my headless Chrome work locally but fail in Docker?
Docker containers lack a display server and often have restricted shared memory. You must use the --no-sandbox and --disable-dev-shm-usage flags to bypass these environment-specific limitations.
What is the difference between --headless and --headless=new?
The --headless=new flag uses the full Chrome browser engine without a GUI, whereas the old --headless flag used a separate, stripped-down implementation that often behaved differently and was easier for websites to detect.
How do I stop zombie Chrome processes from filling up my RAM?
Always wrap your WebDriver code in a try...finally block and call driver.quit() in the finally section. On Linux, you can also run pkill chrome to clear orphaned processes.
Do I need to install a separate driver for headless mode?
No, the same ChromeDriver used for headed mode is used for headless mode. The "headless" part is a configuration option passed to the browser, not a different driver version.
Can I run multiple headless Chrome instances simultaneously?
Yes, but you should provide each instance with a unique --user-data-dir to avoid profile locking issues and ensure they don't interfere with each other's cookies and cache.
Why is my headless browser being blocked by Cloudflare or other bot detectors?
Headless Chrome sends specific signals (like the navigator.webdriver property) that identify it as a bot. Using a custom User-Agent and the --headless=new flag can help, but some advanced detectors require specialized libraries like undetected-chromedriver.