So we have heard about the new PHP Virtual Machine from Facebook – HHVM and the static typing flavoured new programming language called “Hack” that comes with HHVM. If you haven’t already, check out hhvm.com and hacklang.org for more details.
Now, if you are on a Linux box, setting up HHVM would seem a little less complicated. But if you are on Windows or Mac OS X, the hell breaks lose. HHVM doesn’t have any support for Windows yet. You can manually compile it on OS X or perhaps use a package manager like Homebrew (which would automate the compilation for you). However, because of some bugs in HHVM, the setup for HHVM 2.4.2 has been failing (as I found out from the homebrew repo pointing to the hhvm bugs) on homebrew. I did have an older version installed but I wanted to go cutting edge. So, I needed a Linux box. If you are on Windows, this is a solution for you as well. I setup a Ubuntu VM to install the latest HHVM. I could just download a 64 bit ISO and install inside VirtualBox but I preferred Vagrant since it’s just easier to setup and use. I am going to walk you through the setup now.
Step – 1: Get Vagrant
Head over to: http://www.vagrantup.com/ and follow the instructions for your OS. It is quite simple. You would need to have VirtualBox installed for Vagrant to work.
Step – 2: Installing and launching the Ubuntu VM
Create a directory somewhere and cd into it.
|
mkdir linux-vm cd linux-vm |
Let’s create a vagrant configuration file:
This would create a file named “Vagrantfile” which is a configuration file written in Ruby.
Let’s add a box, a box file is basically a VM image and configuration packed in one file. We shall download one of the default box, Ubuntu 12.04 64bit which we would call “precise64”. “Precise Pangolin” was the nickname of Ubuntu 12.04 if you didn’t know. And please note HHVM pre built packages are available for Ubuntu 12.04 64bit, so we can’t go for 32bit here.
|
vagrant box add precise64 http://files.vagrantup.com/precise64.box |
(PS: If you have a bad internet connection, download the file using a download manager and then use relative path like: “vagrant box add precise64 ~/Downloads/precise64.box” – that would work as well 😀 )
Now let’s make sure that we told Vagrant to use the newly created “precise64” box as the base of our VM. My Vagrantfile roughly looks like this:
|
# Vagrantfile API/syntax version. Don't touch unless you know what you're doing! VAGRANTFILE_API_VERSION = "2" Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| # Every Vagrant virtual environment requires a box to build off of. config.vm.box = "precise64" end |
Now, let’s get the VM up:
This should setup the Ubuntu VM and we’re ready for installing the HHVM now 😀
Step – 3: Setting up HHVM
SSH into the box:
You shall be SSH’d into the linux terminal. So you can play around 🙂
Now, we are going to install HHVM from the official repo. Type the following commands one after one to install. Explanation of the commands are available in comments:
|
# Update repositories sudo apt-get update # We need these packages for the "add-apt-repository" command sudo apt-get install software-properties-common python-software-properties # Add a custom repo for the "boost" library sudo add-apt-repository ppa:mapnik/boost # Grab the gpg key for their official repo wget -O - http://dl.hhvm.com/conf/hhvm.gpg.key | sudo apt-key add - # Add HHVM repo to our sources list echo deb http://dl.hhvm.com/ubuntu precise main | sudo tee /etc/apt/sources.list.d/hhvm.list # Update the repositories to include packages from the newly added repos sudo apt-get update # Do I have to tell you what this one does? :D sudo apt-get install hhvm |
If everything goes right, we should have a working HHVM installation!
(The above instructions to setup HHVM on Ubuntu 12.04 were taken and very slightly modified from: https://github.com/facebook/hhvm/wiki/Prebuilt-packages-on-ubuntu-12.04)
Step – 4: Saying Hello!
First, let’s see if HHVM was installed correctly.
|
vagrant@precise64:~$ php -v HipHop VM 2.4.2 (rel) Compiler: tags/HHVM-2.4.2-0-g432ecffa04b21c60953bb236a9db8278f4650537 Repo schema: 1be260b29a71097b5d1f78c6e4dcbb981ba03bde vagrant@precise64:~$ |
So it was installed correctly.
Let’s try some Hack:
|
<?hh function getHelloString(?string $name): string { return "Hello ". $name . "! ". PHP_EOL; } echo getHelloString("Masnun"); |
Output:
|
vagrant@precise64:/vagrant$ php hack.php Hello Masnun! vagrant@precise64:/vagrant$ |
Now, feel free to try out HHVM and Hack along with the many cool things they can do.
Some useful resources:
A sample site: https://github.com/hhvm/hack-example-site
The PHP Manual with Hack references: http://docs.hhvm.com/
Have something to say? Please leave your feedback in the comments section.