How to Install and Use Multiple Versions of PHP

May 10th, 2020 by Rijad Husic

PHP is a popular general-purpose scripting language that works well for web development. Love it or hate it PHP is here to stay, around 30% of the web is made with it. So it’s safe to say PHP is not going anywhere soon.

Different projects can require different versions of php, so it’s convenient to be able to have multiple php versions and to be able to quickly switch between them.

In this post, I’ll go through the process of installing multiple versions of php and a way to quickly switch between them on ubuntu.

Also, I’ll cover how to install various php extensions for both Apache and Nginx using a Ondřej Surý PPA.

To check what’s the default stable version of php supported in the Ubuntu software repositories you can run the following apt command.

$ sudo apt show php

To install the default PHP version you can simply run:

$ sudo apt install php

Installing PHP 5.6, 7.0, 7.1, 7.2, 7.3, 7.4 on Ubuntu

Install the PPA:

$ sudo add-apt-repository ppa:ondrej/php

Update the system

$ sudo apt-get update

Now we install different versions of PHP:

For Apache Web Server

Using the following commands you can install different versions of PHP for Apache

$ sudo apt install php5.6 [PHP 5.6]
$ sudo apt install php7.0 [PHP 7.0]
$ sudo apt install php7.1 [PHP 7.1]
$ sudo apt install php7.2 [PHP 7.2]
$ sudo apt install php7.3 [PHP 7.3]
$ sudo apt install php7.4 [PHP 7.4]

For Nginx

And the following commands for Nginx

$ sudo apt install php5.6-fpm [PHP 5.6]
$ sudo apt install php7.0-fpm [PHP 7.0]
$ sudo apt install php7.1-fpm [PHP 7.1]
$ sudo apt install php7.2-fpm [PHP 7.2]
$ sudo apt install php7.3-fpm [PHP 7.3]
$ sudo apt install php7.4-fpm [PHP 7.4]

Install PHP Modules

To install any PHP module, simply type the version of the PHP and use the autocomplete to view all available modules

------------ press Tab key for auto-completion ------------
$ sudo apt install php5.6 
$ sudo apt install php7.0 
$ sudo apt install php7.1 
$ sudo apt install php7.2 
$ sudo apt install php7.3 
$ sudo apt install php7.4

Install the most required PHP modules (optional)

You can install the most required PHP modules

------------ Install PHP Modules ------------ 
$ sudo apt install php5.6-cli php5.6-xml php5.6-mysql 
$ sudo apt install php7.0-cli php7.0-xml php7.0-mysql 
$ sudo apt install php7.1-cli php7.1-xml php7.1-mysql 
$ sudo apt install php7.2-cli php7.2-xml php7.2-mysql 
$ sudo apt install php7.3-cli php7.3-xml php7.3-mysql 
$ sudo apt install php7.4-cli php7.4-xml php7.4-mysql

And now to verify your currently active PHP version on your system just run

$ php -v

How to switch between the PHP versions

You can set the default version of PHP using the update-alternatives command

------------ Set Default PHP Version 5.6 ------------ 
$ sudo update-alternatives --set php /usr/bin/php5.6 
------------ Set Default PHP Version 7.0 ------------ 
$ sudo update-alternatives --set php /usr/bin/php7.0 
------------ Set Default PHP Version 7.1 ------------
$ sudo update-alternatives --set php /usr/bin/php7.1 
------------ Set Default PHP Version 7.2 ------------ 
$ sudo update-alternatives --set php /usr/bin/php7.2 
------------ Set Default PHP Version 7.3 ------------ 
$ sudo update-alternatives --set php /usr/bin/php7.3 
------------ Set Default PHP Version 7.4 ------------ 
$ sudo update-alternatives --set php /usr/bin/php7.4

One little trick you can use if you have zsh set up is to create aliases

Simply edit your .zshrc

$ vi ~/.zshrc

(you can use nano instead)

Go to the end of the configuration and add the following

alias @php5.6="sudo update-alternatives --set php /usr/bin/php5.6" 
alias @php7.0="sudo update-alternatives --set php /usr/bin/php7.0" 
alias @php7.1="sudo update-alternatives --set php /usr/bin/php7.1" 
alias @php7.2="sudo update-alternatives --set php /usr/bin/php7.2" 
alias @php7.3="sudo update-alternatives --set php /usr/bin/php7.3" 
alias @php7.4="sudo update-alternatives --set php /usr/bin/php7.4"

Now if you want to set let’s say php7.4 as you currently default PHP you can simply run @php7.4

After changing versions to find your PHP config file you just need to run

$ php -i | grep "Loaded Configuration File"

To set the PHP version that will work with Apache you first need to disable the current version and enable the desired one. To accomplish that we use a2dismod and a2enmod commands as follows

$ sudo a2dismod php5.6 
$ sudo a2enmod php7.1 
$ sudo systemctl restart apache2