Ubuntu 22.04, the latest LTS (Long Term Support) release, brings several enhancements and updates to the world of Linux. If you’re a web developer or server administrator, you might need to work with different versions of PHP for various projects. Fortunately, Ubuntu 22.04 makes it relatively easy to change the PHP version on your system. In this step-by-step guide, we’ll walk you through the process of changing the PHP version on your Ubuntu 22.04 system.
1. Check the Current PHP Version
Before you change the PHP version, it’s essential to verify which version is currently installed on your system. Open your terminal and run the following command:
$ php -v
This command will display information about your current PHP version.
2. Add a PPA (Personal Package Archive)
To easily switch between PHP versions, we’ll use a PPA that provides different PHP versions. We’ll use the well-maintained
ondrej/php PPA for this purpose. Open your terminal and run the following commands to add the PPA to your system:
$ sudo add-apt-repository ppa:ondrej/php
You’ll need to confirm the addition of the repository. After adding the PPA, update your package list:
$ sudo apt update
3. Install the Desired PHP Version
Now that you’ve added the PPA, you can install the PHP version of your choice. You can install PHP 7.2, 7.4, 8.0, or any other available version. Use the following command to install a specific PHP version. Replace
x.x with the desired PHP version, e.g., 7.2, 7.4, or 8.0:
$ sudo apt install phpX.X
For example, to install PHP 7.4, you would run:
$ sudo apt install php7.4
4. Switch Between PHP Versions
Ubuntu 22.04 provides a handy tool called
update-alternatives to switch between different PHP versions. You can configure the default PHP version and choose which one you want to use.
Use the following command to set the default PHP version to the one you just installed (replace
x.x with the PHP version you installed):
$ sudo update-alternatives --set php /usr/bin/phpX.X
For example, if you installed PHP 7.4, you would run:
$ sudo update-alternatives --set php /usr/bin/php7.4
5. Verify the PHP Version
After changing the default PHP version, verify that the switch was successful. Run the
php -v command once again:
$ php -v
You should now see the updated PHP version displayed in your terminal.
6. Configure PHP Extensions
When you switch PHP versions, keep in mind that you might need to reinstall or configure PHP extensions for the specific version you’re using. Extensions are not always compatible between different PHP versions, so make sure to install and enable the necessary extensions for your projects.
Changing the PHP version on Ubuntu 22.04 is a straightforward process, thanks to the
ondrej/php PPA. Whether you’re developing web applications or hosting websites on your server, having the flexibility to work with different PHP versions is a valuable tool in your toolkit. By following the steps outlined in this guide, you can seamlessly switch between PHP versions, ensuring that your projects run smoothly and efficiently.