How To Compile PhantomJS on the Raspberry Pi 2

How To Compile PhantomJS on the Raspberry Pi 2

PhantomJS is an awesome tool for web testing, web scraping and complex scripting. In a future post I will be talking about how to use PhantomJS to check and report on cell plan use. Because the Raspberry Pi 2 is new, no one has created a PhantomJS repository entry so before I could use it on my Raspberry Pi 2 I had to compile it from scratch. The PhantomJS team has done a super job automating most of the process (check out the instructions here) but a few tweaks are needed for the RPi2.

Some Groundwork

To start, PhantomJS is a big project. On the Raspberry Pi 1 it takes 2 days to compile. The RPi2 is faster and doing some preliminary work will reduce the compile time to a few hours. To start, don’t use a SD drive or a flash drive except for the initial boot. Move your main OS to a USB hard drive or SSD drive. See these instructions. This will make a huge difference since the compile process is disk intensive. Next, you will need swap space. The Pi2 has 1GB of ram but it isn’t enough. One compile step takes about 1.1GB and it crashes without swap. Follow these instructions.

Required Packages

If you are running Raspian install these packages:
sudo apt-get install build-essential g++ flex bison gperf ruby perl \
libsqlite3-dev libfontconfig1-dev libicu-dev libfreetype6 libssl-dev \
libpng-dev libjpeg-dev

On Arch Linux you will need to use pacman to get these packages:
pacman -S gcc make flex bison gperf ruby openssl fontconfig \
sqlite libpng libjpeg git python perl

Download the Code and Build

Switch to a directory where you can store the code and use git to download the latest source.
git clone git://github.com/ariya/phantomjs.git
cd phantomjs
git checkout 2.0

And build it with:
./build.sh --jobs 2

Note the –jobs parameter. Normally the build process will use all available CPU cores during the build. On a desktop or laptop this will speed up the build process tremendously. It helps on the RPi too to a point. Using all the cores for builds requires 4x the ram of a single build process. During one step the ram use spikes so much that the compile process is spending most of the time swapping ram rather than doing any real work. Limiting the compile jobs to 2 cuts the swapping to a reasonable amount.

Test it Out

After a few hours the process should be complete. To test
cd bin
./phantomjs --version

And you should see
2.0.1-development

That’s it! As I said, in a few weeks I will show you how to track your cell phone plan with PhantomJS so stay tuned.