Installing PhantomJS and CasperJS on Linux

Recently I’m getting more and more enthusiastic about using CasperJS as a scraper, but also for automating some web stuff I’m doing on a regular basis (downloading binaries, filling forms, etc.). I’m not explaining CasperJS and it’s uses here, but will quote the official website to tell what CasperJS is all about:

CasperJS is an open source navigation scripting & testing utility written in Javascript for the PhantomJS WebKit headless browser and SlimerJS (Gecko). It eases the process of defining a full navigation scenario and provides useful high-level functions, methods & syntactic sugar for doing common tasks, such as: defining & ordering browsing navigation steps, filling & submitting forms, clicking & following links, capturing screenshots of a page (or part of it), testing remote DOM, logging events, downloading resources (including binary ones) and scraping Web contents

Quite a powerfull tool, so let’s install it!

CasperJS has some prerequisites, you must have PhantomJS 1.8.1 or greater and Python 2.6 or greater installed. Python is easily installed by using the package manager (for Debian and Ubuntu apt, other distributions might use another package manager), but the latest PhantomJS isn’t always in the repositories.

Installing PhantomJS

So we need to install PhantomJS first, luckily it’s pretty easy to install PhantomJS by following these steps:

  1. Login to the console and navigate to /usr/local/share:

    [bash]cd /usr/local/share[/bash]

  2. Head to Under the header “Linux” you are able to find the download url for the packages
  3. Download the package using wget:

    [bash]sudo wget[/bash]

    sudo must be used because of the permissions of the /usr/local/share folder

  4. Because we need to organize this package ourselfs, I decided to extract the contents of the tar file to a separate directory for each version:

    [bash]sudo tar -xvf phantomjs-1.9.1-linux-x86_64.tar.bz2[/bash]

    In /usr/local/share there will be a folder called phantomjs-1.9.1-linux-x86_64, because I want to organize it, i’m moving it to a little bit nicer named folder:

    [bash]sudo mv phantomjs-1.9.1-linux-x86_64 phantomjs-1.9.1[/bash]

  5. The next step is making the phantomjs binary widely available on the system. I could add the directory to the environment path, but with every version I would need to change it and for every other manually installed package I will have to edit the environment path again. In my opinion, there’s a better way for organizing this.

    First we make a symlink called phantomjs and point it to the package:

    [bash]sudo ln -s /usr/local/share/phantomjs-1.9.1/ /usr/local/share/phantomjs[/bash]

  6. Then we need to make a symlink in /usr/local/bin (which is in the environment path already) to the binary in /usr/local/share/phantomjs:

    [bash]sudo ln -s /usr/local/share/phantomjs/bin/phantomjs /usr/local/bin/phantomjs[/bash]

  7. The latest PhantomJS should now be installed. Test that by using:

    [bash]phantomjs –version[/bash]

    Which should give you the version number, in this case 1.9.1

PhantomJS is now installed.

When a new version is released, we only have to change the /usr/local/share/phantomjs symlink to point to the new version folder and the whole system will use the new phantomjs version. If the old version is required, we still can use that one by pointing the command to the specific version (for example version 1.8.1):

[bash]/usr/local/share/phantomjs/phantomjs-1.8.1/bin/phantomjs thisscript.js[/bash]

PhantomJS is installed, so now it’s time to install CasperJS.

Installing CasperJS

Installing CasperJS is mostly the same as installing PhantomJS, so I won’t explain it as extensive as I did above with installing PhantomJS. If you want to know why I did things, see the PhantomJS installation instructions.

  1. Login to the console and navigate to /usr/local/share:

    [bash]cd /usr/local/share[/bash]

  2. Head to the CasperJS download page for the download url
  3. Download the latest archive:

    [bash]sudo wget -O casperjs.tar[/bash]

  4. Extract the archive and rename the folder (folder name might be different, so you might want to check that first):

    [bash]sudo tar -xvf casperjs.tar
    sudo mv n1k0-casperjs-0628688 casperjs-0628688[/bash]

  5. Create a symlink to the current version:

    [bash]sudo ln -s /usr/local/share/casperjs-0628688/ /usr/local/share/casperjs[/bash]

  6. Make the casperjs binary available system wide:

    [bash]sudo ln -s /usr/local/share/casperjs/bin/casperjs /usr/local/bin/casperjs[/bash]

  7. Test if it’s working by using:

    [bash]casperjs –version[/bash]

If the last command shows you a version number, then CasperJS is installed successfully. When a new version of CasperJS arrives, just change the symlink in /usr/local/share/casperjs to point to the new version.

For more information about symlinks and adjusting symlinks, see: symlink cheatsheet