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

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

Last updated on | 12 replies

PHP 8 is a major update of the PHP language released on November 26, 2020. It contains many new features and optimizations. In this guide we will upgrade from PHP 7.x to PHP 8 on Ubuntu 18.04, 20.04 and 22.04.

Intro

This guide has been tested on Ubuntu 18.04 and 20.04 with PHP 7.x. It should also work on Ubuntu 22.04 but you do not need to add the custom repository.

Prerequisites

You must back up your server before running these commands as they cannot be reversed. If you are on cloud hosting, make sure you image your instance before running any of these commands. You have been warned!

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

php -v

If you are running PHP 7.x, you can continue with this guide to upgrade to PHP 8.

1. PHP Packages

Upgrading from PHP 7.x to PHP 8 involves not only upgrading PHP core, but all of its extensions. For example, if you use the PHP extension cURL, you will need to manually install the PHP 8 version at the end of this guide.

At the end of this guide, I have included a command to install the most common PHP 8 extensions, however, you should check which PHP 7.x extensions are currently installed on your particular server and take note of anything critical to the running of your own web app.

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 my own server before upgrading to PHP 8. Copy your own results into a text file and keep it safe in case you need to install the PHP 8 version later.

2. Uninstall/Remove PHP 7.x and Extensions

To uninstall PHP 7.x and all of its extensions, run the command below.

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 from Linux, it’s advised to run these two commands.

sudo apt-get autoclean
sudo apt-get autoremove

Press Y and ENTER if prompted.

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

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.

If you are running Ubuntu 22.04 and above, you do not need to add this repository below. Instead, skip to part 5.

sudo add-apt-repository ppa:ondrej/php

You may see a welcome message.

Output:

Co-installable PHP versions: PHP 5.6, PHP 7.x and most requested extensions are included. Only Supported Versions of PHP (http://php.net/supported-versions.php) for Supported Ubuntu Releases (https://wiki.ubuntu.com/Releases) are provided. Don't ask for end-of-life PHP versions or Ubuntu release, they won't be provided.

Debian oldstable and stable packages are provided as well: https://deb.sury.org/#debian-dpa

You can get more information about the packages at https://deb.sury.org

IMPORTANT: The -backports is now required on older Ubuntu releases.

BUGS&FEATURES: This PPA now has a issue tracker:
https://deb.sury.org/#bug-reporting

CAVEATS:
1. If you are using php-gearman, you need to add ppa:ondrej/pkg-gearman
2. If you are using apache2, you are advised to add ppa:ondrej/apache2
3. If you are using nginx, you are advised to add ppa:ondrej/nginx-mainline
   or ppa:ondrej/nginx

PLEASE READ: If you like my work and want to give me a little motivation, please consider donating regularly: https://donate.sury.org/

WARNING: add-apt-repository is broken with non-UTF-8 locales, see
https://github.com/oerdnj/deb.sury.org/issues/56 for workaround:

# LC_ALL=C.UTF-8 add-apt-repository ppa:ondrej/php
 More info: https://launchpad.net/~ondrej/+archive/ubuntu/php
Press [ENTER] to continue or Ctrl-c to cancel adding it.

Press ENTER to add the repository.

5. Install PHP 8

As of writing (June 2022), PHP 8.1 is now available so we will install that. If a later version is available, make sure to change the commands below to match.

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

Output:

Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  libapache2-mod-php8.1 libargon2-1 libpcre2-8-0 libsodium23 php-common php8.1-cli php8.1-common
  php8.1-opcache php8.1-readline
Suggested packages:
  php-pear
The following NEW packages will be installed:
  libapache2-mod-php8.1 libargon2-1 libpcre2-8-0 libsodium23 php-common php8.1 php8.1-cli php8.1-common
  php8.1-opcache php8.1-readline
0 upgraded, 10 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.

sudo systemctl restart apache2

6. Install PHP 8 Extensions

The command below includes some of the most popular PHP extensions, which should cover a typical WordPress site. However, if you find that some extensions are missing, refer to Step 1 above and manually install the packages you require.

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

If running Nginx, make sure to install FPM.

sudo apt install php8.1-fpm -y

Restart Apache.

sudo systemctl restart apache2

7. Check PHP version

To check if you have installed PHP 8 correctly, run:

php -v

Output:

PHP 8.1.6 (cli) (built: May 17 2022 16:47:53) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.6, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.6, Copyright (c), by Zend Technologies

PHP 8 should now be installed and working. Hurrah!

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

p.s. I increased my AdSense revenue by 200% using AI 🤖. Read my Ezoic review to find out how.

12 replies

Leave a reply

Your email address will not be published.

  1. 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

  2. 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

  3. 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!