Most applications I work with using PHP 7.2 but I also have legacy projects that need PHP 5.6 still, so I'll install 5.6 alongside 7.2 and then set which one I want to use on the command line.
This post assumes PHP 7.2 is already installed
sudo apt install php7.2
To install 5.6 first run this command
sudo add-apt-repository ppa:ondrej/php
Now to install 5.6
sudo apt install php5.6
Now you should have 7.2 and 5.6 installed, to list all PHP versions installed:
sudo update-alternatives --config php
This menu lets you set which version to use:
sudo update-alternatives --config php
Which shows:
There are 2 choices for the alternative php (providing /usr/bin/php).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/bin/php7.2 72 auto mode
1 /usr/bin/php5.6 56 manual mode
2 /usr/bin/php7.2 72 manual mode
Press <enter> to keep the current choice[*], or type selection number:
In the above code, I have 7.2 as my active version to switch I would type 1 and press enter. Now checking the PHP version:
php -v
Returns:
PHP 5.6.38-1+ubuntu18.04.1+deb.sury.org+2 (cli)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies
Now I can easily switch back and forth.