PhantomJS is a headless browser, which means it runs without a graphical interface and lets you automate web page interactions through scripting. I needed it for some automated testing at work and ran into a few things worth writing down, mostly because half the guides out there are either outdated or assume you already know what you’re doing.
The version we’re installing is 2.1.1, which is the last release. The project has been unmaintained since 2017, but it still does what it needs to do for basic scraping and screenshot tasks.
First things first, update your packages so nothing unexpected breaks:
sudo apt update && sudo apt upgrade
Then grab the dependencies. You need a few libraries for rendering and SSL support:
sudo apt install build-essential chrpath libssl-dev libxft-dev libfreetype6 libfreetype6-dev libfontconfig1 libfontconfig1-dev
Once that’s sorted, download the binary from Bitbucket:
wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2
Extract it to /usr/local/share/:
sudo tar xvjf phantomjs-2.1.1-linux-x86_64.tar.bz2 -C /usr/local/share/
Now create a symlink so you can run phantomjs from anywhere:
sudo ln -sf /usr/local/share/phantomjs-2.1.1-linux-x86_64/bin/phantomjs /usr/local/bin
And finally, check it worked:
phantomjs --version
Should print 2.1.1. If it doesn’t, double-check the symlink path and make sure you’re not missing any of those libraries from step two.