DEV Community

Kaibalya Kar
Kaibalya Kar

Posted on

๐Ÿš€ How to Install Chromium for Puppeteer on AWS (EC2 or Lambda)

๐Ÿง  Why This Happens

AWS environments (like Amazon Linux 2 on EC2 or Lambda) donโ€™t come with Chromium installed by default. Also, the headless version Puppeteer tries to download is often incompatible with AWS's OS.

To make Puppeteer work, you need:

  1. A compatible headless Chromium binary.

  2. Proper dependencies installed (fonts, sandbox libs, etc.).

  3. The right launch options.

๐Ÿง  Why This Happens

AWS environments (like Amazon Linux 2 on EC2 or Lambda) donโ€™t come with Chromium installed by default. Also, the headless version Puppeteer tries to download is often incompatible with AWS's OS.

To make Puppeteer work, you need:

  1. A compatible headless Chromium binary.

  2. Proper dependencies installed (fonts, sandbox libs, etc.).

  3. The right launch options.

sudo yum update -y

# Install missing libraries
sudo yum install -y \
  atk.x86_64 \
  cups-libs.x86_64 \
  gtk3.x86_64 \
  libXcomposite.x86_64 \
  libXcursor.x86_64 \
  libXdamage.x86_64 \
  libXext.x86_64 \
  libXi.x86_64 \
  libXrandr.x86_64 \
  libXss.x86_64 \
  libXtst.x86_64 \
  pango.x86_64 \
  alsa-lib.x86_64 \
  ipa-gothic-fonts \
  xorg-x11-fonts-100dpi \
  xorg-x11-fonts-75dpi \
  xorg-x11-utils \
  xorg-x11-fonts-cyrillic \
  xorg-x11-fonts-Type1 \
  xorg-x11-fonts-misc \
  wget
Enter fullscreen mode Exit fullscreen mode

โœ… Step 2: Download a Compatible Chromium Binary

cd ~
mkdir chromium && cd chromium

wget https://github.com/Sparticuz/chromium/releases/download/v123.0.0/chromium.br
# OR another known working build for Amazon Linux

# Decompress
brew install brotli # if needed
brotli --decompress chromium.br
chmod +x chromium

Enter fullscreen mode Exit fullscreen mode

โœ… Step 3: Point Puppeteer to This Chromium

Now in your Node.js code:

import puppeteer from 'puppeteer-core';

const browser = await puppeteer.launch({
  executablePath: '/home/ec2-user/chromium/chromium',
  headless: true,
  args: [
    '--no-sandbox',
    '--disable-setuid-sandbox',
  ],
});
Enter fullscreen mode Exit fullscreen mode

โ˜๏ธ Bonus: Using Puppeteer with Chromium in AWS Lambda?

Use chrome-aws-lambda:

npm install puppeteer-core chrome-aws-lambda

Then:

import chromium from 'chrome-aws-lambda';
import puppeteer from 'puppeteer-core';

export const handler = async () => {
  const browser = await puppeteer.launch({
    args: chromium.args,
    defaultViewport: chromium.defaultViewport,
    executablePath: await chromium.executablePath,
    headless: chromium.headless,
  });

  const page = await browser.newPage();
  await page.goto('https://example.com');
  const screenshot = await page.screenshot();

  await browser.close();

  return screenshot;
};
Enter fullscreen mode Exit fullscreen mode

๐Ÿงช Test It

After setup:

node yourScript.js
Enter fullscreen mode Exit fullscreen mode

โœ… If all goes well, Chromium should launch and Puppeteer will work as expected!


โœ… Summary

Task Done

Install system dependencies โœ…
Download compatible Chromium โœ…
Launch Puppeteer with custom binary โœ…

Running Puppeteer on AWS isnโ€™t plug and play and but once Chromium is correctly installed and referenced, everything runs smoothly.

Top comments (0)