How To Change PHP Version From Command Line In Ubuntu

In this tutorial we will learn how to change the PHP version from command line in Ubuntu. Sometime we need to make some changes in the project or need to integrate new thing in the website which required upper PHP version to run. Let’s suppose you have PHP 5.6 installed in the server. what if you need to install and work on latest version of some PHP framework like Laravel. In that condition you only have one choice either setup new server or upgrade the existing one. So lets suppose you have two PHP versions installed on the server. For changing the version using command line you need to have SSH server access.

So today we will learn how to change the PHP version in server using command line. Suppose your system has PHP 7.4 and 8.0 both versions installed and you want to switch PHP version 7.4 to 8.0.

So let’s check the following example and see how we can achieve the same.

Run the below command, If you want to change PHP version for apache.

sudo a2dismod php7.4
sudo a2enmod php8.0
sudo service apache2 restart

Run the following command, If you want to change PHP version for command line.

sudo update-alternatives --set php /usr/bin/php8.0
sudo update-alternatives --set phar /usr/bin/phar8.0
sudo update-alternatives --set phar.phar /usr/bin/phar.phar8.0

Now you will see the PHP version has been changed and you can check the same by typing PHP -v in command line. If you want to change the version to other installed PHP version you can follow the above commands, you only need to specify the versions of the PHP in the command.

If you are using shared hosting, you can change the same by

  1. login into Cpanel
  2. Search for MultiPHP Manager
  3. Select domain in list
  4. Select PHP version from right side and apply.

So now you have learned how easy is to change PHP version from command line and cpanel both.