Sometimes PHP developers need to work on multiple versions of PHP for different projects and one might think of using a docker installation, which is a better idea of what we are going to do below, but Docker can be complicated for many reasons and one might not be ready to get started with Docker. In the below article, I will be explaining how one can work with multiple PHP version on Ubuntu. I am using ubuntu 18.10 and the below article for me without any issues.

Install PHP (5.6, 7.0, 7.1, 7.2 and 7.3) on Ubuntu Using PPA

One might wonder why 5.6! but we have clients who are still using outdated versions of PHP and upgrading to latest PHP might cost thousands/millions of dollars as the application developed using that version are massive and upgrading is just not an option as of now!

1. Adding Ondřej PPA to install different versions of PHP – PHP 5.6PHP 7.0, PHP 7.1, PHP 7.2 and PHP 7.3 on the system.

sudo apt-get install software-properties-common
## sudo apt install python-software-properties 
sudo add-apt-repository ppa:ondrej/php

2. Update the system

Make sure you have the sudo privileges to do so.

sudo apt-get update

3. Add different versions of PHP as required

I will be adding fpm version as well, you may ignore them if you do not need them

sudo apt install php5.6 php5.6-fpm
sudo apt install php7.0 php7.0-fpm
sudo apt install php7.1 php7.1-fpm
sudo apt install php7.2 php7.2-fpm
sudo apt install php7.3 php7.3-fpm

4. You might need to add the most common modules one normally uses with PHP

I work a lot with Laravel, WordPress, Symfony, EspoCRM & Zend Framework so I shall add the below modules for all the PHP versions

For PHP 5.6

sudo apt install php-gmagick php5.6-xml php-ssh2 php5.6-curl php5.6-gd php5.6-mbstring php5.6-mysql php-tokenizer php-xdebug php-apcu php5.6-bcmath php5.6-bz2 php-memcache php-zip

For PHP 7.0

sudo apt install php-gmagick php7.0-xml php-ssh2 php7.0-curl php7.0-gd php7.0-mbstring php7.0-mysql php-xdebug php-apcu php7.0-bcmath php7.0-bz2 php-memcache php7.0-zip

For PHP 7.1

sudo apt install php-gmagick php7.1-xml php-ssh2 php7.1-curl php7.1-gd php7.1-mbstring php7.1-mysql php-xdebug php-apcu php7.1-bcmath php7.1-bz2 php-memcache php7.1-zip

For PHP 7.2

sudo apt install php-gmagick php7.2-xml php-ssh2 php7.2-curl php7.2-gd php7.2-mbstring php7.2-mysql php-xdebug php-apcu php7.2-bcmath php7.2-bz2 php-memcache php7.2-zip

For PHP 7.3

sudo apt install php-gmagick php7.3-xml php-ssh2 php7.3-curl php7.3-gd php7.3-mbstring php7.3-mysql php-xdebug php-apcu php7.3-bcmath php7.3-bz2 php-memcache php7.3-zip

Well the above should be more than enough to get you started with your PHP projects.

5. Check the installed versions of PHP

execute the following command to check all the versions of PHP installed on the system:

ls -al /usr/bin | grep php

--------------------------------- output -------------------------------------

lrwxrwxrwx  1 root root          21 Mar 26  2020 php -> /etc/alternatives/php*
-rwxr-xr-x  1 root root     4512600 Jan 11 19:12 php5.6*
-rwxr-xr-x  1 root root     4508520 Dec  7 13:58 php7.0*
-rwxr-xr-x  1 root root     4665176 Jan 11 19:44 php7.1*
-rwxr-xr-x  1 root root     5014552 Feb  8 21:08 php7.2*
-rwxr-xr-x  1 root root     4769528 Feb  8 21:14 php7.3*

To check your current version of PHP execute the following command:

php -v

It should output something as below:

PHP 7.3.2-3+ubuntu18.10.1+deb.sury.org+1 (cli) (built: Feb  8 2019 15:44:30) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.2, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.3.2-3+ubuntu18.10.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies
    with Xdebug v2.7.0RC2, Copyright (c) 2002-2019, by Derick Rethans

Compiling extensions or working with PHP Dev tools?

If you are planning to compile shared PECL extensions install the PHP dev tools for respective version

sudo apt install php5.6-dev
sudo apt install php7.0-dev
sudo apt install php7.1-dev
sudo apt install php7.2-dev
sudo apt install php7.3-dev

6. Changing PHP version

One may use the command `update-alternatives` to update the default PHP version

For changing PHP version to 5.6 execute the following command

sudo update-alternatives --set phpize /usr/bin/phpize5.6
sudo update-alternatives --set php /usr/bin/php5.6
sudo update-alternatives --set php-config /usr/bin/php-config5.6

For changing to PHP 7.0:

sudo update-alternatives --set phpize /usr/bin/phpize7.0
sudo update-alternatives --set php /usr/bin/php7.0
sudo update-alternatives --set php-config /usr/bin/php-config7.0

For changing to PHP 7.1:

sudo update-alternatives --set phpize /usr/bin/phpize7.1
sudo update-alternatives --set php /usr/bin/php7.1
sudo update-alternatives --set php-config /usr/bin/php-config7.1

For changing to PHP 7.2:

sudo update-alternatives --set phpize /usr/bin/phpize7.2
sudo update-alternatives --set php /usr/bin/php7.2
sudo update-alternatives --set php-config /usr/bin/php-config7.2

For changing to PHP 7.3:

sudo update-alternatives --set phpize /usr/bin/phpize7.3
sudo update-alternatives --set php /usr/bin/php7.3
sudo update-alternatives --set php-config /usr/bin/php-config7.3

Simple! Right?

For PHP professionals working on multiple frameworks, using multiple versions of PHP becomes inevitable. Hope the above content help others as it is was helpful for me.

References: https://www.tecmint.com/install-different-php-versions-in-ubuntu/