Build PhantomJS from Source

Unless it is absolutely necessary to build PhantomJS from source, it is highly recommended to download and install the ready-made binary package instead.

Requirements

Hardware requirements

PhantomJS is still a web browser, albeit headless. Thus, building it from source takes a long time (mainly due to thousands of files in the WebKit module). Estimated build time for a 4-core system is 30 minutes.

Linux requirements

First, install the development packages of the following tools and libraries: GNU C++ compiler, bison, flex, gperf, Perl, Ruby, SQLite, FreeType, Fontconfig, OpenSSL, and ICU. The actual package names may vary from one distribution to another.

On Debian-based distro (tested on Ubuntu 14.04 and Debian 7.0), run:

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 python libx11-dev libxext-dev

Note: It is recommend also to install ttf-mscorefonts-installer package.

On Fedora-based distro (tested on CentOS 6), run:

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

Windows requirements

Supported toolchains: MSVC2012 and MSVC2013.

You must have Perl, Python, Ruby, and Git on PATH. Also, note that Git comes with its own version of perl.exe. If you have both Git and a separate Perl installation in your PATH, please make sure that you separate Perl install’s bin folder comes before the git’s bin folder in your PATH.

Please also add the folder <phantomjs_path>\src\qt\3rdparty\gnuwin32\bin to your PATH, as required tools such as bison, flex, and gperf will not be found otherwise. Example:

SET PATH=%CD%\src\qt\3rdparty\gnuwin32\bin;%PATH%

Run the build script from Visual Studio Command Prompt.

Tip: Enabling incremental linking will make the linkage process faster.

OS X requirements

FreeBSD requirements

Build PhantomJS from the FreeBSD Ports Collection: svnweb.freebsd.org/ports/head/lang/phantomjs/

Getting the Code

To obtain the code using Git from the official repository github.com/ariya/phantomjs:

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

From the code checkout:

python build.py

This will take some time. Once it is completed, the executable will be available under the bin subdirectory.

Tip: If the compilation process is interrupted, once started again, the build.py script will continue where left off.