Getting started with HHVM


Facebook deploys one of the biggest PHP codebases in the world. They’re not only pushing the boundaries of what you can do with PHP, but also PHP itself. A few years ago they open-sourced their hiphop compiler and nowadays facebook.com runs on their latest generation of the HipHop Virtual Machine (hhvm).

What Facebook says about HipHop:

HipHop is Facebook's complete toolchain for the PHP language: interpreter, JIT compiler and debugger.

Facebook.com was motivated to create HipHop to save recources on their servers. They claim to be 40% faster than their previous version that was in fact a PHP to C++ compiler which was already faster than plain PHP 1.

HHVM Performance with addition of IR

source: Wow HHVM is fast…too bad it doesn’t run my code

In this blog series we’ll get you started with hhvm. We’ll get the symfony standard edition running and show the ins and outs of debugging your code with hhvm (which is awesome!). Today, we’ll start of by setting up hhvm in your own vagrant box.

Create the Vagrant box

The first thing you have to do is install Vagrant (if you haven’t already). You can follow the instructions on the official site. With Vagrant installed we can get started. First create a directory called hhvm for this project and initialize Vagrant.

mkdir hhvm
cd hhvm
vagrant init precise64 http://files.vagrantup.com/precise64.box

precise64 is an out-of-the-box Ubuntu 12.04 VirtualBox image. A file named Vagrantfile is created in the hhvm directory. Open this file in your favorite editor to change and add the following before the ‘end’ keyword to give the VM some more RAM:

config.vm.provider "virtualbox" do |v|
  v.customize ["modifyvm", :id, "--memory", 4096]
end

Or if you use an old version of Vagrant (< 1.1)

config.vm.customize ["modifyvm", :id, "--memory", 4096]

Boot your newly configured VM with vagrant up:

$ vagrant up

This will take a few minutes because Vagrant downloads and creates the virtual machine the first time it is boot. When the virtual machine is booted, you can access it using ssh. This can be done using vagrant with the command vagrant ssh.

Compiling HHVM

Once you logged in to your VM we can start building HHVM. On the wiki pages of the HHVM github repository you can find the necessary steps. For your convenience I copied them to this blog post.

Packages installation

Using sudo or as root user: (you may have to run sudo apt-get update first)

sudo apt-get install git-core cmake g++ libboost1.48-dev \
  libmysqlclient-dev libxml2-dev libmcrypt-dev libicu-dev \
  openssl build-essential binutils-dev libcap-dev \
  libgd2-xpm-dev zlib1g-dev libtbb-dev libonig-dev \
  libpcre3-dev autoconf libtool libcurl4-openssl-dev \
  libboost-regex1.48-dev libboost-system1.48-dev \
  libboost-program-options1.48-dev \
  libboost-filesystem1.48-dev wget memcached \
  libreadline-dev libncurses-dev libmemcached-dev \
  libbz2-dev libc-client2007e-dev php5-mcrypt \
  php5-imagick libgoogle-perftools-dev \
  libcloog-ppl0 libelf-dev libdwarf-dev subversion
Getting HipHop source-code
mkdir dev
cd dev
git clone git://github.com/facebook/hiphop-php.git
cd hiphop-php
export CMAKE_PREFIX_PATH=`pwd`/..
export HPHP_HOME=`pwd`
cd ..
Building third-party libraries
libevent
git clone git://github.com/libevent/libevent.git
cd libevent
git checkout release-1.4.14b-stable
cat ../hiphop-php/hphp/third_party/libevent-1.4.14.fb-changes.diff | patch -p1
./autogen.sh
./configure --prefix=$CMAKE_PREFIX_PATH
make
make install
cd ..
libCurl

Make sure that your system time is correct, otherwise ./configure will fail.

git clone git://github.com/bagder/curl.git
cd curl
./buildconf
./configure --prefix=$CMAKE_PREFIX_PATH
make
make install
cd ..

Note: If you're building a version of cURL older than 7.28.0 (for whatever reason), you'll need to apply the patch in src/third_party to update it prior to the make step.

cat ../hiphop-php/hphp/third_party/libcurl-7.22.1.fb-changes.diff | patch -p1
Google glog
svn checkout http://google-glog.googlecode.com/svn/trunk/ google-glog
cd google-glog
./configure --prefix=$CMAKE_PREFIX_PATH
make
make install
cd ..
JEMalloc 3.0
wget http://www.canonware.com/download/jemalloc/jemalloc-3.0.0.tar.bz2
tar xjvf jemalloc-3.0.0.tar.bz2
cd jemalloc-3.0.0
./configure --prefix=$CMAKE_PREFIX_PATH
make
make install
cd ..
Building HipHop
cd hiphop-php
export HPHP_HOME=`pwd`
cmake .
make

source: Building and installing HHVM on Ubuntu 12.04

When you are done compiling - on my system it took over half an hour - you can find hhvm in ~dev/hiphop-php/hphp/hhvm. HHVM is statically linked, so if you like, you can copy it to the /usr/local/bin directory, so you can run it directly without adding the complete path.

sudo cp ~/dev/hiphop-php/hphp/hhvm/hhvm /usr/local/bin

Hello world!

Now the most important part of this blog… writing a hello-world-script and running it with hhvm! Create the file hello.php:

<?php
echo "Hello world!";

And run it with the command hhvm hello.php.

Congratulations! You now have a working HHVM install and you can try to run all your scripts with it. In the next blog post of this series we’ll get webserver together with the Symfony standard edition running with hhvm. In the mean time, enjoy playing!