How to Setup Puppeteer In CentOS 7

How to Setup Puppeteer in CentOS 7

Setup Puppeteer In CentOS 7 is easy but gets tricky in Linux distributions. Few weeks back we have Completed a project using puppeteer . Now the Question was Where Should we deploy ?

Our code is written in Windows and we wanted to move code to Production . Now we have CentOS 7 in prod . We were struggling to run Puppeteer on Prod machine .

Learn Puppeteer and Nodejs and lessons here

Have a look at Environment details we have used to deploy a puppeteer .

Environment Details:

Have a look at Steps we Have Followed to Deploy A puppeteer backed application in CentOS 7

Step 1:- Install Node JS In CentOS 7

curl -sL | sudo -E bash -

yum install nodejs

Step 2 :- Install Puppeteer In CentOS 7

npm i puppeteer

Step 3 :- Install Chromium In CentOS 7

yum install pango.x86_64 libXcomposite.x86_64 libXcursor.x86_64 libXdamage.x86_64 libXext.x86_64 libXi.x86_64 libXtst.x86_64 cups-libs.x86_64 libXScrnSaver.x86_64 libXrandr.x86_64 GConf2.x86_64 alsa-lib.x86_64 atk.x86_64 gtk3.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

Step 4 :- Install puppeteer in CentOs7

npm install --save puppeteer

Now Let’s Write a Sample Code to run Puppeteer in headless mode and name that file as test.js

'use strict';
const puppeteer = require('puppeteer');
const fs = require('fs');
(async () => {"Starting browser");
  let browser;
  try {
    browser = await puppeteer.launch({});
  } catch (e) {"Unable to launch browser mode in sandbox mode. Lauching Chrome without sandbox.");
    browser = await puppeteer.launch({args:['--no-sandbox']});
  }"Browser successfully started");"Closing browser");
  await browser.close();"Done");

Step 5 :- Run the Code , you will see the following output

running puppeteer centos7 without sandbox mode

Real problem starts here , We are not able to launch browser without --no-sandbox flag.

I Challenge most of you will face this problem while you setup puppeteer in Centos 7. SO Follow down this simple hack to solve this . I struggled a lot , I think you may or may not . But if you have reached this section of article then i firmly believe you have faced this as well.

For running Puppeteer in sandbox mode we need to do few changes as of now .

If you navigate to node_modules/puppeteer/.local-chromium/linux-549031, you notice that for puppeteer there is a file named chrome_sandbox (with an underscore).

Renaming this file to chrome-sandbox and perform following steps below

sudo mv chrome_sandbox chrome-sandbox
sudo chown root chrome-sandbox
sudo chmod 4755 chrome-sandbox

So what are we doing here ,

We are renaming the chrome_sandbox folder to chrome-sandbox as standard chromium searches for file with chrome-sandbox. Now run the test file again.

$ node test.js

Starting browser
Browser successfully started
Closing browser

Conclusion :-

While running puppeteer we were facing issues Unable to connect to chrome . While searching on internet about the problem i got this solution . Hence sharing this with you all . Let me know if this helps .

References :-