Managing Multiple PHP Versions on Ubuntu Using APT
Introduction
Managing multiple PHP versions is essential for developers who work on different projects requiring different PHP versions. Ubuntu provides a way to install and switch between multiple PHP versions using the APT package manager and the update-alternatives
command. This guide will help you list, install, and switch between PHP versions efficiently.
Listing Available PHP Versions
To check the available PHP versions that can be installed on your system using APT, run the following command:
$ apt list php -a
If the desired version is available, install it using:
$ apt install php8.0 -y
Checking Installed PHP Versions
If multiple PHP versions are already installed, you can check which version is currently in use by running:
$ update-alternatives –display php
Example Output:

Switching Between PHP Versions
To change the active PHP version, use the following command:
$ sudo update-alternatives –set php /usr/bin/php8.0
This command sets PHP 8.0 as the default version to use.
Verifying the Active PHP Version
After switching, confirm the currently active PHP version by running:
$ php -v
This should display the PHP version currently in use.
Conclusion
By following these steps, you can effectively manage multiple PHP versions on your Ubuntu system. This method allows for flexibility in development environments, ensuring that you can work with different PHP versions as needed. Keeping track of installed versions and switching between them ensures smooth development workflows and compatibility across various projects.