How to switch the php version on ubuntu

July 16, 2021 MrAnyx 4 min de lecture

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.

Because it is PHP, the development environment is not as modern as other programming languages such as Javascript. Indeed, javascript, especially NodeJS, has a tool called NVM which stands for Node Version Manager which is a great tool if you have to use multiple NodeJS versions. But PHP doesn't have such a tool so, in most cases, you'll need to do it yourselves.

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 👍

Current version

Terminal configuration

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.

Apache configuration

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
  • a2dismod is 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 restart will 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.

Terminal configuration

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 phar and phar.phar files.

Everything should be up and running. You will be able to use the version 8.0 of PHP instead the 7.4.

Final result

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

Other informations

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


Cette œuvre est mise à disposition selon les termes de la licence Licence Creative Commons