Install PHP 5.6 on Ubuntu
Use the following set of commands to enable PPA for PHP 5.6 in your Ubuntu system and install PHP 5.6.
sudo apt-get install python-software-properties sudo add-apt-repository ppa:ondrej/php sudo apt-get update sudo apt-get install -y php5.6
Check Installed PHP Version:
php -v PHP 5.6.36-1+ubuntu16.04.1+deb.sury.org+1 (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
Install PHP 7.2 on Ubuntu
Use the following set of command to add PPA for PHP 7.2 in your Ubuntu system and install PHP 7.2
sudo apt-get install python-software-properties sudo add-apt-repository ppa:ondrej/php sudo apt-get update sudo apt-get install -y php7.2
Check Installed PHP Version:
php -v PHP 7.2.7-1+ubuntu16.04.1+deb.sury.org+1 (cli) (built: Jun 22 2018 08:44:50) ( NTS ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies with Zend OPcache v7.2.7-1+ubuntu16.04.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies
Switch Between PHP Version’s
If you have installed multiple PHP versions and want to use differently than the default. Use following steps to switch between php5.6 and php7.2 version. You can use the same command for other PHP versions.
From PHP 5.6 => PHP 7.2
Apache:- sudo a2dismod php5.6 sudo a2enmod php7.2 sudo service apache2 restart CLI:- sudo update-alternatives --set php /usr/bin/php7.2 sudo update-alternatives --set phpize /usr/bin/phpize7.2 sudo update-alternatives --set php-config /usr/bin/php-config7.2
From PHP 7.2 => PHP 5.6
Apache:- sudo a2dismod php7.2 sudo a2enmod php5.6 sudo service apache2 restart CLI:- sudo update-alternatives --set php /usr/bin/php5.6 sudo update-alternatives --set phpize /usr/bin/phpize5.6 sudo update-alternatives --set php-config /usr/bin/php-config5.6