How to change current PHP version on Ubuntu

Change current PHP version on Ubuntu
To list all the installed PHP versions on your local device.

 

When working on Ubuntu, often you might need to switch between different PHP versions depending on the project requirements. 
Some applications run best on the latest PHP release, while others require an older version for compatibility. 

In this guide, we’ll go step by step through changing your PHP version on Ubuntu so you can easily switch between projects without breaking your setup.

Supported on listed versions of Ubuntu: 16.04, 18.04, 20.04, 22.04,

How to check if there are multiple versions of PHP installed
sudo update-alternatives --list php

This command will output all available PHP versions on this system.

Example:
/usr/bin/php7.4
/usr/bin/php8.0
/usr/bin/php8.1
/usr/bin/php8.2
/usr/bin/php8.3
/usr/bin/php8.4

To change active PHP version, Interactive mode
sudo update-alternatives --config php

# Related PHP applications
sudo update-alternatives --config phar
sudo update-alternatives --config phar.phar

To change active PHP version and list all the installed PHP versions on your local device.

To change active PHP version, Manual mode
sudo update-alternatives --set php /usr/bin/php8.3

# Related PHP applications
sudo update-alternatives --set phar /usr/bin/phar8.3
sudo update-alternatives --set phar.phar /usr/bin/phar.phar8.3

In this example, we are changing current PHP version to PHP 8.3

To change your currently used PHP version to a different version (that is installed), you can use the following command:
update-alternatives --set php /usr/bin/php{version}

{version} - the PHP version you want to change to.

 

Conclusion


Switching PHP versions in Ubuntu is a straightforward way to keep your development environment flexible and compatible with different projects. 
By installing multiple PHP versions and configuring which one is active, you can smoothly transition between old and new applications without issues. 

With just a few commands, you’ll have better control over your setup and ensure your projects run in the right environment.

Tags