PhantomJS is a headless browser interface that fully (or near enough) supports JavaScript. This is a bit of a game changer for anyone doing automated browser testing or advanced scraping, as you can execute JavaScript functions as easily as 1,2,3.
This is a short guide on getting PhantomJS up and running on Debian 6 Squeeze. Inevitably each distro is different, so hopefully this will help other Debian users find their way.
Install Dependencies
PhantomJS needs a few dependencies to build from scratch; Qt being the most significant, but also Xvfb (X server headless frame buffer) if you want to run it from the terminal (which is very likely):
sudo apt-get install libqt4-dev qt4-qmake
sudo apt-get install xvfb xfonts-100dpi xfonts-75dpi xfonts-scalable xfonts-cyrillic
Configure Xvfb
Unfortunately the Debian package for Xvfb does not include any init scripts to either start Xvfb as a daemon, let alone do it automatically on startup. I have forked someone elses init.d script for Xvfb and tweaked it slightly.
Copy this script to /etc/init.d/Xvfb
:
Now you need to set this init script to be executable and also set it to start up when the system boots:
sudo chmod 755 /etc/init.d/Xvfb
sudo update-rc.d Xvfb defaults
sudo /etc/init.d/Xvfb start
Xvfb should now be running (you can do a ps aux | grep Xvfb
to check).
Checkout a copy of PhantomJS
mkdir -p ~/src && cd ~/src
git clone git://github.com/ariya/phantomjs.git && cd phantomjs
git checkout 1.4
Note: You may be in a position to checkout a more recent version than 1.4; you should verify this before you build
Build PhantomJS from source
qmake-qt4 && make
This will take a few seconds to a few minutes depending on your machine. When it has finished building you need to copy the phantomjs
binary somewhere useful:
sudo cp ~/src/phantomjs/bin/phantomjs /bin
You could also update your PATH to point at the build directory, but this seems a bit messy.
Run your first PhantomJS Script
Create a test-phantom.js
file with the following contents:
And finally you can run it…
DISPLAY=:0 phantomjs test-phantom.js
If you don’t want to set the DISPLAY every time, you can run export DISPLAY=:0
in your bash session, or even add it in to your bash configuration files so it is always set.
Finished
There we go, you should now be up and running with PhantomJS on Debian 6.0/Squeeze without adding any potentially-outdated APT sources and should allow you to update to new versions as and when they come out. Any problems, drop me a comment below.