User Agents

Web browsers and everyday web tech, explained

Fix Puppeteer Chrome Error on Elastic Beanstalk

· 6 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

If your Puppeteer script fails on AWS Elastic Beanstalk with an error like Could not find Chrome or No usable sandbox!, it's almost always because the default Amazon Linux environment lacks the browser, its dependencies, or the right sandbox settings. This guide walks you through the exact steps to get Puppeteer running in production on Elastic Beanstalk, including installing Chrome, adding missing libraries, and configuring the right launch flags.

What You'll Need

  • An AWS account with access to Elastic Beanstalk
  • Your application code deployed to Elastic Beanstalk (Node.js platform)
  • SSH access to the EC2 instance (or ability to redeploy with config files)
  • Puppeteer installed in your project (version 22 or newer recommended)

Step-by-Step

  1. Check your current setup. SSH into your Elastic Beanstalk instance and run which chromium or which google-chrome. If nothing appears, Chrome isn't installed. Also, check your Puppeteer version with npm list puppeteer — older versions may have different installation paths.
  2. Create an .ebextensions directory in your project root if it doesn't exist. This is where you'll put configuration files that run during deployment.
  3. Create a file named 01-install-chrome.config inside .ebextensions. This file will install Chrome and its dependencies using the platform's package manager. For Amazon Linux 2 (the default for most recent Beanstalk Node.js platforms), use yum:
  4. commands:
      01_install_chrome:
        command: "yum install -y https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm"
        ignoreErrors: false
      02_install_deps:
        command: "yum install -y libX11 libXcomposite libXdamage libXrandr libXext libXcursor libXi libXtst libXss libXfixes alsa-lib atk at-spi2-atk at-spi2-core cups-libs dbus-libs libdrm libgbm libxkbcommon libxshmfence mesa-libGL pango nss nspr"
    

    If you're on Amazon Linux 2023 (the newer default), use dnf instead of yum. Replace yum with dnf in the commands above.

  5. Ensure Puppeteer can find Chrome. By default, Puppeteer looks for Chrome in a few standard locations. After installation, Chrome will be at /usr/bin/google-chrome. In your Node.js code, pass the executable path explicitly:
  6. const browser = await puppeteer.launch({
      executablePath: '/usr/bin/google-chrome',
      args: ['--no-sandbox', '--disable-setuid-sandbox']
    });
  7. Add the sandbox flags. The --no-sandbox flag is essential on Elastic Beanstalk because the default EC2 user is not root and the Chrome sandbox often fails without proper kernel settings. You can also set this in your Puppeteer launch options as shown above.
  8. Deploy your changes. Commit the .ebextensions folder and your code changes, then run eb deploy. The configuration will run during deployment.
  9. Test your application. After deployment, hit your endpoint that uses Puppeteer. If it still fails, SSH into the instance and run google-chrome --version to confirm the installation. Also, run your Node.js script manually to see the exact error.
  10. If you're using Puppeteer's bundled Chromium (via puppeteer-core or the default download), you may need to install additional shared libraries. The yum install command above covers the common ones, but if you see errors like error while loading shared libraries: libnss3.so, you need to add that package. Use yum provides */libnss3.so to find the package name and install it.

Common Mistakes

  • Forgetting the --no-sandbox flag. This is the most common cause of Chrome failing to launch on Elastic Beanstalk. Always include it unless you have a specific reason not to.
  • Using the wrong package manager. Amazon Linux 2 uses yum, but Amazon Linux 2023 uses dnf. Mixing them up will cause installation failures.
  • Not committing the .ebextensions folder. If you don't commit it, the deployment won't include the installation commands, and your instance will remain without Chrome.
  • Assuming Puppeteer will find Chrome automatically. Even after installation, Puppeteer may not look in the right place. Always specify executablePath.
  • Ignoring missing libraries. Chrome needs many system libraries. If you skip the dependency installation, you'll get cryptic errors like Failed to move to new namespace or libX11.so.6: cannot open shared object file.

