How to switch the php version on ubuntu
As a PHP developer, you will probably face situations where you need to regularly switch between multiple PHP versions. For instance, your current project is running on PHP 8.0 but an old project you have to maintain is running on PHP 7.4.
In this article, I will show you how to switch you PHP version on a Debian based system.
I'll assume you already have PHP installed on your machine. If it's not the case, please read this article before :
Let's jump into it 👍
By doing a
php -v, you can see that my current php version is 7.4. This command returns the current PHP version used by the terminal.
PHP 7.4.3 (cli) (built: Jul 5 2021 15:13:35) ( NTS ) Copyright (c) The PHP Group Zend Engine v3.4.0, Copyright (c) Zend Technologies with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies
To retrieve the PHP version used by apache, you can use the
phpinfo() function natively available in PHP.
Switching from one version to another
Let's assume that you're current PHP version is 7.4 and you want to use PHP 8.0. There are two things you will need to do :
- Switch the PHP version on your Apache configuration,
- Switch the PHP version on your cli in order to use PHP commands with the right version.
Indeed, the PHP version used by you terminal and apache are two different things.
Only three lines are needed to change the PHP version on your apache configuration. In order to do it, you will need to use the
sudo command to run these commands with admin rights.
sudo a2dismod php7.4 sudo a2enmod php8.0 sudo service apache2 restart
a2dismodis used to disable a specific apache module. In this case, we will disable the previous PHP version which is php7.4.
a2enmod, in the other hand, is used to enable a module. Because you want to enable the version 8.0, you need to enable it.
service apache2 restartwill be used to restart your apache web server.
These three lines are the only things you need to do to enable a new php version on your apache configuration.
But now, the only thing that remains is the php version used by the terminal.
Switching the PHP version for your terminal is slightly different. You will need to update the symbolic link for each php file in the
/usr/bin folder. But don't worry, it's not that hard.
The simplest way is to use the
update-alternatives command to link the
php symbolic link file to the version 8.0 of PHP.
sudo update-alternatives --set php /usr/bin/php8.0
This command should work by itself but if you want to fully update your PHP version, you need to execute other commands.
sudo update-alternatives --set phar /usr/bin/phar8.0 sudo update-alternatives --set phar.phar /usr/bin/phar.phar8.0
These commands will respectively update the symbolic link for the
Everything should be up and running. You will be able to use the version 8.0 of PHP instead the 7.4.
After the whole process, you can see that my PHP version changed from 7.4.3 to 8.0.8 for both the terminal and apache.
PHP 8.0.8 (cli) (built: Jul 1 2021 15:26:46) ( NTS ) Copyright (c) The PHP Group Zend Engine v4.0.8, Copyright (c) Zend Technologies with Zend OPcache v8.0.8, Copyright (c), by Zend Technologies
If you want to re-enable a previous version, just invert the versions.
- 8.0 → 7.4
- 7.4 → 8.0
This manipulation can be applied to any other php versions.
Hope this helped you 👍 on your development journey.
Cover by NordWood Themes