Switching Linux PHP version
Sometimes we have multiple PHP versions installed on the system with respect to the application’s requirements we are working such as PHP 5, PHP 7, PHP 8, and even more detailed versions like PHP 5.6, PHP 5.8, PHP 7.2, PHP 7.4, PHP 8.0 and so on. But switching between the required PHP version sometimes messes with the working code and other functionalities.
Here are some tricks that you can use to switch between your installed Linux PHP version in Ubuntu.
1- Using update-alternatives
First thing first, you have to check your PHP version using
php -v
Now, you have to check your other PHP version list by using this command:
sudo update-alternatives --config php
It will display all your installed PHP versions list, which looks like this:
There are 4 choices for the alternative php (providing /usr/bin/php). Selection Path Priority Status ------------------------------------------------------------ * 0 /usr/bin/php7.2 72 auto mode 1 /usr/bin/php5.8 58 manual mode 2 /usr/bin/php7.2 72 manual mode 3 /usr/bin/php7.4 74 manual mode 4 /usr/bin/php8.0 80 manual mode Press <enter> to keep the current choice[*], or type selection number:
Now, you simply just have to choose your desired PHP version to whichever version you need and press enter.
Don’t forget to reload/restart your apache server.
sudo service apache2 restart
Check your PHP version again using
php -v
You will get your selected required PHP version active.
2- Using Enable and Disbale mode
Check you running the PHP version like the above step and then use the following commands to switch your required PHP version:
sudo a2dismod php5.8 sudo a2enmod php7.4 sudo service apache2 restart
As you can see from the above-highlighted code, php7.4 is selected to enable and php5.8 has been set to disable. So, it will set the PHP 7.4 to your active PHP version. This enables and disable mode feature in Ubuntu Linux will not only switch to the desired PHP version but also enable and disable their respected services too.
To check more articles related to this topic, please check related posts here.