How to Upgrade from PHP 7.x to PHP 8 on Ubuntu/Apache

How to Upgrade from PHP 7.x to PHP 8.x on Linux (Ubuntu, Debian, Fedora & More)

Last updated on | 22 replies

Intro

This guide covers upgrading from an existing PHP 7.x release to the latest PHP 8.x (8.4 at the time of writing).

If you are already on PHP 8.x and wish to upgrade to a newer PHP 8 release (e.g., 8.4 to a future 8.x), please see this guide instead: How to Upgrade PHP 8 on Linux

We use Ubuntu (18.04, 20.04, 22.04, 24.04, and beyond) as the main example, but these steps are similar for other Linux distributions such as Debian, Linux Mint, Fedora, CentOS, or Arch. Simply adapt the commands (like yum, dnf, or pacman) and repositories (e.g., Remi for CentOS/Fedora) to match your distro.

Prerequisites

You must back up your server before running these commands as they cannot be reversed easily. If you are on cloud hosting, make sure you image (snapshot) your instance before running any of these commands.

To find out which version of PHP you are currently using, run:

php -v

If you are running PHP 7.x, continue with this guide to upgrade to PHP 8 (8.4 or newer).

1. PHP Packages

Upgrading from PHP 7.x to PHP 8.x involves installing not only the new PHP package but also the corresponding PHP 8 extensions. If you rely on cURL, for example, you’ll need to install the php8.4-curl package later.

Below, you can see how to list your currently installed PHP 7.x extensions. Make note of any that are critical to your web application so you can install their PHP 8 equivalents.

dpkg -l | grep php | tee packages.txt

Output:

