Last Updated: July 12, 2018
·
1.541K
· tanmayk

Switch between PHP versions on Ubuntu

Want to switch PHP version according to project need? Here are set of commands for Ubuntu:

Install PHP 5.6
sudo add-apt-repository ppa:ondrej/php

(If you get command not found error, try below first)
sudo apt-get install software-properties-common

sudo apt-get update
sudo apt-get install php5.6
sudo apt-get install php5.6-mbstring php5.6-mcrypt php5.6-mysql php5.6-xml
php -v

Switch PHP Version
sudo update-alternatives --config php

Use PHP5.6 for Apache
sudo a2dismod php7.x // Replace x with your php7 version.
sudo a2enmod php5.6
sudo service apache2 restart

Use PHP7.x for Apache
sudo a2dismod php5.6
sudo a2enmod php7.x // Replace x with your php7 version.
sudo service apache2 restart

Pretty neat, eh?

References
https://askubuntu.com/questions/756181/installing-php-5-6-on-xenial-16-04#answer-756186
https://tecadmin.net/switch-between-multiple-php-version-on-ubuntu/