Fix Chrome Failed to Start Crashed in Headless Mode
In this article
To fix the "Chrome failed to start" or "crashed in headless mode" error, you must typically add the --no-sandbox and --disable-dev-shm-usage flags to your browser launch arguments. This error occurs most frequently in Docker containers, Linux servers, or CI/CD pipelines where Chrome cannot access a GPU, a display server (X11), or sufficient shared memory. By disabling the sandbox and forcing Chrome to use the disk instead of /dev/shm for shared memory, you bypass the environment restrictions that cause the browser process to terminate immediately upon startup.
What You'll Need
- A system with Google Chrome or Chromium installed.
- Administrative or sudo access to the operating system (for installing dependencies).
- Access to the source code or configuration file of the automation script (Puppeteer, Selenium, Playwright, or Pyppeteer) triggering the crash.
- A terminal or command-line interface.
Step-by-Step
- Identify the Environment Constraints: Before changing code, determine where the crash is happening. If you are on a local Windows or macOS machine, this error is rare. If you are on Linux (Ubuntu, Debian, CentOS) or inside a Docker container, the crash is usually due to the lack of a window manager or restricted kernel permissions. Check your logs for phrases like "Failed to move to new namespace" or "No usable sandbox found".
-
Update the Launch Arguments: Locate the section of your code where the browser is initialized. You need to pass a specific array of arguments to the browser instance.
If using Puppeteer (Node.js), modify yourpuppeteer.launchcall as follows:const browser = await puppeteer.launch({ headless: "new", args: ['--no-sandbox', '--disable-setuid-sandbox', '--disable-dev-shm-usage'] });
If using Selenium (Python), configure the ChromeOptions object:from selenium import webdriver from selenium.webdriver.chrome.options import Options options = Options() options.add_argument('--headless') options.add_argument('--no-sandbox') options.add_argument('--disable-dev-shm-usage') driver = webdriver.Chrome(options=options) -
Resolve Shared Memory Issues: The
--disable-dev-shm-usageflag is critical for Docker users. By default, Docker containers allocate only 64MB to/dev/shm. Chrome uses this space for its render process. When it runs out, the browser crashes. This flag forces Chrome to use/tmpinstead, which is typically larger and mapped to the main disk. -
Install Missing System Dependencies: Even in headless mode, Chrome requires several system-level libraries to render pages and handle fonts. If the browser crashes without a clear error message, you may be missing libraries like
libnss3,libatk1.0-0, orlibgbm1. On Ubuntu/Debian, run the following command to ensure all necessary libraries are present:sudo apt-get update && sudo apt-get install -y libgbm-dev libnss3 libatk-bridge2.0-0 libgtk-3-0 libasound2 -
Verify Chrome Binary Path: Sometimes the "failed to start" error is simply because the automation tool cannot find the Chrome executable. Explicitly define the path to the binary in your launch options. For example, in Puppeteer, use the
executablePathproperty:executablePath: '/usr/bin/google-chrome-stable' -
Test with a Simple Page Load: Once the flags are added and dependencies installed, run a minimal script that navigates to a lightweight site (like
example.com) and takes a screenshot. This confirms that the browser is not only starting but is successfully rendering content in the background.
Common Mistakes
One of the most frequent mistakes is forgetting that --no-sandbox reduces the security of the browser. While necessary in many containerized environments, you should never use this flag when browsing untrusted websites manually. The sandbox is a critical security layer that prevents malicious code from escaping the browser and affecting the host system. Only use it for automated testing of trusted sites or within isolated containers.
Another common error is mixing headless modes. In newer versions of Chrome, there is a distinction between --headless (the old mode) and --headless=new (the new headless mode that behaves exactly like the headed browser). If your site relies on specific rendering features or extensions, using the old headless mode may cause the browser to crash or render pages incorrectly, leading you to believe there is a startup error when it is actually a rendering failure.
Users also frequently neglect to update their WebDriver version. If you are using Selenium, the version of chromedriver must match the version of the installed Chrome browser exactly. If Chrome updates to version 125 but you are using chromedriver 123, the process will crash immediately upon startup, often throwing a generic "failed to start" error that mimics a headless configuration issue.
Tips
To optimize performance in headless mode, consider adding the --disable-gpu flag. While modern Chrome versions handle this better, many server environments still lack a physical GPU. Disabling GPU acceleration prevents Chrome from wasting resources searching for hardware acceleration that doesn't exist, which can speed up the initial startup time.
For those running high-volume scraping or testing tasks, use the --single-process flag with caution. While it can reduce memory overhead in very constrained environments, it is known to be unstable and can lead to more frequent crashes over long sessions. A better approach is to manage your browser context by closing pages and restarting the browser instance every 50-100 requests to clear memory leaks.
If you are using Docker, instead of relying solely on --disable-dev-shm-usage, you can increase the shared memory size of the container at runtime by adding the --shm-size=2gb flag to your docker run command. This allows Chrome to use the faster shared memory system rather than falling back to the slower disk-based /tmp directory.
Troubleshooting
If the browser still crashes after applying the flags, the first step is to enable verbose logging. In your launch arguments, add --verbose or check the system logs using dmesg or journalctl -xe. This will often reveal the exact shared library (.so file) that is missing, allowing you to install the specific package required.
Check for zombie processes. If a previous headless session crashed, it may have left behind "orphan" Chrome processes that are locking the user data directory. Run ps aux | grep chrome in your terminal and kill any remaining processes using kill -9 [PID]. If the user data directory is locked, Chrome will fail to start a new instance.
Verify the user permissions. Chrome cannot be run as the root user unless the --no-sandbox flag is present. If you are running your script as root in a Linux environment and you haven't added that flag, the browser will crash instantly. The best practice is to create a dedicated chrome user in your Dockerfile and switch to that user using the USER instruction before executing the script.
Frequently Asked Questions
Why does Chrome work in headed mode but crash in headless mode?
Headless mode relies on different rendering paths and often requires specific flags to handle the absence of a display server (X11 or Wayland). Issues like shared memory limits and sandbox restrictions are more prominent in headless environments, especially on servers.
Is the --no-sandbox flag safe to use?
It is safe if you are automating a site you trust or running the browser inside a disposable container. However, it is unsafe for general web browsing because it removes the primary layer of isolation between the web page and your operating system.
What is the difference between --headless and --headless=new?
The original --headless mode was a separate browser implementation. --headless=new is the full Chrome browser running without a visible window, meaning it supports extensions and renders pages exactly as a user would see them.
How do I find which Chrome version I have installed?
You can run google-chrome --version or chromium-browser --version in your terminal. This is essential for ensuring your WebDriver version matches your browser version.
Can I run headless Chrome on a Windows Server?
Yes, but you must ensure that the server has the necessary Visual C++ Redistributable packages installed, as these are often missing from minimal Server editions and are required for Chrome to launch.