Last Updated: July 12, 2018
· 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?