How to Downgrade PHP on Ubuntu/Debian: Removing Current Version and Installing Older PHP Releases


3 views

When working with legacy applications or specific frameworks, you might need to downgrade PHP to an older version. The process involves carefully removing the current PHP installation and configuring the system to accept older packages.

First, identify all installed PHP packages:


sudo apt list --installed | grep php

Then remove them completely:


sudo apt-get purge php*
sudo apt-get autoremove
sudo apt-get autoclean

Many older PHP versions are available through Ondřej Surý's PPA:


sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update

For example, to install PHP 7.2:


sudo apt-get install php7.2 php7.2-common php7.2-cli php7.2-fpm

For PHP 5.6 (very old but sometimes needed):


sudo apt-get install php5.6 php5.6-common php5.6-cli php5.6-fpm

After installation, check the active version:


php -v

If you need multiple PHP versions, consider using update-alternatives:


sudo update-alternatives --config php

If you encounter dependency problems, try:


sudo apt-get install -f

For Apache users, remember to enable the correct module:


sudo a2enmod php7.2
sudo systemctl restart apache2

When you install PHP via apt install php on Ubuntu/Debian, you typically get the latest stable version from the default repositories. But what if your legacy application requires PHP 7.4 when the system offers 8.2?

# Current PHP version check
php -v
# Output might show: PHP 8.2.8 (cli)

First, we need to cleanly remove the existing installation and its dependencies:

sudo apt purge php* libapache2-mod-php* 
sudo apt autoremove
sudo rm -rf /etc/php

The most reliable way to get older PHP versions is through Ondřej Surý's PPA:

sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt update

Now you can install any supported version. For PHP 7.4 example:

sudo apt install php7.4 php7.4-cli php7.4-fpm php7.4-mysql php7.4-curl
sudo a2enconf php7.4-fpm
sudo systemctl restart apache2

Create a test file to confirm the version:

echo "" > /var/www/html/info.php

Then visit http://localhost/info.php in your browser. The headers should show your installed version.

For development environments, you might need multiple versions:

sudo update-alternatives --config php
# Select your preferred version from the list
  • Older PHP versions won't receive security updates forever
  • Some extensions might not be available for old versions
  • Consider using Docker for better isolation if possible