Intro
This guide covers upgrading from an existing PHP 8.x release to the latest PHP 8.x (8.2, 8.3, etc.) on Ubuntu, including versions 18.04, 20.04, 22.04, 24.04, and beyond. If you’re on Ubuntu 22.04 or above, you can typically install the latest packages from the official repositories. However, if you’re running Ubuntu 20.04 or older, you’ll need Ondřej Surý’s PPA to access the newest PHP 8.x packages.
Note: If you need to upgrade from PHP 7.x to PHP 8, please refer to our separate guide:
Prerequisites
Before making any changes to your server, it’s crucial to have a reliable backup. If you’re on a cloud hosting service, create a snapshot of your instance so you can roll back if anything goes wrong.
Check your currently installed version of PHP by running the command below:
php -v
The output might look like this:
Output:
PHP 8.1.2-1ubuntu2.20 (cli) (built: Dec 3 2024 20:14:35) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.2, Copyright (c) Zend Technologies
with Zend OPcache v8.1.2-1ubuntu2.20, Copyright (c), by Zend Technologies
This example shows PHP 8.1.2 installed. Let’s proceed to upgrade to a newer 8.x version.
1. Identify Your PHP Extensions
When upgrading from one PHP 8.x release to another (for example, 8.0 to 8.2), you also need to upgrade your PHP extensions (like php8.0-curl
to php8.2-curl
). First, list all currently installed PHP 8 packages:
dpkg -l | grep php | tee php8-packages.txt
The command above will copy the list of all installed PHP packages into php8-packages.txt
. Keep this file safe in case you need to reference or reinstall specific extensions in their newer versions.
2. Uninstall the Old PHP 8.x Version
We will remove any existing PHP 8.x versions (like PHP 8.0 or 8.1) before installing the newer release. This helps avoid conflicts between packages:
sudo apt-get purge php8.*
Confirm removal by pressing y
and ENTER
when prompted.
If you have phpMyAdmin
installed, you may be prompted about removing its database. Choose NO if you want to keep the existing database intact.
3. Autoclean & Autoremove
After uninstalling PHP 8.x, clear out any redundant packages to keep your system tidy.
sudo apt-get autoclean
sudo apt-get autoremove
Press y
and ENTER
to confirm if prompted.
4. (Optional) Add Ondřej Surý’s PPA Repository
If you are on Ubuntu 22.04 or newer, you generally do not need to add this repository because newer PHP packages should already be available in the default Ubuntu repositories.
If you are on Ubuntu 20.04 or older, newer PHP 8.x packages are only available via the Ondřej Surý PPA. Add it below:
sudo add-apt-repository ppa:ondrej/php
Press ENTER
when prompted to continue.
5. Install the Newer PHP 8.x Version
Next, update your package list and install a newer PHP 8.x version, for example, PHP 8.2. (Replace 8.2
with 8.3
or newer if available.)
sudo apt-get update
sudo apt-get install php8.2
Press y
and ENTER
if prompted to confirm installation.
If you are running Apache, restart it to activate the new PHP version:
sudo systemctl restart apache2
6. Install PHP 8.x Extensions
Install the matching extensions for your new PHP version. Below is an example for PHP 8.2 with commonly used extensions for WordPress:
sudo apt-get install php8.2-common php8.2-mysql php8.2-xml php8.2-xmlrpc php8.2-curl php8.2-gd php8.2-imagick php8.2-cli php8.2-dev php8.2-imap php8.2-mbstring php8.2-opcache php8.2-soap php8.2-zip php8.2-intl -y
If you are running Nginx, you should also install PHP-FPM:
sudo apt-get install php8.2-fpm -y
Restart Apache or Nginx after installing any extensions:
sudo systemctl restart apache2
or if using Nginx:
sudo systemctl restart nginx
sudo systemctl restart php8.2-fpm
7. Verify the Upgrade
Finally, verify that the new PHP version is active:
php -v
Output:
PHP 8.2.26 (cli) (built: Nov 25 2024 18:09:54) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.26, Copyright (c) Zend Technologies
with Zend OPcache v8.2.26, Copyright (c), by Zend Technologies
You have successfully upgraded to a newer PHP 8.x version on Ubuntu! Remember to test your web applications thoroughly after the upgrade to ensure full compatibility.
Let me know if this helped. Follow me on Twitter, Facebook and YouTube, or 🍊 buy me a smoothie.