In this article, we will be updating and downgrading PHP versions on Ubuntu 20.04.
How to upgrade and downgrade PHP versions on Ubuntu 20.04.
Unlike CentOS 7, changing the PHP version on Ubuntu does not require its removal. This is because when Ubuntu adds PHP to its list, the system also updates the file of alternative versions, which we will mention later in the article. We will also discuss a method to completely remove PHP. Warning: Removing an old PHP version may affect the functionality of phpMyAdmin and other programs that require a specific PHP version.
Preparation
1. Update repository lists with the command:
apt-get update
It is essential to update because failing to do so may result in the system not finding the required package.
2. Install the repository manager:
apt install software-properties-common
Without it, adding a PHP repository won't work.
3. Add the PHP repository:
add-apt-repository ppa:ondrej/php
Press [ENTER] when prompted. After that, repeat the first step. Now, the system is prepared for PHP update.
Updating
To begin, find out the installed PHP version and modules:
1. Show installed PHP version:
php -v
2. Show installed modules for the selected PHP version:
php -m
Copy this list to know which modules are missing in the new version for the website or program to work.
Options for PHP Version Update
1. Update to the latest PHP version:
Remove the old PHP version completely:
apt-get remove php5.6*
Warning: This command may also remove Apache2, which is part of the PHP dependencies on Ubuntu, causing websites to stop working. Then, install the new version:
apt-get install php
Install missing modules separately:
apt-get install php-module1 php-module2 .....
As of writing, the latest PHP version is 8.2. After installation, check with:
php -v
1.2. Alternatively, use:
apt upgrade php
But it only updates the basic version without modules. To fully update the version:
apt upgrade php*
It won't work due to package conflicts. Manually install missing modules:
apt-get install php-module1 php-module2 .....
2. Install a specific PHP version:
Remove the old version:
apt-get remove php5.6*
To install a specific version, use:
apt-get install php7.4 php7.4-module1 php7.4-module2...
Where 7.4 is the PHP version.
Downgrading
The principle is similar to updating:
Remove the old version:
apt-get remove php7.4*
Install the new version:
apt-get install php5.6 php5.6-module1 php5.6-module2...
Where 5.6 is the PHP version.
If you need multiple PHP versions or the safest way to switch PHP versions:
Install two PHP versions at once:
apt-get install php5.6 && apt-get install php7.4
To switch between versions, use:
update-alternatives --config php
In this list, choose the desired version by entering the corresponding number (e.g., 1 = php5.6, 2 = php7.4...). To check versions, use:
php -v
If using Apache, disable/enable PHP versions:
a2dismod php5.6
a2enmod php7.4
After changing PHP versions, always restart Apache:
systemctl restart apache2 or service apache2 restart
Conclusion:
The asterisk (*) in the command means it searches for all matches in package names, so avoid using:
apt-get remove php*
This would not only remove PHP but also phpMyAdmin and other packages whose names start with PHP. The double ampersand (&&) is used to execute commands sequentially:
```
Command1 && Command2 and so on.
```
We also suggest you other useful articles: