BinaryWebPark

How to Install PhantomJS 1.8 on a 32-Bit Ubuntu Linux Virtual Machine for Testing Javascript In Your Rails Application

January 14, 2013

What is PhantomJS?

PhantomJS is a “headless webkit”. What does that mean? Webkit is the engine of choice that Chrome, Firefox, and some other browsers use to render the content of a webpage. The “headless” part refers to the fact that PhantomJS never displays web pages. For our purposes, we just need to understand that PhantomJS can execute Javascript like a browser, but without having to display a web page.

First, we check if our Ubuntu 10.04 virtual machine is running as a 32-bit or 64-bit system

Type uname -a at the shell command prompt.

If you get something like:

Linux YOURMACHINENAME 2.6.32-45-generic #101-Ubuntu SMP Mon Dec 3 15:41:13 UTC 2012 i686 GNU/Linux

The i686 means we’re running a 32-bit system. If we had seen x64686_ that would have meant we’re running a 64-bit system.

What does this mean?

As of right now (1/6/13), it means we have to compile and build PhantomJS for Ubuntu system. If we had been running a 64 bit system, we could have just downloaded a binary file and set up some symbolic links and we would have been fine.

Time to build PhantomJS

The build instructions are at http://phantomjs.org/build.html and I’ll repeat them here for your convenience.

=======================================================================

For Ubuntu Linux (tested on a barebone install of Ubuntu 10.04 Lucid Lynx and Ubuntu 11.04 Natty Narwhal):

sudo apt-get install build-essential chrpath git-core libssl-dev libfontconfig1-dev
git clone git://github.com/ariya/phantomjs.git
cd phantomjs
git checkout 1.8
./build.sh

For Amazon EC2 AMI (tested on AMI release 2011.09 and 2012.03):

sudo yum install gcc gcc-c++ make git openssl-devel freetype-devel fontconfig-devel
git clone git://github.com/ariya/phantomjs.git
cd phantomjs
git checkout 1.8
./build.sh --jobs 1

Note 1: build.sh by default will launch parallel compile jobs depending on the available CPU cores, e.g. 4 jobs on a modern hyperthreaded dual-core processor. If necessary, e.g. when building on a virtual machine/server or other limited environment, reduce the jobs by passing a number, e.g ./build.sh –jobs 1to set only one compile job at a time.

Note 2: After finishing the build, run deploy/package.sh which creates PhantomJS binary tarball that can be moved around and/or extracted to a different location. This is the correct way to install the built binary into an arbitrary directory, i.e. do not simply copy the executable as it won’t work.

=======================================================================

After running deploy/package.sh, you’ll have a binary tarball that can be extracted to a different location

This is what I did to get it working on my machine1. You’ll have to change your file and directory name accordingly, depending on what version of PhantomJS you’re running by the time you’re reading this installation guide.

mv ~/phantomjs-1.8.1-linux-i686.tar.bz2 /usr/local/share
cd /usr/local/share
sudo tar xvf phantomjs-1.8.1-linux-i686.tar.bz2
sudo ln -s /usr/local/share/phantomjs-1.8.1-linux-i686 /usr/local/share/phantomjs
sudo ln -s /usr/local/share/phantomjs/bin/phantomjs /usr/local/bin/phantomjs
phantomjs --version

Then you should see the version number displayed. After that, you’re ready to test the javascript in your rails application.

Sources:
  1. http://www.joyceleong.com/log/installing-phantomjs-on-ubuntu/