Install Chrome Headless Shell with Puppeteer Version Pinning
In this article
Installing the Chrome Headless Shell stable build with version pinning in Puppeteer ensures that your automation scripts run against a consistent browser version, eliminating the "browser version mismatch" errors common in Continuous Integration (CI) environments. By decoupling the browser binary from the Puppeteer library version and pinning it to a specific stable release, you create a deterministic environment where updates to the Puppeteer package do not unexpectedly break your headless browser execution.
What You'll Need
- Node.js (LTS version recommended) installed on your local machine and CI runner.
- A package manager such as npm or yarn.
- Access to a CI provider (GitHub Actions, GitLab CI, Jenkins, etc.).
- Basic familiarity with JavaScript/TypeScript and the Puppeteer API.
Step-by-Step
- Install Puppeteer Core
Instead of installing the standardpuppeteerpackage, which automatically downloads a compatible version of Chrome for Puppeteer, installpuppeteer-core. This version does not come with a bundled browser, giving you full control over which binary is used.npm install puppeteer-core - Identify the Stable Chrome Headless Shell Version
Visit the Chrome for Testing (CfT) availability dashboard or the JSON endpoints provided by Google to find the current stable version number (e.g., 125.0.6422.141). Note this version number precisely, as you will need it for pinning. - Configure the Browser Download in CI
In your CI configuration file (e.g.,.github/workflows/main.yml), add a step to download the specific version of the Chrome Headless Shell. Using the@puppeteer/browsersutility is the most reliable method.
To pin to a specific version instead of "stable", replacenpx @puppeteer/browsers install chrome-headless-shell@stable@stablewith the version number:chrome-headless-shell@125.0.6422.141. - Capture the Binary Path
The download utility will output the path where the binary was installed. In a CI environment, you should save this path to an environment variable (e.g.,CHROME_PATH) so that your Node.js script can reference it dynamically. - Initialize Puppeteer with the Pinned Path
Modify your Puppeteer launch configuration to use theexecutablePathoption. This tellspuppeteer-coreexactly which binary to execute, bypassing the default search for a bundled browser.const puppeteer = require('puppeteer-core'); (async () => { const browser = await puppeteer.launch({ executablePath: process.env.CHROME_PATH || '/usr/bin/google-chrome-headless-shell', args: ['--headless=new', '--no-sandbox', '--disable-setuid-sandbox'] }); // Your automation logic here await browser.close(); })(); - Pin Versions in package.json
To ensure the Puppeteer library itself doesn't shift, remove the caret (^) or tilde (~) from the version number in yourpackage.json. Change"puppeteer-core": "^22.0.0"to"puppeteer-core": "22.0.0". - Verify CI Execution
Run your CI pipeline. Ensure the logs show the correct version of the Headless Shell being downloaded and that theexecutablePathis correctly resolved.
Common Mistakes
- Using puppeteer instead of puppeteer-core: Installing the full
puppeteerpackage often leads to duplicate browser downloads—one from the package install and one from your manual pinning step—which wastes CI disk space and time. - Forgetting the --no-sandbox flag: Most CI environments (like GitHub Actions or Docker) run as root or in restricted containers. Without
--no-sandbox, the browser will fail to launch. - Using an outdated Headless mode: Using
headless: trueis the legacy mode. For the newer Chrome Headless Shell, use--headless=newin theargsarray to ensure you are using the optimized shell. - Hardcoding local paths: Hardcoding a path like
C:\Users\Name\...will cause the CI pipeline to fail. Always use environment variables for theexecutablePath.
Tips
- Cache the Browser Binary: Downloading the browser on every CI run slows down your pipeline. Use your CI provider's caching mechanism (e.g.,
actions/cache) to store the Chrome Headless Shell binary based on the version number. - Use a .env file for Local Dev: Create a
.envfile locally withCHROME_PATH=/path/to/your/local/shellto mirror the CI environment without changing the code. - Monitor Chrome for Testing Releases: Subscribe to the Chrome for Testing release notes to know when to bump your pinned version to maintain security and performance.
Troubleshooting
If the browser fails to launch in CI, first check the executablePath. Print the value of the environment variable to the console immediately before puppeteer.launch() to ensure it is not undefined or pointing to a non-existent directory.
If you encounter "Shared library missing" errors (e.g., libnss3.so or libatk-1.0.so), it means the CI runner lacks the necessary Linux dependencies for Chrome. You must install these using the system package manager. For Ubuntu/Debian runners, run: sudo apt-get update && sudo apt-get install -y libnss3 libatk1.0-0 libatk-bridge2.0-0 libcups2 libdrm2 libxkbcommon0 libxcomposite1 libxdamage1 libxrandr2 libgbm1.
If you see a "Protocol error" or "Target closed" message, ensure that the version of puppeteer-core is compatible with the version of the Headless Shell you pinned. While pinning prevents unexpected updates, a massive version gap (e.g., Puppeteer v10 with Chrome v125) will cause communication failures.
Frequently Asked Questions
What is the difference between Chrome Headless and Chrome Headless Shell?
Chrome Headless Shell is a stripped-down version of the browser specifically designed for automation. It has a smaller footprint and faster startup times than the full Chrome browser running in headless mode.
Why should I pin the browser version?
Pinning prevents "breaking changes" from being introduced into your pipeline. If Google releases a new stable version that changes how a certain element is rendered or how the CDP (Chrome DevTools Protocol) behaves, your tests won't fail until you manually update and verify the new version.
Can I use this setup with Docker?
Yes. In your Dockerfile, you can use the @puppeteer/browsers utility to install the specific version during the build phase and set the ENV CHROME_PATH to the installation directory.
Does puppeteer-core support all Puppeteer features?
Yes, puppeteer-core provides the exact same API as the standard puppeteer package; the only difference is that it does not manage the browser installation for you.
How often should I update my pinned version?
It is recommended to update your pinned version once a month or whenever you encounter a bug that is fixed in a newer stable release of Chrome.