Search code examples
node.jsubuntunpmvpspuppeteer

How to run a node Puppeteer app on Ubuntu Server as root


I'm trying to run node apps that use the npm package Puppeteer to scrape websites, using VPSes that use root as the default user I login as.

It never works. I always have to spend half an hour googling to find the solution... well no more.


Solution

  • Do this:

    0. Run sudo apt update

    1. Install libx stuff

    sudo apt-get install libpangocairo-1.0-0 libx11-xcb1 libxcomposite1 libxcursor1 libxdamage1 libxi6 libxtst6 libnss3 libcups2 libxss1 libxrandr2 libgconf-2-4 libasound2 libatk1.0-0 libgtk-3-0
    

    2. Set --no-sandbox argument in the launch() part of your Puppeteer app (only required if you are running as root/root's cron)

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

    E: as Niko pointed out, --no-sandbox opens your server to a malicious attack from a website the scraper visits. It looks like there are some StackOverflow questions elsewhere that post alternatives that aren't awful to implement. I like David's answer here which seems to suggest that even in 2021 --no-sandbox is still a necessary evil in some cases.

    E2: See answer below. It looks like in 2021 we also have to install libgbm-dev