User Agents

Web browsers and everyday web tech, explained

Set Chrome Bin for Karma Headless Chrome CI

· 4 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Setting the Chrome binary path for Karma tests in a headless Chrome CI environment ensures your tests run consistently using the correct Chrome version, especially when default installations aren’t detected. This is essential in CI/CD pipelines where Chrome may not be installed in standard locations or when using custom or managed Chrome builds. By explicitly defining the Chrome binary path in your Karma configuration, you avoid execution failures due to missing or mismatched browsers. This guide walks you through configuring Karma to use a specific Chrome executable in headless mode on continuous integration systems like GitHub Actions, GitLab CI, or Jenkins.

What You'll Need

  • A working Karma configuration file (karma.conf.js)
  • Chrome or Chromium installed in your CI environment (or via a package like puppeteer)
  • Access to modify CI environment variables or install packages (e.g., via .gitlab-ci.yml, github/workflows/test.yml, etc.)
  • Node.js and npm or yarn installed in the CI environment
  • Basic familiarity with command-line tools and JavaScript configuration

Step-by-Step

  1. Install Chrome or Chromium in your CI environment. Many CI platforms don’t include Chrome by default. Use a package manager to install it. For example, in a GitHub Actions workflow, add:
          - name: Install Chrome
            run: sudo apt-get update && sudo apt-get install -y google-chrome-stable
    
    Or, if using Puppeteer (which bundles Chromium), install it via:
            npm install puppeteer --save-dev
        
  2. Determine the Chrome binary path. Find where Chrome is installed. Common paths include:
    • Linux: /usr/bin/google-chrome or /usr/bin/chromium-browser
    • macOS: /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    • CI with Puppeteer: node_modules/puppeteer/.local-chromium/ (path varies by OS)
    If using Puppeteer, you can log the path in a script:
            const puppeteer = require('puppeteer');
            console.log(puppeteer.executablePath());
        
  3. Modify your Karma configuration file (karma.conf.js). Open karma.conf.js and locate the browsers and customLaunchers sections. If they don’t exist, add them. Define a custom launcher for Headless Chrome with the explicit binary path:
            customLaunchers: {
              ChromeHeadlessCI: {
                base: 'ChromeHeadless',
                binary: '/usr/bin/google-chrome', // Update with actual path
                flags: [
                  '--no-sandbox',
                  '--disable-setuid-sandbox',
                  '--disable-dev-shm-usage',
                  '--disable-accelerated-2d-canvas',
                  '--no-first-run',
                  '--no-zygote',
                  '--single-process', // Optional: helps in some CI environments
                ]
              }
            },
            browsers: ['ChromeHeadlessCI']
        
  4. Use environment variables for flexibility. Instead of hardcoding the path, use process.env to make it configurable:
            binary: process.env.CHROME_BIN || '/usr/bin/google-chrome'
        
    Then, in your CI script, set the environment variable before running Karma:
            CHROME_BIN=/usr/bin/google-chrome npm run test
        
  5. Update CI pipeline script. In your CI configuration (e.g., .github/workflows/test.yml), ensure the test script sets the environment and runs Karma. Example:
            - name: Run Karma Tests
              env:
                CHROME_BIN: /usr/bin/google-chrome
              run: npm run test:ci
        
    Where test:ci is an npm script like karma start karma.conf.js --single-run.
  6. Verify Chrome launches correctly. Run the Karma command in your CI job and check logs. Look for messages like ChromeHeadlessCI has connected and ensure no Chrome not found errors appear.
  7. Handle platform-specific paths (optional). If your project runs on multiple CI platforms, conditionally set the binary path:
            binary: process.platform === 'linux' 
              ? process.env.CHROME_BIN || '/usr/bin/chromium-browser'
              : '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'
        

Common Mistakes

  • Using ChromeHeadless without setting no-sandbox flags — causes crashes in CI due to restricted environments.
  • Hardcoding binary paths that don’t exist in the CI runner (e.g., macOS path on Linux CI).
  • Forgetting to install Chrome in the CI environment, leading to “command not found” errors.
  • Omitting --disable-dev-shm-usage, which can cause memory issues in Docker-based CI runners.
  • Not updating browsers array to use the custom launcher name.

Tips

  • Use Puppeteer to manage Chromium — it guarantees a compatible, known version and provides the executable path programmatically.
  • Log the Chrome version in CI: $(which google-chrome) --version helps debug version mismatches.
  • Reuse the same Chrome instance flags across projects to maintain consistency.
  • Set single-process flag cautiously — it can help in constrained environments but may hide issues.
  • Test locally with the same flags to catch config issues early.

Troubleshooting

If Karma fails to launch Chrome:

  • “Chrome not found” error: Verify Chrome is installed and the binary path is correct. Run which google-chrome or which chromium-browser in CI to confirm.
  • Chrome crashes on launch: Add --no-sandbox and --disable-setuid-sandbox flags. These are often required in containerized environments.
  • Tests hang or timeout: Include --disable-dev-shm-usage and --no-zygote to reduce memory pressure.
  • Wrong Chrome version used: Explicitly install the desired version in CI and verify via google-chrome --version.
  • Environment variable not recognized: Ensure CHROME_BIN is exported in the same shell session as Karma. Use env: in CI config or prefix the command.

Frequently Asked Questions

Why do I need to set CHROME_BIN?

Karma may not detect Chrome automatically in CI environments. Setting CHROME_BIN ensures Karma uses the correct executable, especially when Chrome is installed in non-standard paths.

Can I use Chromium instead of Chrome?

Yes. Use the Chromium binary path (e.g., /usr/bin/chromium-browser) and ensure it’s installed in CI. Many Linux distributions package Chromium by default.

What flags are required for headless Chrome in CI?

Essential flags include --no-sandbox, --disable-setuid-sandbox, and --disable-dev-shm-usage. These prevent crashes in restricted environments like Docker.

How do I find the Chrome binary path in Puppeteer?

Run require('puppeteer').executablePath() in a Node script. This returns the full path to the bundled Chromium executable.

Does this work with Docker?

Yes. Ensure Chrome is installed in the Docker image and the binary path matches. Use the same flags and environment variables as in CI.

Can I run this on GitHub Actions and GitLab CI?

Yes. The configuration is platform-agnostic. Just ensure Chrome is installed and the binary path is correct for the runner’s OS.