DEV Community

Cover image for Installing Puppeteer on an Ubuntu AWS EC2 Instance
chis0m
chis0m

Posted on • Updated on

Installing Puppeteer on an Ubuntu AWS EC2 Instance

Haven worked on projects where I converted HTML/CSS to pdf, I have come to learn a few things. First, I must say that Puppeteer in my opinion is still the best converter from the server-side of things.

So, in this article, I will be discussing how to install Puppeteer and mitigate errors that are likely to arise.

Scenario:
My project was a Laravel/PHP project which communicated with a NodeJS server through a socket within the same localhost. So PHP sends the JSON data to the NodeJS, then NodeJS converts the data with a predefined HTML template into pdf, saves the file, and writes the result(file) back to PHP through the socket. Php on receiving the pdf file returns the result as a downloadable file to the browser.
In this way, I have achieved total separation of concern, highly reusable, and printed out a very clear pdf.
The project was hosted on an AWS EC2/ubuntu instance.

How to install Puppeteer on an Ubuntu instance (18.04 and 20.04)

  • Go to your project directory
  • Install NodeJS/npm if you haven't
  • Run: sudo npm install -g puppeteer --unsafe-perm=true -allow-root && sudo apt install chromium-browser -y

Errors you are likely to face

ubuntu 20.04/18.04 Oh error: Error: Failed to launch the browser process! error while loading shared libraries: libnss3.so: cannot open shared object file: No such file or directory.

Note: You might see this same error for libnss3.so, libgbm.so, libxss1.so or libasound2.so missing packages

Solution:
You can install these packages singly like

  • sudo apt install libnss3-dev
  • sudo apt install libgbm-dev and so on

But here is a complete install

Run:

sudo apt update && sudo apt install -y gconf-service libgbm-dev libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget
Enter fullscreen mode Exit fullscreen mode

This will install all dependencies that might be missing.
If however, you encounter any challenges along the line, you could reach out, I could be of help. Thank you.

Links that were of help to me

Top comments (5)

Collapse
 
haynesgt profile image
Gavin Haynes

Seems to work, though libgbm needs to be changed to libgbm-dev

Collapse
 
chis0m profile image
chis0m

ok thanks, will check that out

Collapse
 
chungvh profile image
vhchung

Thanks! This solution save me a lot of time. It worked on my ubuntu ec2 instance.

Collapse
 
kamalhossain profile image
Kamal Hossain

thanks! this solution worked on my ubuntu ec2 instance. For https://www.npmjs.com/package/node-html-to-image this package.

Collapse
 
moldy23 profile image
Moldy23

Couldn't get it to work for some reason same error I made sure to install it all and in the right place so I'm not really sure what it is...