puppeteer / puppeteer

JavaScript API for Chrome and Firefox

Home Page:https://pptr.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Ubuntu 18.04 Headless Chrome Node API - Puppeteer - Installation Guide

JohnDotOwl opened this issue · comments

I ran sudo npm i puppeteer on Ubuntu 18.04

Sample Script


const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('https://example.com');
  await page.screenshot({path: 'example.png'});

  await browser.close();
})();


Error
(node:23835) UnhandledPromiseRejectionWarning: Error: Chromium revision is not downloaded. Run "npm install" or "yarn install"
    at Launcher.launch (/var/www/puppeteer/node_modules/puppeteer/lib/Launcher.js:112:15)
    at <anonymous>
(node:23835) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:23835) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Node
v8.10.0

Fix for Above Error
sudo npm install --unsafe-perm

New Error Below

(node:25272) UnhandledPromiseRejectionWarning: Error: Failed to launch chrome!
/var/www/puppeteer/node_modules/puppeteer/.local-chromium/linux-594312/chrome-linux/chrome: error while loading shared libraries: libX11-xcb.so.1: cannot open shared object file: No such file or directory

Fix new Error with 
gconf-service libasound2 libatk1.0-0 libatk-bridge2.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

Based on https://github.com/GoogleChrome/puppeteer

You only have to run the following command in Ubuntu 18.04

npm i puppeteer

Unfortunately this is not enough.

You will require the following Dependencies

sudo apt-get install gconf-service libasound2 libatk1.0-0 libatk-bridge2.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

After which if you run it as per their example , you will receive an error

    (node:28469) UnhandledPromiseRejectionWarning: Error: Failed to launch chrome!
[1025/150325.817887:ERROR:zygote_host_impl_linux.cc(89)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.

The solution to this is

const browser = await puppeteer.launch({args: ['--no-sandbox']});

Adding --no-sandbox

It will work accordingly then. The full working source code is below

    const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch({args: ['--no-sandbox']});
  const page = await browser.newPage();
  await page.goto('http://owlcommand.com');
  await page.screenshot({path: 'example.png'});

  await browser.close();
})();
how to handle captcha with headless chrome puppeteer

This works :)

Based on https://github.com/GoogleChrome/puppeteer

You only have to run the following command in Ubuntu 18.04

npm i puppeteer

Unfortunately this is not enough.

You will require the following Dependencies

sudo apt-get install gconf-service libasound2 libatk1.0-0 libatk-bridge2.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

After which if you run it as per their example , you will receive an error

    (node:28469) UnhandledPromiseRejectionWarning: Error: Failed to launch chrome!
[1025/150325.817887:ERROR:zygote_host_impl_linux.cc(89)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.

The solution to this is

const browser = await puppeteer.launch({args: ['--no-sandbox']});

Adding --no-sandbox

It will work accordingly then. The full working source code is below

    const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch({args: ['--no-sandbox']});
  const page = await browser.newPage();
  await page.goto('http://owlcommand.com');
  await page.screenshot({path: 'example.png'});

  await browser.close();
})();

Thanks a lot, it works.

i think you can also try this way:

 const broswer = await puppteer.launch({
        executablePath:path.resolve(__dirname,'../node_modules/puppeteer/.local-chromium/linux-650583/chrome-linux/chrome')
    });

my enviroment is ubuntu 16.04,you can add the path to the puppteer

@fireairforce this never worked for me :/

i think you can also try this way:

 const broswer = await puppteer.launch({
        executablePath:path.resolve(__dirname,'../node_modules/puppeteer/.local-chromium/linux-650583/chrome-linux/chrome')
    });

my enviroment is ubuntu 16.04,you can add the path to the puppteer

@fireairforce this never worked for me :/

i think you can also try this way:

 const broswer = await puppteer.launch({
        executablePath:path.resolve(__dirname,'../node_modules/puppeteer/.local-chromium/linux-650583/chrome-linux/chrome')
    });

my enviroment is ubuntu 16.04,you can add the path to the puppteer

 const browser = await puppeteer.launch({args: ['--no-sandbox']});

you can try this way?

