Running Multiple Versions of PHP
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:
Now we’re all set and we can compile the code and install it to the related location:
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:
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!
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!
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
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
Yes, the webserver is excellent, I wrote about including an example router.php file before: http://lornajane.net/posts/2012/php-5-4-built-in-webserver. Be very careful with the built in webserver; it serves requests in serial which is why it is not recommended (as in strongly recommended to be avoided) for production use.
Or just run my docker image: http://www.splitbrain.org/blog/2014-02/02-docker_phpfarm
You could also use phpbrew by c9s, which does a bulk of the above aswell as extensions etc.
You must use a tool like phpswitch (https://github.com/jubianchi/phpswitch) or php-build (https://github.com/CHH/php-build).
They are awesome to manage multiple PHP version on the same machine. It must be a developer machine but it seems to fit your needs…
If you are looking for a simple and straightforward way to use multiple PHP versions on Windows, feel free to try https://github.com/JanTvrdik/PhpVersionSwitcher.
Are you referring to installing the CLI version of PHP here, as opposed to the version that integrates with Apache?
Hi Lorna Jane, great article.
You can also use .htaccess to control the version of PHP you want your script to run.
Here is a post i have wrote about how to run multiple versions of PHP on Windows with Wamp Server. http://kbariotis.wordpress.com/2014/08/02/multiple-php-versions-on-wampserver/
The original phpfarm tool by Christian Weiske ( http://cweiske.de/tagebuch/Introducing%20phpfarm.htm ) is very good also. Just a few minutes ago I was able to quickly compile up a version of 5.3.28 (I know! :( ) and write up a quick linting tool around it:
for i in $*
/home/kguest/dev/phpfarm/src/php-5.3.28/sapi/cli/php -l $i
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.
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.