Running php 5 and php 7 on same environment (Ubuntu)

Why we need php 5 and php 7 on same PC?

Recent releases of Magento 2 mentioned that they have officially started to support php 7 and they are heading more towards it. Many of us including myself working on several projects which includes Magento v1 and v2. So time to time we are required to switch between projects. The question arises when we try to switch from Magento 1 project to Magento 2. Some functions of the Magento 1 doesn’t work properly or gives errors when running on php 7. So it’ll be convenient to run Magento 1 with php 5. Magento 2 could run on php 5.6 however if we are able to run it on php 7, Then that would be great. This solution comes in handy when addressing both these problems.

There are some issues when using this via CLI and that is also addressed in one of the below sections.


Here we use third party PPA to get php 5 and php 7. This will allow us to install and run both php versions 5.6 and 7 including their extensions. Here what this gives is two apache modules which allows to load and run your preferred php version as a module. But please note that this doesn’t allow you to run both php versions in parallel.

To add the PPA to your system:

sudo add-apt-repository ppa:ondrej/php

Then to get information about this PPA:

sudo apt-get update

If you already have a system which runs on php 5 this suppose to work without any alterations. Simply run,

sudo apt-get install php5.6 php7.0

If there are issues or conflicts resulted from the previous step, then remove all the php5 packages or if you have php 7 installed, remove all of them. It is always wise to backup your important configurations before performing this.

sudo apt-get -y purge php.*

Once done re-run the install command stated in previous step.

Configuring and switching versions

The config files of the newly installed reside in /etc/php/5.6 and /etc/php/7.0. These locations have the configurations and extensions belongs to each version.

To switch from php 5.6 to php 7.0

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

To switch from php7.0 to php 5.6

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

Installing extensions

This works well with standard php extensions installation procedure. However if you want to install a extension to a particular version then you are required to mention the version when installing the extension.

E.g. Imagine you want to install php gd and curl to your php 5.6

sudo apt-get install php5.6-gd php5.6-curl

Follow the same if you want to install modules for php 7.0

sudo apt-get install php7.0-gd php7.0-curl

Installed extensions can be checked with:

php -m

php -v result in CLI??

You got what you’ve searching for and you probably left early before the conclusion of this article and had terrible time figuring out “Why I am getting same result when php -v is executed?” 🙂

The issue is when you are switching modules via apache module enable/disable commands that doesn’t change the php executable path used by CLI.

Run these commands to see what’s happening behind.

which php

This gives you which instance is called when you use php in cli. Result I got is /usr/bin/php. This is actually a symlink points to the file /etc/alternatives/php.

ll /usr/bin/php

Which is again points to either /usr/bin/php7.0 or /usr/bin/php5.

ll /etc/alternatives/php

In order to use particular version of php you have to either use version specific command E.g. to use php5.6 in command line instead of php you’ll have to use php5.6

or else you’ll have to modify the symlinks to /etc/alternatives/php.

E.g. to use php5 as php in cli.

sudo ln -sfn /usr/bin/php5 /etc/alternatives/php

In my dev environment I use bash aliases (refer link under resources section) to switch php versions using below entries. Commands I am using to switch

php 5 to php 7

alias sphp7='sudo a2dismod php5 && sudo a2enmod php7.0 && sudo ln -sfn /usr/bin/php7.0 /etc/alternatives/php && sudo service apache2 restart && php -v'


php 7 to php 5

alias sphp5='sudo a2enmod php5 && sudo a2dismod php7.0 && sudo ln -sfn /usr/bin/php5 /etc/alternatives/php && sudo service apache2 restart && php -v'



Leave a Reply

  • (will not be published)