@fireairforce Yes now going to try that one 🦅 and for that i have do first
sudo apt-get install gconf-service libasound2 libatk1.0-0 libatk-bridge2.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

@fireairforce this never worked for me :/

i think you can also try this way:

 const broswer = await puppteer.launch({
        executablePath:path.resolve(__dirname,'../node_modules/puppeteer/.local-chromium/linux-650583/chrome-linux/chrome')
    });

my enviroment is ubuntu 16.04,you can add the path to the puppteer

 const browser = await puppeteer.launch({args: ['--no-sandbox']});

you can try this way?

works :)

works :)

Which one worked for you?

**i just install dependencies; on my ubuntu live server **
sudo apt-get install gconf-service libasound2 libatk1.0-0 libatk-bridge2.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

Im running BugBuntu 18.04 and none of that is need it
all you need to do is to tell headless: false
wrap the whole thing in a try catch

const puppeteer = require('puppeteer');

(async () => {
    try {

  const browser = await puppeteer.launch({
      headless: false
  })
  const page = await browser.newPage();
  await page.goto('https://www.instagram.com');
  await page.screenshot({path: 'example.png'});

} catch (error) {
    console.log('error: ', error);
}
})();

With puppeteer 3.0.0+ I also had to install libgbm-dev as a dependency

sudo apt install libgbm-dev

I found that with the latest version of puppeteer 3+ which uses the latest chromium, I had to do the following on Ubuntu 20.

sudo apt-get update && apt-get install -y libgtk2.0-0 libgtk-3-0 libnotify-dev libgconf-2-4 libnss3 libxss1 libasound2 libxtst6 xauth xvfb libgbm-dev

In docker I had to add the following to my Docker file:

RUN apt-get update && \
  apt-get install -y libgtk2.0-0 libgtk-3-0 libnotify-dev \
  libgconf-2-4 libnss3 libxss1 \
  libasound2 libxtst6 xauth xvfb \
  libgbm-dev

I also had to add the argument '--no-sandbox' when calling puppeteer.

It is easier if you just install $ sudó apt install build-essential also if you have a fresh version of bugbuntu probably you are missing typhon2.7 so install it as well
sudo apt install python.
Make sure it is 2.. even Though NodeJS supports python 3.. node-gyp stills runs on python 2..
Couple of days ago I created a puppeteer script on the latest bugbuntu version and it ran 😌👌

I tried a simple and crude way, it works:
apt install chromium-browser
Let it install all the dependencies by itself ... Although I don't use chromium installed in this way

commented

I tried a simple and crude way, it works:
apt install chromium-browser
Let it install all the dependencies by itself ... Although I don't use chromium installed in this way

This was the only solution working within my rabbitmq:management container. I like your solution because it's simple and short.

commented

Hello, it also won't work for me, I get the same error (Error: Could not find browser revision 737027. Run "npm install" or "yarn install" to download a browser binary.)

Could you please help me? I did everything you told ^ up...

@Rainbowhat
works... thanks!!

With puppeteer 3.0.0+ I also had to install libgbm-dev as a dependency

sudo apt install libgbm-dev
@rannooo
This worked for me! Thanks!

In docker I had to add the following to my Docker file:

RUN apt-get update && \
  apt-get install -y libgtk2.0-0 libgtk-3-0 libnotify-dev \
  libgconf-2-4 libnss3 libxss1 \
  libasound2 libxtst6 xauth xvfb \
  libgbm-dev

I also had to add the argument '--no-sandbox' when calling puppeteer.

Worked for me, thanks @impressto

I've had the same problem on Ubuntu 20.04. Fixed with installing these packages

sudo apt-get update
sudo apt-get -y --no-install-recommends install libxrandr2 libatk1.0-0 libatk-bridge2.0-0 libx11-xcb1 libxcb-dri3-0 libxcomposite1 libxcursor1 libxdamage1 libcups2 libdrm2 libgbm1 libgtk-3-0 

And it's possible to see the list of missing libraries with ldd

ldd ./node_modules/puppeteer/.local-chromium/linux-782078/chrome-linux/chrome | grep "not found"

With puppeteer 3.0.0+ I also had to install libgbm-dev as a dependency

