Using Composer with HHVM on Mac OS X

What is HHVM?

HHVM stands for HipHop Virtual Machine, the Facebook PHP execution engine. It was originally created to save resources on Facebook servers and has proven to increase PHP execution speeds significantly.

Why Composer on HHVM?

Due to its ability to improve the execution speed of PHP code, HHVM can also greatly increase the execution speed of Composer. This is because Composer is a PHAR file, or PHP Archive. Composer can feel quite slow at times and originally I thought this was due to networking speeds and how quickly packages could be pulled down from GitHub, but the simple comparison below proves that’s not the case.

Clocking Composer with and without HHVM

I’m clocking a `composer update` on a project I am currently working on. There were no new dependencies added so all Composer is doing is checking dependencies and building the updated autoload.php file. Network latency should have very little to no effect on the outcome.

Composer with PHP:

$ time php /usr/local/bin/composer update
real   0m54.441s
user   0m51.084s
sys    0m0.650s

Composer on HHVM:

$ time hhvm /usr/local/bin/composer update
real   0m15.132s
user   0m13.072s
sys    0m0.393s

The results speak for themselves. HHVM took only 28% of the time as compared to the PHP engine.

Installing HHVM on Mac OS X

I believe the best way to install HHVM on Mac is with Homebrew. In case you don’t have it installed yet, let’s start there.

Install Homebrew

$ ruby -e "$(curl -fsSL"

Add additional required repositories to Homebrew for HHVM

$ brew tap homebrew/dupes
$ brew tap homebrew/versions
$ brew tap mcuadros/homebrew-hhvm

Install MySQL

Due to a bit of an issue with dependencies as of this writing I had to install MySQL before installing HHVM. You may not have this issue depending how long it takes to be repaired, but I suggest just installing MySQL up front just to be safe.

$ brew install mysql

Install HHVM

This is a long process (45+ minutes) and will take time to compile the various dependencies as well as HHVM itself. Grab a cup while you wait.

$ brew install hhvm

Default to running Composer with HHVM

While there’s a lot you can do with HHVM in the big picture, using it to run Composer is a great place to start. The best way to do this is to create an alias in your shell. Place the following in your .bash_profile to ensure it autoloads when Terminal starts.

$ alias composer='hhvm /usr/local/bin/composer'

The example above assumes you have installed Composer globally. If you have not, you will need to adjust your alias to something like below and run composer on the same directory level as your composer.phar file.

$ alias composer='hhvm composer.phar'

And that’s it! I hope this is helpful to you and if you find a better way please share in the comments.