ii  libapache2-mod-php               1:7.2+60ubuntu1                             all          server-side, HTML-embedded scri
ii  libapache2-mod-php7.2            7.2.24-0ubuntu0.18.04.7                     amd64        server-side, HTML-embedded scri
ii  php                              1:7.2+60ubuntu1                             all          server-side, HTML-embedded scri
ii  php-bz2                          1:7.2+60ubuntu1                             all          bzip2 module for PHP [default]
ii  php-common                       1:60ubuntu1                                 all          Common files for PHP packages
ii  php-curl                         1:7.2+60ubuntu1                             all          CURL module for PHP [default]
ii  php-gd                           1:7.2+60ubuntu1                             all          GD module for PHP [default]
ii  php-mbstring                     1:7.2+60ubuntu1                             all          MBSTRING module for PHP [defaul
ii  php-mysql                        1:7.2+60ubuntu1                             all          MySQL module for PHP [default]
ii  php-pear                         1:1.10.5+submodules+notgz-1ubuntu1.18.04.3  all          PEAR Base System
ii  php-php-gettext                  1.0.12-0.1                                  all          read gettext MO files directly,
ii  php-phpseclib                    2.0.9-1                                     all          implementations of an arbitrary
ii  php-tcpdf                        6.2.13+dfsg-1ubuntu1                        all          PHP class for generating PDF fi
ii  php-xml                          1:7.2+60ubuntu1                             all          DOM, SimpleXML, WDDX, XML, and
ii  php-zip                          1:7.2+60ubuntu1                             all          Zip module for PHP [default]
ii  php7.2                           7.2.24-0ubuntu0.18.04.7                     all          server-side, HTML-embedded scri
ii  php7.2-bz2                       7.2.24-0ubuntu0.18.04.7                     amd64        bzip2 module for PHP
ii  php7.2-cli                       7.2.24-0ubuntu0.18.04.7                     amd64        command-line interpreter for th
ii  php7.2-common                    7.2.24-0ubuntu0.18.04.7                     amd64        documentation, examples and com
ii  php7.2-curl                      7.2.24-0ubuntu0.18.04.7                     amd64        CURL module for PHP
ii  php7.2-gd                        7.2.24-0ubuntu0.18.04.7                     amd64        GD module for PHP
ii  php7.2-json                      7.2.24-0ubuntu0.18.04.7                     amd64        JSON module for PHP
ii  php7.2-mbstring                  7.2.24-0ubuntu0.18.04.7                     amd64        MBSTRING module for PHP
ii  php7.2-mysql                     7.2.24-0ubuntu0.18.04.7                     amd64        MySQL module for PHP
ii  php7.2-opcache                   7.2.24-0ubuntu0.18.04.7                     amd64        Zend OpCache module for PHP
ii  php7.2-readline                  7.2.24-0ubuntu0.18.04.7                     amd64        readline module for PHP
ii  php7.2-xml                       7.2.24-0ubuntu0.18.04.7                     amd64        DOM, SimpleXML, WDDX, XML, and
ii  php7.2-zip                       7.2.24-0ubuntu0.18.04.7                     amd64        Zip module for PHP
ii  phpmyadmin                       4:4.6.6-5ubuntu0.5                          all          MySQL web administration tool

The example above shows PHP 7.2 extensions installed on a server prior to upgrading to PHP 8. Copy your own results into a text file and keep them safe so you know which PHP 8 equivalents to install later.

2. Uninstall/Remove PHP 7.x and Extensions

To uninstall PHP 7.x and all its extensions, run:

sudo apt-get purge php7.*

Press y and ENTER when prompted.

If you have phpMyAdmin installed, you may be presented with this screen:

If prompted with the above message, select YES and press ENTER.

You may also be prompted to delete the database.

If prompted with the message above, select NO and press ENTER.

3. Autoclean and Autoremove

After uninstalling packages, it’s a good idea to run the following commands to remove any lingering dependencies:

sudo apt-get autoclean
sudo apt-get autoremove

Press Y and ENTER if prompted.

4. (Optional) Add Ondřej Surý’s PPA repository

If you are running Ubuntu 22.04 or above, you do not need to add this repository. Skip to step 5.

If you are running Ubuntu 20.04 or 18.04, the PHP 8 binary packages are only available in the Ondřej Surý PPA repository. Install it with the command below. (For Fedora, CentOS, or RHEL, enable the Remi repository instead.)

sudo add-apt-repository ppa:ondrej/php

You may see a welcome message; press ENTER to continue.

5. Install PHP 8

As of writing, the latest stable release is PHP 8.4. If a newer version (like 8.5) is available, replace 8.4 with that version in the commands below.

sudo apt-get update
sudo apt-get install php8.4

Output:

Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  libapache2-mod-php8.4 libpcre2-8-0 php-common php8.4-cli php8.4-common
  php8.4-opcache php8.4-readline
Suggested packages:
  php-pear
The following NEW packages will be installed:
  libapache2-mod-php8.4 libpcre2-8-0 php-common php8.4 php8.4-cli php8.4-common
  php8.4-opcache php8.4-readline
0 upgraded, 8 newly installed, 0 to remove and 159 not upgraded.
Need to get 5035 kB of archives.
After this operation, 22.0 MB of additional disk space will be used.
Do you want to continue? [Y/n]

Press Y and ENTER if prompted.

Restart Apache (or your web server) to load the new PHP version:

sudo systemctl restart apache2

6. Install PHP 8 Extensions

The command below includes commonly used PHP extensions, suitable for a typical WordPress site. If additional extensions are missing, refer to your earlier packages.txt (or your distro’s package manager) to install the ones you need (e.g., php8.4-curl, php8.4-gd, etc.).

sudo apt install php8.4-common php8.4-mysql php8.4-xml php8.4-xmlrpc php8.4-curl php8.4-gd php8.4-imagick php8.4-cli php8.4-dev php8.4-imap php8.4-mbstring php8.4-opcache php8.4-soap php8.4-zip php8.4-intl -y

If running Nginx, be sure to install PHP-FPM (matching your new PHP version):

sudo apt install php8.4-fpm -y

Restart Apache (or Nginx) after installing any extensions:

sudo systemctl restart apache2

For Nginx:

sudo systemctl restart nginx
sudo systemctl restart php8.4-fpm

7. Check PHP version

To verify that you’re now running PHP 8.4, use:

php -v

Output:

PHP 8.4.1 (cli) (built: Dec 29 2024 10:15:00) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.4.1, Copyright (c) Zend Technologies
    with Zend OPcache v8.4.1, Copyright (c), by Zend Technologies

PHP 8.4 is now installed and working! Be sure to test your site or web application thoroughly to ensure compatibility.

Let me know if this helped. Follow me on Twitter, Facebook and YouTube, or 🍊 buy me a smoothie.

22 replies

Leave a reply

Your email address will not be published. Required fields are marked *

  1. Hi,

    I have an ubuntu OS 18.04.4 LTS and php7.2, but I have followed this steps and it didn’t work for me to upgrade my php to 8.1. Anyone with a suggestion?.. Thank you.

  2. I also have to say thank you! Quick and easy steps. Works on the first try. I only had to adapt my nginx config and it works fine. Thanks again 🙂

  3. Thanks a bunch! I’d tried other postings without success to the point of busting the whole setup.
    This one worked perfectly.

    1. Ondřej Surý is the maintainer of the PHP package in the Debian and Ubuntu operating systems. By adding his Personal Package Archive (PPA) repository to your system, you will be able to install the latest version of PHP directly from the source package maintained by Surý, rather than from the version included in the official Ubuntu repositories, which may not be the latest version.

  4. Thank you for the info! Very useful!!

    Just a side note:
    If anyone is using memcache and redis, the extensions names are different:
    For php-memcache and php-redis that used to work with php 7.x, the extensions php8.0-memcache and php8.0-redis should be used instead

    Greetings

  5. This looks excellent, thank you. One question before I start – in my /etc directory there is are these files and directories. Will they be converted or do I need to do it manually:
    /etc/apache2/mods-enabled/php7.4.load
    /etc/apache2/mods-enabled/php7.4.conf
    /etc/apache2/mods-available/php7.4.load
    /etc/apache2/mods-available/php7.4.conf
    /etc/php/7.4/cli/php.ini
    /etc/php/7.4/apache2/php.ini

  6. Hello,

    I updated the php 8 on wordpress, via command line.

    Everything went fine, I only had 2 plugins to disable renaming the folder, in the end everything worked out!

    Greetings from Brazil.

    Thank you very much for the content, it helped me!