sudo apt install libgbm-dev

this worked for me

I found that with the latest version of puppeteer 3+ which uses the latest chromium, I had to do the following on Ubuntu 20.

sudo apt-get update && apt-get install -y libgtk2.0-0 libgtk-3-0 libnotify-dev libgconf-2-4 libnss3 libxss1 libasound2 libxtst6 xauth xvfb libgbm-dev

In docker I had to add the following to my Docker file:

RUN apt-get update && \
  apt-get install -y libgtk2.0-0 libgtk-3-0 libnotify-dev \
  libgconf-2-4 libnss3 libxss1 \
  libasound2 libxtst6 xauth xvfb \
  libgbm-dev

I also had to add the argument '--no-sandbox' when calling puppeteer.

this actually worked for me on Digital Ocean Droplet!
thanks a lot!

Based on https://github.com/GoogleChrome/puppeteer

You only have to run the following command in Ubuntu 18.04

npm i puppeteer

Unfortunately this is not enough.

You will require the following Dependencies

sudo apt-get install gconf-service libasound2 libatk1.0-0 libatk-bridge2.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

After which if you run it as per their example , you will receive an error

    (node:28469) UnhandledPromiseRejectionWarning: Error: Failed to launch chrome!
[1025/150325.817887:ERROR:zygote_host_impl_linux.cc(89)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.

The solution to this is

const browser = await puppeteer.launch({args: ['--no-sandbox']});

Adding --no-sandbox

It will work accordingly then. The full working source code is below

    const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch({args: ['--no-sandbox']});
  const page = await browser.newPage();
  await page.goto('http://owlcommand.com');
  await page.screenshot({path: 'example.png'});

  await browser.close();
})();

I am unable to solve the problem with these steps. Pasting the output of npm i puppeteer

npm i puppeteer

> puppeteer@5.3.1 install /home/ubuntu/vendor-module/node_modules/puppeteer
> node install.js

(node:18339) UnhandledPromiseRejectionWarning: /home/ubuntu/vendor-module/node_modules/puppeteer/lib/cjs/puppeteer/install.js:138
                    catch {
                          ^

SyntaxError: Unexpected token {
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:616:28)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
    at download (/home/ubuntu/vendor-module/node_modules/puppeteer/install.js:35:7)
(node:18339) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)

Refer https://stackoverflow.com/questions/64043249/failed-to-launch-chromium-headless-using-puppeteer-on-ubuntu-16-04-aws-ec2-insta for more details

I found that with the latest version of puppeteer 3+ which uses the latest chromium, I had to do the following on Ubuntu 20.

sudo apt-get update && apt-get install -y libgtk2.0-0 libgtk-3-0 libnotify-dev libgconf-2-4 libnss3 libxss1 libasound2 libxtst6 xauth xvfb libgbm-dev

In docker I had to add the following to my Docker file:

RUN apt-get update && \
  apt-get install -y libgtk2.0-0 libgtk-3-0 libnotify-dev \
  libgconf-2-4 libnss3 libxss1 \
  libasound2 libxtst6 xauth xvfb \
  libgbm-dev

I also had to add the argument '--no-sandbox' when calling puppeteer.

Thank you so much. This worked for me on ubuntu 18.04.

i fix it!!!
i was open the browser outside a function like this
const browserPromise = puppeteer.use(StealthPlugin()).launch({ args: ["--no-sandbox"], });
i move it inside the function and it works!

Installing libgbm-dev broke the rescue console (QEMU) for me on Ubuntu 20.04; I was able to get puppeteer working with just the following:

sudo apt-get install xvfb libnss3 libatk1.0-0 libatk-bridge2.0-0 libxcomposite1 libcups2 libxrandr2 libpangocairo-1.0-0 libgtk-3-0 -y
sudo apt-get update
sudo apt-get -y --no-install-recommends install libxrandr2 libatk1.0-0 libatk-bridge2.0-0 libx11-xcb1 libxcb-dri3-0 libxcomposite1 libxcursor1 libxdamage1 libcups2 libdrm2 libgbm1 libgtk-3-0 

Its works after install chromium: sudo apt install -y chromium-browser