Installing PhantomJS
Let's begin the PhantomJS Cookbook with the recipe that is the prerequisite for all of the other recipes—downloading and installing PhantomJS so that it is available on our computers.
Prebuilt binaries of PhantomJS are available for most major platforms, and in the interest of expedience and simplicity, that is how we proceed. PhantomJS is designed to be a stand-alone application, and in most situations, no external dependencies are required.
Getting ready
To install PhantomJS, we will need access to the Internet and permission to install applications.
How to do it…
Perform the following steps to download and install PhantomJS:
- Navigate to the PhantomJS download page at http://phantomjs.org/download.
- Locate and download the prebuilt binary that is appropriate for our system. Prebuilt binaries exist for the following operating systems:
- Windows (XP or later).
- Mac OS X (10.6 or later).
- Linux (for 32-bit or 64-bit systems). Current binaries are built on CentOS 5.8, and should run successfully on Ubuntu 10.04.4 (Lucid Lynx) or more modern systems.
- Extract the prebuilt binary. For Windows and OS X systems, this will be a
.zip
archive; for Linux systems, this will be a.tar.bz2
archive. For Windows machines, the binary should bephantomjs.exe
; for OS X and Linux machines, the binary should bebin/phantomjs
.Note
We should place the binary somewhere on your system that makes sense to us.
- Once extracted, make sure to add PhantomJS to the system's
PATH
.Tip
The
PATH
or search path is a variable on the command line that contains a list of directories searched by the shell to find an executable file when it is called. On POSIX-compatible systems (Linux and OS X), this list is delimited by colons (:
), and on Windows, it is delimited by semicolons (;
). For more information about thePATH
variable, visit http://en.wikipedia.org/wiki/PATH_(variable).For a tutorial that focuses on POSIX-compatible systems, visit http://quickleft.com/blog/command-line-tutorials-path.
For documentation on the Windows
PATH
, visit http://msdn.microsoft.com/en-us/library/w0yaz275(v=vs.80).aspx. - After placing the PhantomJS binary on our
PATH
, we can verify that it was installed by typing the following in the command line:phantomjs –v
The version of PhantomJS that we just installed should print out to the console.
Tip
If we have trouble here, we should check out the troubleshooting guide on the PhantomJS project site at http://phantomjs.org/troubleshooting.html.
How it works…
In an effort to lower the barrier to entry and help drive adoption, the prebuilt binaries of PhantomJS are made available by community volunteers. This is, in part, an acknowledgment that building PhantomJS from the source code can be a complex and time-consuming task. To quote the build page on the PhantomJS site: "With 4 parallel compile jobs on a modern machine, the entire process takes roughly 30 minutes." It is easy to imagine that this might scare off many developers who just want to try it out.
These prebuilt binaries should therefore make it easy to drop PhantomJS onto any system and have it running in minutes. These binaries are intended to be fully independent applications, with no external library dependencies such as Qt or WebKit. On some Linux systems, however, a little extra work may be required to ensure that the libraries necessary for proper font rendering (for example, FreeType
and Fontconfig
) are in place, along with the basic font files.
Note
Throughout this book, our code will assume that we are using Version 1.9 or higher of PhantomJS.
There's more…
In addition to the prebuilt binaries, Mac OS X users may also install PhantomJS using Homebrew. To do this, enter the following as the command line:
brew update && brew install phantomjs
Note that installing PhantomJS with Homebrew also means that we will be compiling it from source.
Tip
Homebrew is an open source, community-run package manager for OS X built on top of Git and Ruby. To find out more information about Homebrew, check out its website at http://brew.sh.
As a bonus, Homebrew also automatically adds PhantomJS to your PATH
.
Installing from Source
In the event that one of the prebuilt binaries is not suitable for your specific situation, you may need to consider building PhantomJS from the source code. If this is the case, you will want to check out the build instructions that are listed at http://phantomjs.org/build.html; note that you will need the developer tools specific to your system (for example, Xcode on OS X and Microsoft Visual C++ on Windows) to be installed before you begin.