This article explains how to downgrade the version of PHP on Ubuntu 24.04.
PHP is a popular open-source scripting language ideal for web development. It can be embedded into HTML, enabling the creation of dynamic web pages, database interactions, and effective management of server-side tasks.
If the version of PHP you have installed is not compatible with some of your applications and services, you can downgrade to an earlier version. Reverting to an older version can help resolve issues and ensure the smooth operation of legacy code or specific third-party services that depend on those older versions.
The following steps will guide you through downgrading the PHP version in your environment.
Get PHP details
To downgrade PHP on Ubuntu, you must gather information about the current PHP version and its supporting modules.
First, list the current version of PHP by running the command below.
php --version
php -v
The command provided should produce an output similar to the example below. The currently installed version of PHP is [8.3.6].
PHP 8.3.6 (cli) (built: Dec 2 2024 12:36:18) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.6, Copyright (c) Zend Technologies
with Zend OPcache v8.3.6, Copyright (c), by Zend Technologies
Next, execute the command below to check which PHP modules have been installed.
dpkg --get-selections | grep -i php
The command above should output something similar to the one below. Here is a list of the PHP modules version currently installed on your system.
php8.3-bcmath install
php8.3-cli install
php8.3-common install
php8.3-curl install
php8.3-fpm install
php8.3-gd install
php8.3-imap install
php8.3-intl install
php8.3-ldap install
php8.3-mbstring install
php8.3-mysql install
php8.3-opcache install
php8.3-readline install
php8.3-soap install
php8.3-sqlite3 install
php8.3-xml install
php8.3-xmlrpc install
php8.3-zip install
......
......
Having collected PHP and all its required modules, you can now downgrade to a version compatible with your environment.
Install older version of PHP
To downgrade to PHP version 8.2, you can install that version on your machine instead.
To install PHP version 8.2 on Ubuntu, run the command below.
sudo apt install php8.2
If PHP 8.2 is unavailable in the default Ubuntu repositories, you can add a third-party PPA that provides this version.
Run the command below to add the repository.
sudo add-apt-repository ppa:ondrej/php
sudo apt update
Once the package index has been added and refreshed, try installing PHP 8.2 again.
After installing PHP 8.2, gather the list of currently installed PHP modules and ensure that the appropriate versions for PHP 8.2 are installed.
Obtain the list and execute the command, replacing version 8.3 with 8.2 for each module.
sudo apt install php8.2 php8.2-cli php8.2-commom php8.2-opcache php8.2-readline php8.2-bcmath php8.3-curl php8.2-gd
Now you should have both [PHP 8.3] and [PHP 8.2] installed. However, your web server, such as Apache or Nginx, might still use PHP 8.3.
Switch PHP in Apache
You can use the command below to switch to the PHP version in Apache.
Disable the current PHP.
sudo a2dismod php8.3
Enable the PHP version you want to use.
sudo a2enmod php8.2
Restart Apache to apply the changes.
sudo systemctl restart apache2.service
Switch PHP in Nginx
Open the server block file and update the PHP version when using an Nginx web server.
location ~ .php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
Restart Nginx and PHP-FPM after making the changes.
sudo systemctl restart nginx.service
sudo systemctl restart php8.2-fpm
That should do it!
Conclusion:
In summary, downgrading the PHP version on Ubuntu 24.04 can be straightforward if you follow the necessary steps. Here are the key points to remember:
- Check Current PHP Version: Always start by verifying your existing PHP version.
- List Installed PHP Modules: Assess which modules are currently installed to ensure compatibility with the older PHP version.
- Install Older PHP Version: Use the appropriate commands to install the required older version of PHP, such as PHP 8.2.
- Verify PHP and Modules: Ensure that the necessary PHP modules for the older version are correctly installed.
- Configure Web Server: Switch the PHP version as required depending on your web server (Apache or Nginx).
- Restart Services: Always restart your web server and PHP-FPM after making configuration changes to apply the updates.
By following these steps, you can effectively manage your PHP versions and maintain the stability of your applications.
Leave a Reply