Installing PhantomJS on Amazon Linux / Fedora / RedHat / CentOS over SSH

OS: Amazon Linux / Fedora / Redhad / CentOS
method: SSH
program: PhantomJS
dependency: Python

What is PhantomJS

PhantomJS is what is known as a headless web browser. This means it’s a web browser like Firefox or Google Chrome, without the bulky window that you’ve become very familiar with. This is great for program testing and website testing using scripts. It greatly reduces load time and thus makes testing a breeze.

Recent news shows that Google has decided to release their own headless version of Chrome for their extensive testing they do, and Firefox has been on that bandwagon for a long time now too. Which version you pick I will not discuss here, but this tutorial is for PhantomJS on a Linux computer over SSH.

Installing PhantomJS on Linux Computer over SSH

It was incredibly frustrating for me to find how to install PhantomJS onto my computer since it’s a Linux Amazon distro, which is a stripped down version of Linux meant for running on Amazon’s EC2 cloud computing servers.

However, it uses yum so that gives us a hint that much of the tutorials for CentOS or RedHat will work with this.

 

Build PhantomJS

note: there was a suggestion on PhantomJS webpage that one should just download the package and install it simply, with some dependencies, however, I wasn’t able to get this working so I defaulted to Build.

I am assuming you have Python installed. You’ll need it. (to install it, type: sudo yum install python3.4)

Make sure that git is intalled:

sudo yum install git

Directly from the Build PhantomJS page, we’ll first install the dependencies:

sudo yum -y install gcc gcc-c++ make flex bison gperf ruby \
openssl-devel freetype-devel fontconfig-devel libicu-devel sqlite-devel \
libpng-devel libjpeg-devel

Next, we’re going to add the git and download the source to begin the build, which you type into your SSH window one line at a time, pressing enter at the end of each line:

git clone git://github.com/ariya/phantomjs.git
cd phantomjs
git checkout 2.1.1
git submodule init
git submodule update

python build.py

This last command (build.py) will prompt you with an alert that it will take some time, of which you type Y for yes, continue.

Once it is completed, the executable will be available under the bin subdirectory.

Linking the executable in the Bin SubDirectory

Once this is complete, you’ll still not be able to run PhantomJS unless you ensure that the location of the PhantomJS file is included in the system $PATH. This is the way for the system to know what you mean when you type into the command window: phantomjs.

The bin subdirectory is most likely going to be located in the folder where PhantomJS was installed to, which if you weren’t running sudo for the above, is going to be: /home/user/phantomjs/bin/

To view the directories that are included in your system path, type:

echo $PATH

This will give you a list of locations that are included in the PATH, seperated by a semi-colon ;. There is most likely the dir: /home/user/bin included in this path, so instead of adding a new symbolic link (ln) or adding a new dir to the path, I just copied the phantomjs executable into this bin folder.

note: keep in mind, ‘user’ here is your Linux user, so it may be another name.

If you run the command:

sudo ls -ln /home/user/

and don’t see a /bin folder in there, you’ll have to first create one:

sudo mkdir /home/user/bin/

then you can copy the phantomjs executable to the /bin/ folder:

sudo cp -Rp /home/user/phantomjs/bin/phantomjs /home/user/bin/

make sure it’s been copied over successfully:

sudo ls -la /home/user/bin

which will hopefully show you your phantomjs file!

Test PhantomJS

No point dusting your hands off yet, who knows if it’s really worked?! Time to test.

I’ve taken this code from the PhantomJS Quick Start page, that will check how quickly you can view a page using the PhantomJS headless magic:

var page = require(‘webpage’).create(),
system = require(‘system’),
t, address;

if (system.args.length === 1) {
console.log(‘Usage: loadspeed.js <some URL>’);
phantom.exit();
}

t = Date.now();
address = system.args[1];
page.open(address, function(status) {
if (status !== ‘success’) {
console.log(‘FAIL to load the address’);
} else {
t = Date.now() – t;
console.log(‘Loading ‘ + system.args[1]);
console.log(‘Loading time ‘ + t + ‘ msec’);
}
phantom.exit();
});

To run this, create a new file called loadspeed.js, which we’ll do without sudo permissions:

vi loadspeed.js

Then copy the code above, and right click in the SSH terminal to paste it all in. Then type: ‘:wq’ to save the file and exit.

Now we run this little codelet to see how fast it will load google.com with:

phantomjs loadspeed.js http://www.google.com

So, how quickly did yours load? Mine:

load time for google

Hope this works!

 

Resources:

Leave a Reply

fifteen + 17 =