How to Downgrade PHP Version on Ubuntu 24.04
Downgrading PHP on Ubuntu 24.04 lets you switch to an older version of the PHP scripting language.
Install the desired PHP version using `sudo apt install phpX.Y`. Then, disable the current PHP module with `sudo a2dismod phpX.Y` and enable the new one with `sudo a2enmod phpX.Y`, restarting your web server afterward.
PHP is a coding language that runs on servers and helps make websites work. Sometimes, older websites or specific code need an older PHP version, like PHP 8.1 or 8.2, to run properly.
This guide shows you how to replace your current PHP setup with an older one on Ubuntu 24.04.
- First, open your terminal and check your current PHP version by typing
php -v. - Next, list all installed PHP modules with
dpkg --get-selections | grep -i phpto see what you have. - If the older PHP version you need isn’t in Ubuntu’s default list, add the Ondřej Surý PPA by running
sudo add-apt-repository ppa:ondrej/php. - Update your package list with
sudo apt update. - Install the specific older PHP version and its common modules. For example, to install PHP 8.2, use
sudo apt install php8.2 php8.2-cli php8.2-common php8.2-opcache php8.2-readline php8.2-bcmath php8.2-curl. - Verify both versions are installed by checking
php -vandphp8.2 -v(or your chosen version). - For Apache, disable the current PHP module (e.g.,
sudo a2dismod php8.3) and enable the new one (e.g.,sudo a2enmod php8.2), then restart Apache. - For Nginx, update your server block’s
fastcgi_passto point to the new PHP-FPM socket (e.g.,unix:/var/run/php/php8.2-fpm.sock) and restart Nginx and PHP-FPM.
Get PHP details
Checking your current PHP version is the first step when you need to downgrade PHP on Ubuntu 24.04. You can easily see which version you have installed by opening your terminal and typing the command `php -v`. This will show you the exact PHP version currently running on your system.
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.
With your target PHP version and its necessary modules identified, you’re ready to downgrade. This process ensures your applications run correctly on Ubuntu 24.04 by using a PHP version, such as PHP 8.1, that matches your project’s needs.
Install older version of PHP
If your project needs an older version, you can install an older PHP version on Ubuntu 24.04. For instance, to install PHP 8.2, you can use the command `sudo apt install php8.2`. If this version isn’t available in Ubuntu’s standard software sources, you might need to add a third-party repository first.
To install PHP version 8.2 on Ubuntu, run the command below.
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.
You can check the installed PHP modules for PHP 8.2. To see the PHP module list for PHP 8.2, run the command `php -m`. For each module listed, replace `’version 8.3’` with `’8.2’`. Running this command confirms that both PHP 8.2 and PHP 8.3 are now on your system.
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
Switching the PHP version for Apache involves a few commands.
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.
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 short, downgrading your PHP version on Ubuntu 24.04 is manageable by following these steps. Here are the key takeaways:
-
- 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.
Managing PHP versions on Ubuntu 24.04 ensures application stability; for example, downgrading to PHP 8.1 can fix compatibility issues if your application requires it.
How do I switch to a different version of PHP in Ubuntu?
To switch the default PHP version, use the sudo update-alternatives –config php command. For example: sudo update-alternatives –config php # There are 4 choices for the alternative php (providing /usr/bin/php).
Was this guide helpful?
About the Author
Richard
Tech Writer, IT Professional
Richard, a writer for Geek Rewind, is a tech enthusiast who loves breaking down complex IT topics into simple, easy-to-understand ideas. With years of hands-on experience in system administration and enterprise IT operations, he’s developed a knack for offering practical tips and solutions. Richard aims to make technology more accessible and actionable. He's deeply committed to the Geek Rewind community, always ready to answer questions and engage in discussions.
Hi,
php8.2-commom. not available
so, impossible process