Running Multiple Versions of PHP

When I advise people about upgrading their PHP version, I say things like “just run your test suite with the new version” “just grab the new version and try your site with the built-in webserver”. A couple of people recently have asked for more detail on how to actually achieve these things so here’s a quick primer on getting new PHP without touching anything to do with your existing PHP installation.

Install Your New PHP Version

(These instructions are for *nix systems; I have literally no idea how it works on Windows)

You will want to download the source code of your desired version of PHP, then compile it. The key thing here is that we’ll put this version of PHP in a different location than your operating system would like to put PHP by default – and so we’ll avoid overwriting anything.

Go and choose your version from http://php.net/downloads.php. Download it into its own directory somewhere and extract the files.

Now we’ll configure the source, but here is the key ingredient: we set a prefix so that it’ll be installed into a separate directory and not interfere with anything. Make a command like this with your path in:


./configure --prefix=/path/to/toy/php

Now we’re all set and we can compile the code and install it to the related location:


make
make install

The make step builds the binaries and the make install step moves them to the correct location – so you may need to sudo make install if your user doesn’t have write permission to the location you set in the prefix earlier on.

You’re basically done :) Check everything is working by doing:


/path/to/toy/php/bin/php -v

This should show you what version of PHP you just built.

Checking Your Application With Your New PHP Version

Whatever you have in place for your usual test/build process, you can go through and have it use your /path/to/toy/php/bin/php everywhere it would usually run a php command. This can mean fiddling with environment variables or hardcoding paths in places while you try this out – so if you modify your tools make sure you’re making those changes on a branch in your source control tool.

To test out your code with the webserver, you just need to start it using the new PHP version. For me, that command looks like this:


/path/to/toy/php/bin/php -S localhost:8080

I am seeing more PHP projects being upgraded now, where once they would have been shipped onto a set version, deployed to the server, and left there until they needed replacing. Now we build applications, not just websites, and those need to grow and live along with the companies they exist to serve. The much smoother upgrade process in PHP as well as tools like the webserver make upgrading platforms a perfectly advisable thing to do and I hope the outline above helps someone to make that leap!

14 thoughts on “Running Multiple Versions of PHP

  1. It would be interesting to have a symlink in your path that goes sends “php” to the specific version you want, and create a few bash aliases that switch the symlink… rvm-ish!

  2. Why not using a Vagrant environment or something similar? Even though using a virtualized setup might be some overhead in the first place for learning and setup, it will pay off in the long term. Imagine a working virtual environment (e.g. Vagrant with Puppet or Chef), you just had to switch the version and here we go => 20 minutes later your full working stack is ready to be tested with the new or changed PHP version. No need for path switches, changed build steps or an adjusted shebang in some outdated CLI-PHP scripts ;-)

    • Usually because I’m actually testing extensions against various versions of PHP. So it’s not one more version of PHP, it’s grabbing a few versions and using them for a few different things, sometimes with different compile switches. Definitely +1 to virtualised environments for development and also for testing new versions – but in this case I was trying to illustrate something really specific

  3. Hi, feature with build in server is great. I use this the same way as You describe but and the end in command line, I add php script that is something like web server emulator.
    /path/to/toy/php/bin/php -S localhost:8080 router.php
    all requests are going through that script, if you are using nice url it’s needed. I’m using such script for my projects instead apache or nginx. One thing that must be done in such script is support for content type header for images, css or javascript files.

  4. Are you referring to installing the CLI version of PHP here, as opposed to the version that integrates with Apache?

  5. If you want to switch between versions on OSX I find it’s easy to do with just homebrew. If I want to switch to PHP 5.5 I type `brew link php55`, I want PHP 5.6 it’s … you guessed it … `brew link php56`. Very simple.

    I also use MAMP stack to switch between versions for the browser which is just “out of the box”. I will, however, being moving away from MAMP and toward NGINX/FPM but my command-line solution then should work fine. I think where it really becomes complicated is when you want to run multiple versions at the same time on the same machine. For instance, to have one app using 5.4 and another on 5.5. Hopefully this is an uncommon requirement for most but I imagine some people run into it and I don’t have any good answers for that.

  6. I have to confess that the idea of upgrading PHP to the next version fills me with equal parts geekish joy and technical dread. The more code I have created and need to maintain the more troubling new versions become. Just reading through the comments (and the above excellent article) has left me feeling a lot more confident that I should actually be playing with the newer versions already.

Leave a Reply

Please use [code] and [/code] around any source code you wish to share.

This site uses Akismet to reduce spam. Learn how your comment data is processed.