Headless Chrome on Jenkins Linux: Install and Configure
In this article
Installing Headless Chrome on a Jenkins Linux server allows you to run automated UI tests, web scraping, and PDF generation within your CI/CD pipeline without needing a physical monitor or a graphical user interface (GUI). By configuring Chrome to run in "headless" mode, the browser operates in the background, consuming fewer resources while providing the same rendering capabilities as a standard browser window.
What You'll Need
- A Linux server (Ubuntu/Debian or CentOS/RHEL) with Jenkins installed.
- Sudo or root privileges to install system packages.
- Internet connectivity to download the Chrome binaries and dependencies.
- A WebDriver (such as ChromeDriver) that matches the version of Chrome you install.
Step-by-Step
-
Update System Packages:
Begin by ensuring your package manager is up to date to avoid dependency conflicts.
For Ubuntu/Debian:sudo apt-get update
For CentOS/RHEL:sudo yum update -
Install Google Chrome:
Download the latest stable Google Chrome package. Using the command line is the most efficient method for a headless server.
For Ubuntu/Debian:wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb sudo apt install ./google-chrome-stable_current_amd64.deb
For CentOS/RHEL:wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm sudo yum localinstall google-chrome-stable_current_x86_64.rpm
-
Verify Chrome Installation:
Confirm that Chrome is installed and check the version. This version number is critical for selecting the correct ChromeDriver in the next step.
google-chrome --version -
Install ChromeDriver:
ChromeDriver acts as the bridge between your test scripts (Selenium, Puppeteer, etc.) and the Chrome browser. You must download the version that matches your installed Chrome version.
Visit the Chrome for Testing availability dashboard to find the correct URL for your version. Once identified, download and unzip it:wget [URL_TO_CHROMEDRIVER] unzip chromedriver_linux64.zip sudo mv chromedriver /usr/local/bin/chromedriver sudo chmod +x /usr/local/bin/chromedriver
-
Configure Jenkins Environment Variables:
To ensure Jenkins can find the browser and driver, add their paths to the Jenkins global configuration.
- Go to Manage Jenkins > System (or Configure System).
- Locate the Global properties section and check Environment variables.
- Add
CHROME_BINwith the value/usr/bin/google-chrome. - Add
CHROMEDRIVER_BINwith the value/usr/local/bin/chromedriver.
-
Configure Browser Arguments in Your Code:
Chrome will not launch on a headless server unless specific flags are passed. In your test scripts (Java, Python, Node.js), you must include the following arguments:
--headless: Runs the browser without a GUI.--no-sandbox: Disables the sandbox security layer, which is often necessary when running as thejenkinsuser.--disable-dev-shm-usage: Forces Chrome to use/tmpinstead of/dev/shm, preventing crashes in Docker containers or low-memory environments.
- Run a Test Build: Create a simple Jenkins pipeline job to verify the setup. Use a shell script step to run a basic command or execute your test suite to ensure the browser initializes correctly without throwing a "SessionNotCreatedException" or "DevToolsActivePort" error.
Common Mistakes
- Version Mismatch: The most frequent error is using a ChromeDriver version that does not match the installed Google Chrome version. Even a minor version difference can cause the driver to fail.
- Forgetting --no-sandbox: By default, Chrome uses a sandbox for security. However, the
jenkinsuser often lacks the necessary permissions to initialize this sandbox on Linux, leading to immediate browser crashes. - Insufficient Memory: Headless Chrome is resource-intensive. If the server runs out of RAM, the Linux OOM (Out of Memory) killer may terminate the process, resulting in "unexpectedly closed" errors in Jenkins logs.
- Missing Dependencies: Sometimes the
.debor.rpminstallation misses low-level libraries (likelibnss3orlibgconf-2-4). Always check the logs for "shared library not found" errors.
Tips
- Use Chrome for Testing: Google now provides a specific "Chrome for Testing" binary that is designed for automation and does not auto-update, making it easier to keep the browser and driver in sync.
- Implement Screenshots on Failure: Since you cannot see the browser, configure your framework to take a screenshot and save it as a Jenkins build artifact whenever a test fails.
- Use a Dedicated User: Avoid running Chrome as the
rootuser. Using the standardjenkinsuser is safer and more representative of a production environment. - Clean Up Processes: Headless Chrome can sometimes leave "zombie" processes behind if a build is aborted. Add a post-build step to run
pkill chrometo ensure the server remains clean.
Troubleshooting
If Chrome fails to start, check the following:
- Error: "DevToolsActivePort file doesn't exist": This is almost always caused by missing the
--headless,--no-sandbox, or--disable-dev-shm-usageflags. Verify your code configuration. - Error: "SessionNotCreatedException": Check your versions. Run
google-chrome --versionandchromedriver --version. If they differ, download the matching driver. - Browser hangs or freezes: Increase the server's swap space or upgrade the RAM. Check
dmesgto see if the OOM killer is active. - Permission Denied: Ensure the
chromedriverbinary has execution permissions by runningchmod +x /usr/local/bin/chromedriver.
Frequently Asked Questions
Do I need a GUI installed on my Linux server?
No. Headless Chrome is specifically designed to run without a X11 server or any graphical desktop environment.
Will Headless Chrome run slower than a regular browser?
Generally, it is faster because it does not have to render the actual pixels to a screen, though the execution speed of the JavaScript remains the same.
How do I handle Chrome auto-updates?
Standard Google Chrome updates automatically. To prevent this from breaking your ChromeDriver, you can either pin the version using your package manager or use the "Chrome for Testing" binaries which are version-static.
Can I run multiple Chrome instances in parallel on Jenkins?
Yes, provided your server has enough CPU and RAM. Use Jenkins parallel stages and ensure each instance uses a unique user-data directory to avoid profile locking.
Is --no-sandbox safe to use?
It reduces security by disabling the browser's isolation layer. While necessary for many CI environments, you should ensure the server is secured and the sites being tested are trusted.