In this tutorial, I am going to show you how to easily switch between different php versions in Ubuntu 22.04.
When working on software that will be used on different systems, it is of utmost importance to check backwards compatibility with servers that run different versions of php. At the time of writing, the most popular php version is 7.4. However, it is going to be obsolete in the end of 2023. This means that all new software needs to be carefully tested with more recent versions of php, e.g. php8, php8.1 and php8.2. At the same time, due to many different kinds of reasons, some users are stuck with older versions of php, e.g. php7.0, php7.1 and even php5.6. Being a WordPress theme and plugin contributor, for me is very important not to leave users behind and my team and I always tries to create products with solid backwards compatibility. It might be an extra effort but in most cases it would be worth it, unless of course it is too much of a hassle.
Switching between different php versions
I will presume that you are administering LAMP server and the server is up and running. Before switching php versions, make sure you have the required versions installed by running the following command in the terminal:
sudo update-alternatives --list php
Install missing php versions
Skip this step if the above command shows that you have the php version you are planning to switch to.
Lets imagine you wanted to downgrade to php7.2. If you see that the php version you want to switch to is missing, install it by running:
sudo apt install php7.2
Replace php7.2 with the target php version you want to up/downgrade to. You will most probably need additional modules. If you are using WordPress, you will most certainly need the following php modules:
sudo apt install php7.2-common php7.2-cli php7.2-json php7.2-xml php7.2-opcache php7.2-mysql php7.2-mbstring php7.2-zip php7.2-fpm
Switch to the Desired PHP Version
update-alternatives command to set the PHP version you want as the default. Replace 7.2 with the version you want (e.g., 8.0):
sudo update-alternatives --set php /usr/bin/php7.2
We are far from ready yet, lets execute a few extra commands:
sudo update-alternatives --set phar /usr/bin/phar7.2
sudo update-alternatives --set phar.phar /usr/bin/phar.phar7.2
sudo update-alternatives --set phpize /usr/bin/phpize7.2
sudo update-alternatives --set php-config /usr/bin/php-config7.2
Finally, in the context of the LAMP stack, you would need to update the Apache web server:
sudo service apache2 restart
Now verify the change by running the following command:
This works in most cases but sometimes it might not enough to change the php version on LAMP stack.
In order to be absolutely sure that you have successfully changed the php version, I would recommend creating a phpinfo.php file and adding the code:
<?php phpinfo(); inside it. Place the file in the root folder of your server and reach to it the by running https://mydomain.com/phpinfo.php in the browser. (Don’t forget to remove this file immediately after the test, as it poses a security risk). If it shows php.7.2, then you have successfully updated the php version and you don’t need to read this article anymore.
In some cases, the above commands are not enough to change to the desired php version. This sometimes happens due to dependancy conflicts, web server configuration, update alternatives limitations and others. On a LAMP stack, I have found it a good practice to run these commands too:
sudo a2dismod php7.4
sudo a2enmod php7.2
sudo systemctl start php7.2-fpm
sudo systemctl enable php7.2-fpm
sudo a2disconf php7.4-fpm
sudo service apache2 restart
This batch of commands ensures not only swaping the php version but also proper integration with the Apache web server. Make sure you replace php7.4 with the current php version and php7.2 with the target php version.
Bonus: Create a Shell Script
We have covered a broad range of configuration aspects and dependencies in order to successfully change php to the desired version. Finally, I would recommend to create a shell script that runs one time instead of executing these commands one by one. Open Gedit or nano text editor and place the above commands in a file with .sh extension. After that, execute it by running it through the terminal:
We have learnt how to switch php versions on a LAMP server in Ubuntu. The process is not straightforward but hopefully these commands will be of help to you and your teammates and it will make the development process less of a hassle and more fun.