Skip to content

Instantly share code, notes, and snippets.

@alvinveroy
Created February 21, 2019 13:26
Show Gist options
  • Save alvinveroy/87857ee3c481eb0584bba58717bf4513 to your computer and use it in GitHub Desktop.
Save alvinveroy/87857ee3c481eb0584bba58717bf4513 to your computer and use it in GitHub Desktop.
Installing headless chrome and chromeless on digitalocean.
sudo apt-get update
sudo apt-get install -y libappindicator1 fonts-liberation
cd /tmp
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
sudo dpkg -i google-chrome*.deb

In case there was an error

sudo apt-get -f install
sudo dpkg --configure -a

Test your installation and check the chrome version

google-chrome-stable -version

Spawn a new screen

screen

If screen is not yet installed on your droplet install it using apt-get

sudo apt-get install screen -y

Now you can run an instance of a headless chrome (make sure you're not running as root!)

google-chrome-stable --remote-debugging-port=9222 --disable-gpu --headless

To detach on the current screen type CTRL-A then D

Install the latest node.js version through PPA

curl -sL https://deb.nodesource.com/setup_11.x | sudo -E bash -
sudo apt-get install -y nodejs

Create a directory for your project and install chromeless

mkdir chromeless-project
cd chromeless-project
npm install chromeless

Open a file called app.js using your favorite text editor such as vi

nano app.js

Paste this code

const { Chromeless } = require('chromeless')
 
async function run() {
  const chromeless = new Chromeless()
 
  const screenshot = await chromeless
    .goto('https://www.google.com')
    .type('chromeless', 'input[name="q"]')
    .press(13)
    .wait('#resultStats')
    .screenshot()
 
  console.log(screenshot) // prints local file path or S3 url
 
  await chromeless.end()
}
 
run().catch(console.error.bind(console))

Now run it

node app.js

You should get a screenshot of a google search for "chromeless"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment