How to switch PHP version using ubuntu terminal?

In this article, we will see how to switch the PHP version for Apache and CLI using ubuntu terminal. For example, you have installed both PHP 5.6 and PHP 7.1 on your server. By default, PHP 7.1 is running for Apache and CLI. For any requirement, you need to use PHP 5.6. Then you don’t need to remove the PHP version 7.1 from your server. Instead, you can simply switch your PHP version to default which is to be used for Apache and command line on your server.

The below example will help you to switch the PHP version from 5.6 to 7.1 and 7.1 to 5.6.

Switch PHP version from 5.6 to PHP 7.1

By default, PHP 5.6 is set on your system and if you want to switch the PHP version to PHP 7.1, then run the following commands on your terminal.

For Apache:

sudo a2dismod php5.6
sudo a2enmod php7.1
sudo service apache2 restart

For Command Line:

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

Switch PHP version from 7.1 to PHP 5.6

By default, PHP 7.1 is set on your system and if you want to switch the PHP version to PHP 5.6, then run the following commands on your terminal.

For Apache:

sudo a2dismod php7.1
sudo a2enmod php5.6
sudo service apache2 restart

For Command Line:

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

Hope this helps you.

One Comment on “How to switch PHP version using ubuntu terminal?”

Leave a Reply

Your email address will not be published. Required fields are marked *