Tips

  • Use a custom AMI if you need to install Chrome frequently. You can create a custom AMI with Chrome pre-installed and use it for your Beanstalk environment. This speeds up deployments.
  • Set the PUPPETEER_EXECUTABLE_PATH environment variable. Instead of hardcoding the path in your code, you can set this variable in your Beanstalk environment properties to /usr/bin/google-chrome, and then in your code use executablePath: process.env.PUPPETEER_EXECUTABLE_PATH.
  • Consider using puppeteer-core. If you don't need Puppeteer to download its own Chromium, use puppeteer-core and rely on the system Chrome. This reduces deployment size and avoids version mismatches.
  • Use the --disable-dev-shm-usage flag. On small EC2 instances, the shared memory space may be limited, causing Chrome to crash. Adding this flag uses /tmp instead.

Troubleshooting

Error: Could not find Chrome after following steps. Verify that the .ebextensions file actually ran. Check the Beanstalk logs (in the AWS console, go to your environment's Logs tab) and look for the command output. If you see errors like command not found, the package manager might be wrong. Also, confirm that the file is named correctly and is in the .ebextensions directory at the root of your project.

Error: No usable sandbox! This means the --no-sandbox flag isn't being applied. Double-check that you're passing args: ['--no-sandbox'] in your launch() call. Also, if you're using a library that wraps Puppeteer (like puppeteer-cluster), ensure it forwards the args.

Error: Missing X server or $DISPLAY This usually happens when trying to run Chrome with a GUI. Add the --headless flag to your launch arguments: args: ['--no-sandbox', '--headless'].

Error: Failed to launch chrome! spawn /usr/bin/google-chrome ENOENT This means the executable path is wrong or Chrome isn't installed. Run which google-chrome on the instance to confirm the path, and adjust your executablePath accordingly.

Error: libX11.so.6: cannot open shared object file Missing dependencies. Re-run the yum install command for the libraries listed in the step above. If you're on Amazon Linux 2023, use dnf install.

Frequently Asked Questions

Why does Puppeteer need --no-sandbox on Elastic Beanstalk?

The Chrome sandbox relies on kernel features that are often disabled in containerized or restricted environments like Elastic Beanstalk. Without the flag, Chrome fails to start with a sandbox error. Disabling it is a common workaround, but be aware it reduces security isolation.

Can I use Puppeteer's bundled Chromium instead of installing Chrome?

Yes, but you still need to install the shared libraries. The bundled Chromium is downloaded during npm install, but it won't run without system dependencies. You can install the same libraries listed in the steps above, and then point executablePath to the location of the downloaded Chromium (usually node_modules/puppeteer/.local-chromium/linux-*/chrome-linux/chrome).

How do I know which Amazon Linux version my Beanstalk uses?

Check your environment's platform in the Elastic Beanstalk console. It will say something like "Node.js 18 running on 64bit Amazon Linux 2" or "Amazon Linux 2023". You can also SSH in and run cat /etc/os-release.

Is it safe to disable the sandbox in production?

It's a trade-off. The sandbox adds a layer of protection against malicious web content. If you're only visiting trusted sites, the risk is low. For better security, you could try enabling the sandbox by running Chrome as a non-root user with proper kernel settings, but that's more complex and often not worth it on Beanstalk.

Why does Chrome crash with Out of memory?

Your EC2 instance may have insufficient memory. Chrome is memory-hungry, especially with multiple pages. Try upgrading to a larger instance type, or limit the number of concurrent pages. Also, add the --disable-dev-shm-usage flag to use /tmp instead of /dev/shm.

Do I need to install Chrome on every deployment?

Yes, because Elastic Beanstalk creates new instances during deployments if you're using immutable updates. The .ebextensions commands run on each new instance, so your setup is consistent. If you use a custom AMI, you can avoid reinstallation.