Do you need an older version of PHP and won’t want to loose newest version ?
How to Install Different PHP Versions in Ubuntu?
PHP is a widely-used, open-source, general-purpose scripting language that is best suited for developing websites and web-based applications. There are three supported versions of PHP — PHP 5.6, 7.x, and 8.0. In this tutorial, we will explain how to install all supported versions of PHP in Ubuntu and its derivatives. We will also show how to set the default version of PHP to be used on the Ubuntu system.
Before we begin, it is essential to note that PHP 8.1 and 8.2 is the supported stable version in the Ubuntu software repositories. You can confirm this by running the apt command below:
$ sudo apt show php
Now, let’s proceed to install different PHP versions in Ubuntu.
Step 1: Adding PPA
The first step is to add the PPA, which contains different versions of PHP. You can add the PPA by running the following commands:
$ sudo apt install software-properties-common
$ sudo add-apt-repository ppa:ondrej/php
Step 2: Updating the System
After adding the PPA, update the system by running the following command:
$ sudo apt-get update
Step 3: Installing Different Versions of PHP
You can install different supported versions of PHP depending on the web server you are using. In this tutorial, we will show how to install PHP versions for both Apache and Nginx web servers.
For Apache Web Server
To install different versions of PHP for Apache web server, run the following commands:
$ sudo apt install php5.6 [PHP 5.6]
For Nginx Web Server
To install different versions of PHP for Nginx web server, run the following commands:
$ sudo apt install php5.6-fpm [PHP 5.6]
Step 4: Install Required PHP module
Now you can install the most required PHP module, run the following commands:
$ sudo apt install php5.6-cli php5.6-xml php5.6-mysql
Step5: Finally, verify your default PHP version
It still appears same (I know). Now follow step to change Version.
$ php -v
Step6: Set Default PHP Version in Ubuntu
Set PHP 5.6 Version in Ubuntu, Similarly you can set any version.
sudo update-alternatives --set php /usr/bin/php5.6
To set the PHP version that will work with the Apache web server, use the commands below. First, disable the current version with the a2dismod command and then enable the one you want with the a2enmod command.
I was having 8.1 in my system so I am disabling that
sudo a2dismod php8.0
I want 5.6 so I am enabling that, You can do according to your need
sudo a2enmod php5.6
Restarting Apache server
sudo systemctl restart apache2
After switching from one version to another, you can find your PHP configuration file, by running the command below. ( I am enabling for 5.6 version)
$ sudo update-alternatives --set php /usr/bin/php5.6
$ php -i | grep "Loaded Configuration File"
All Done you can confirm by using following command
$ php -v
Thank You!
Note: I was downgrading from PHP 8 to PHP 5.6.
So, command may vary according to your need.