How to have two versions of PHP installed and switch easily between them?

You can use a php version manager to achieve this. Different version managers are available like:

  • https://github.com/phpbrew/phpbrew
  • https://github.com/CHH/phpenv
  • https://github.com/wilmoore/php-version

My favorite is phpbrew. Hope this helps.


You can run 2 different PHP versions at once, but it's not as easy as just apt-getting them. You need to run one seperately installed version and serve it up according to the settings in your apache config.

You can do this using fastcgi for example: basically what you are looking for is the config you see on this page. You add a different handler in your config based on the situation/port/domain you need. The trick, after installing both versions, is this step:

===from that page==

  1. The last step was to create virtual hosts. In the end I have three files in /etc/apache2/sites-enabled: 000-default, php5.3.17 and php5.4.7 With the following contents

default:

    <VirtualHost *:80>
      ServerName localhost
      DocumentRoot /var/www
      <Directory "/var/www">
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
        AddHandler php-cgi .php
        Action php-cgi /php-fcgi/php5317.fcgi
      </Directory>
    </VirtualHost>

php5.3.17:

    <VirtualHost *:80>
      ServerName 5317.localhost
      DocumentRoot /var/www
      <Directory "/var/www">
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
        AddHandler php-cgi .php
        Action php-cgi /php-fcgi/php5317.fcgi
      </Directory>
    </VirtualHost>

php5.4.7:

    <VirtualHost *:80>
      ServerName 547.localhost
      DocumentRoot /var/www
      <Directory "/var/www">
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
        AddHandler php-cgi .php
        Action php-cgi /php-fcgi/php547.fcgi
      </Directory>
    </VirtualHost>

See for the complete installation the linked question. Don't forget to look at the two links in the header as well, they look like nice tutorials (but less compressed). This and That

(don't be put of by the fact that the linked question is a big one with no accepted answer. The method should (and does) work fine, but the user forgot to use <?php and used <? with shorttags off, see the comments)


I installed php 5.6 and 7.0 but besides all tips it kept running 5.6, so this command saves the day (in my case i chosse option 1 and restart apache):

sudo update-alternatives --config php

This one works for me: https://lornajane.net/posts/2016/php-7-0-and-5-6-on-ubuntu

Same as the script from "Growling Flea", but using new versions.

Add the PPA

The PHP 5.6 and PHP 7.0 packages are from a third party PPA, not provided by the official Ubuntu repositories from Canonical. The PPAs I'm recommending here are from Ondřej Surý who packages PHP for Debian (which is then used by Ubuntu) so while it's not an official repository, he's not exactly random! The PPA itself is here

To add the PPA to your setup:

sudo add-apt-repository ppa:ondrej/php  

Then we'll also want to grab information about what is on offer from this new PPA, so then run:

sudo apt-get update   

Install new PHP versions

I already had some of the php5 packages installed, but I didn't uninstall anything, I just let apt work out what it wanted to do when I asked it to install the new versions:

sudo apt-get install php5.6 php7.0

This resulted in a lot of complaining from apt and lots of conflicts. The first suggested resolution was to remove all the stock php5 packages so that PHP 5.6 could be installed - so I just accepted the first suggestion.

I use apache so this setup gave me apache with both php5.6 and php7.0 modules available, and the php5.6 module actually loaded.

As well as just the PHP itself, all the extensions and other tools you'd expect with PHP are there for both versions of PHP so it's very easy to add in the modules that you need. I was very, very impressed with how nicely this is done.

Configuring and switching versions

Now you have two completely separate versions of PHP installed on your system, so let's have a look at where all the pieces went!

The config files are all in /etc/php/5.6 and /etc/php/7.0 respectively - inside here is where you can configure which extensions are loaded, set the ini settings, and everything else for each version in isolation.

I'm an apache user, and as I mentioned both modules are available. So to switch from one to the other I need to do:

sudo a2dismod php5.6
sudo a2enmod php7.0
sudo service apache2 restart

For nginx users, the changes are almost as easy, Digital Ocean have good documentation on this (they do have great docs!) so check out their guide: https://www.digitalocean.com/community/tutorials/how-to-upgrade-to-php-7-on-ubuntu-14-04 as it includes a section on reconfiguring nginx to use another version of PHP.

From the command-line, I have both php5.6 and php7.0 available as commands. I also still have a php command - look in /etc/alternatives to see that it symlinks to a particular version of PHP cli*. You can also quickly check which yours is using by running php -v.

&ast; more specifically, run which php to see which version of PHP is being used - but this will probably point to /usr/bin/php, which for me is itself a symlink to the /etc/alternatives/php command.

Working with extensions

This PPA comes with the usual php-pear package which offers the pecl command for both versions of PHP, so any extensions that are available via PECL can be installed in the usual way. You will also need the relevant headers so either php5.6-dev or php7.0-dev should be installed.

When the pecl installation completes, you'll get a note to add the *.so file to your php.ini; in fact the best thing to do here is to look at what's in /etc/php/mods-available. There will be some modules already here, each in its own file named after the extension and ending in .ini. You can copy one to use as a template or create your own and put all the relevant configuration for the extension in it (as a minimum, you need extension=[extensionName].so).

Once the new extension is in mods available, enable and then check it by doing:

sudo phpenmod extension  
php -m  

This will create the symlinks in the right places for your current version of PHP to load this module, and you should see it in the list of modules output by the php -m. Pro tip: if you don't see it in the output, scroll all the way to the top of the output and see if there are any useful error messages there.