Installing the Latest Versions of PHP on Raspberry Pi OS

In this guide, we will show you how you can install the latest versions of PHP without upgrading your version of Raspberry Pi OS.

Raspberry Pi OS Add PHP repository

PHP is the building block for a vast majority of web applications and we use it for several of our Raspberry Pi projects.

Sometimes developers may update an application to use a newer version of PHP that is not available on your system. To get around this, you can add a third-party repository that provides both older and more recent versions of PHP.

Ondřej Surý maintains the repository for these PHP packages that we are adding and is a very well respected contributor to the PHP community.

These repositories are available for both Debian and Ubuntu systems. They have become one of the primary ways for getting new and old versions of PHP when they are not available.

Equipment

Below is a list of the equipment you may need when adding this third-party PHP repository on your Raspberry Pi.

Recommended

Optional

This tutorial was tested on a Raspberry Pi 400 running the latest version of Raspberry Pi OS Bullseye.

Adding the 3rd Party PHP Repository

The process of adding this repository to your Raspberry Pi is a straightforward one.

By the end of the next few steps, you will be able to install most versions of PHP to your Raspberry Pi.

These steps will differ slightly depending on whether you run Ubuntu on your Pi or another Debian-based operating system like Raspberry Pi.

Adding the Repository on Raspberry Pi OS

Adding a new repository within Raspberry Pi OS is a straightforward process but is a process that is required to be done from the terminal.

We need to download the GPG key and add the repository as a source for the package manager.

1. Before proceeding we need to ensure the “lsb-release” package is installed on Raspberry Pi OS. This package allows us to quickly get the details we need when setting up the PHP repository.

For our first step we need to update the package list by using the command below.

sudo apt update

2. Once the package list has finished updating, run the following command to install the “lsb-release” package.

sudo apt install lsb-release

3. To use any third-party repository within Raspberry Pi OS / Debian, you need to provide its GPG key.

This key is used to verify that the packages you are installing do, in fact, come from that repository.

We can download the GPG key for this PHP repository to our Raspberry Pi using the following command.

curl https://packages.sury.org/php/apt.gpg | sudo tee /usr/share/keyrings/suryphp-archive-keyring.gpg >/dev/null

4. Once the key has been saved to your Raspberry Pi, we can create a new source file that points to the repository.

Use the following one-liner to create this source file with the link to the repository.

echo "deb [signed-by=/usr/share/keyrings/suryphp-archive-keyring.gpg] https://packages.sury.org/php/ $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/sury-php.list

Within this line, you can see that we point to the GPG key that we saved earlier. The “signed-by” text tells APT that it should verify the contents of this repository against the provided key.

5. Since we made changes to the APT package sources, we must perform an update of the package lists.

By running an update, we are requesting new package lists from all of the sources. This will make APT aware of the packages being provided by our new PHP repository.

sudo apt update

6. Once the update completes, you can now download older and newer versions of PHP.

In the case of Raspberry Pi OS Bullseye, this means you can gain access to no longer supported versions of PHP such as 7.3. Alternatively, if you are using Raspberry Pi OS Buster, you can access newer versions of PHP, such as PHP 8.1.

Adding the Repository on Ubuntu

The PHP repository that we are using requires a slightly different process for Ubuntu-based operating systems.

Within Ubuntu, they have a tool called “add-apt-repository” that allows you to easily add a new source like the one we are adding in the next few steps.

1. On your Ubuntu-based system, all you need to do to add this new repository is run the following command.

This tool will handle acquiring the GPG keys and adding the repository to the APT package manager.

sudo apt-add-repository ppa:ondrej/php

2. Once the repository has been added, you will need to perform an update of the package list.

The apt package manager will retrieve a list of packages available from the new repository during this update process.

sudo apt update

Installing PHP 8.1 on a Raspberry Pi

To show you that this repository is working, we will be installing PHP 8.1 to our Raspberry Pi. This version of PHP isn’t available in Bullseye so this will only work if this repository has been added to the sources list.

1. To test this, let us install the CLI version of PHP 8.1 to our Raspberry Pi.

You can install this to the device by running the following command within the terminal.

sudo apt install php8.1-cli

2. If you see no errors, then you have successfully installed PHP 8.1 on your Raspberry Pi. We can verify that this is working by running a tiny PHP script.

Start by creating this simple PHP script by using the following command within the terminal.

echo "<?php echo 'Hello World';" > test.php

This script is super simple and will echo the string “Hello World” to the command line when it is executed.

3. With this script created, you can use PHP 8.1 to run it by using the following command on your device.

php8.1 test.php

If PHP 8.1 has successfully installed to your Pi, you should now see the text “Hello World” appear in the terminal.

Conclusion

Installing versions of PHP that aren’t generally available on your version of Raspberry Pi OS is a relatively straightforward process.

Thanks to the package repository provided by Ondřej Surý, you can gain access to versions of PHP as old as 5.6, as well as the latest releases that might only be in beta.

If you have had any issues installing your required PHP version, feel free to leave a comment below.

With the repository added to your Pi, be sure to check out some of our excellent PHP-driven projects, such as our Nextcloud guide or our Joomla tutorial.

8 Comments

  1. Avatar for Pino
    Pino on

    Hi Emmet,

    Could you also give us the necessary steps to configure our existing apache installation to then use the new version of php?

    I am currently running a LAMP server on my Raspberry Pi 4B 8GB and am hosting a personal WordPress site. Some of the plugins require PHP version 8.0 or above for greater performance and security. I’d like to upgrade but I am afraid it will break my apache installation.

    Any help would be greatly appreciated.

    Thank you,
    Pino

    1. Avatar for Emmet
      Emmet on
      Editor

      Hey Pino,

      That is actually a solid idea and I’ll try and get some time to update the guide with the steps for doing steps.

      With Apache you will need to use the “a2enmod” and “a2dismod” commands to switch versions of PHP.

      1. Start by getting your current PHP Version.

      You are after something like PHP 7.4 or similar.

      php -v

      2. Next use the “a2dismod” to disable that version of PHP. Swap out the version with the one you got in the previous step.

      sudo a2dismod php7.4

      3. After disabling it you can now enable the new version of PHP.

      sudo a2enmod php8.0

      4. For these changes to take effect you will need to restart Apache.

      sudo systemctl restart apache2

      5. If things break, just flip the steps around and return back to your original version of PHP.

      Hopefully this helps you out. Please let me know if you need any more assistance.

      Cheers,
      Emmet

  2. Avatar for Blnukem
    Blnukem on

    Trying to install OpenCart 4.0 for development that requires php 8.0+

    I followed the upgrade tut twice, this is the result after the second attempt:

    pi@raspberrypi:~ $ sudo apt install php8.1-cli
    Reading package lists… Done
    Building dependency tree… Done
    Reading state information… Done
    php8.1-cli is already the newest version (8.1.11-1+0~20220929.27+debian11~1.gbpe414ce).

    Yet phpinfo(); returns this: PHP Version 7.4.30

    So I checked the Pi again and get this:

    i@raspberrypi:~ $ sudo php –version
    PHP 8.1.11 (cli) (built: Sep 29 2022 22:17:15) (NTS)
    Copyright (c) The PHP Group
    Zend Engine v4.1.11, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.11, Copyright (c), by Zend Technologies

    So I try to do the OpenCart install again:

    PHP8+ Required

    Cant seem to get it Pi says PHP Version 8.1.11 yet browser says PHP Version 7.4.30. I did reboots on everything several times, this is just bizarre any ideas?

    1. Avatar for Emmet
      Emmet on
      Editor

      Hi Blnukem,

      It looks like you have PHP 7.4 running alongside PHP 8.1. You will need to install all of the PHP 8.1 packages you require and configure your web server to utilize it.

      Cheers,
      Emmet

  3. Avatar for E
    E on

    Hi,

    I go to execute step 2 after the first step for Raspberry Pi OS (running Raspberry Pi OS) and I get an error dealing with ls-release not being installed.

    You might want to put an instruction or notice that notifies the reader to install ls-release before attempting this or apt install won’t work until they delete the install list for the sury repository and install ls-release and add the repo back in.

    1. Avatar for Emmet
      Emmet on
      Editor

      Hi E,

      That is strange that the “lsb-release” package isn’t installed by default on new versions of Raspberry Pi OS.

      Thank you for letting me know about this, I have updated the steps so it now includes installing the “lsb-release” package in case it isn’t installed.

      Cheers,
      Emmet

  4. Avatar for Max
    Max on

    Hi,

    I’m trying to install the latest PHP onto my Raspberry Pi 4b 8GB. This is just one of the steps towards my ultimate goal of installing NextCould (which is also failing).

    I have a couple of problems, following the above:

    sudo apt-add-repository ppa:ondrej/php

    Returns: sudo: apt-add-repository: command not found

    I managed to resolve this by: sudo apt install software-properties-common

    which allowed me to run the add-apr-repository command, but I then received further errors:

    Warning: apt-key is deprecated. Manage keyring files in trusted.gpg.d instead (see apt-key(8)).
    gpg: no valid OpenPGP data found.
    Exception in thread Thread-1:
    Traceback (most recent call last):
      File "/usr/lib/python3.9/threading.py", line 954, in _bootstrap_inner
        self.run()
      File "/usr/lib/python3.9/threading.py", line 892, in run
        self._target(*self._args, **self._kwargs)
      File "/usr/lib/python3/dist-packages/softwareproperties/SoftwareProperties.py", line 688, in addkey_func
        func(**kwargs)
      File "/usr/lib/python3/dist-packages/softwareproperties/ppa.py", line 386, in add_key
        return apsk.add_ppa_signing_key()
      File "/usr/lib/python3/dist-packages/softwareproperties/ppa.py", line 273, in add_ppa_signing_key
        cleanup(tmp_keyring_dir)
      File "/usr/lib/python3/dist-packages/softwareproperties/ppa.py", line 234, in cleanup
        shutil.rmtree(tmp_keyring_dir)
      File "/usr/lib/python3.9/shutil.py", line 718, in rmtree
        _rmtree_safe_fd(fd, path, onerror)
      File "/usr/lib/python3.9/shutil.py", line 675, in _rmtree_safe_fd
        onerror(os.unlink, fullname, sys.exc_info())
      File "/usr/lib/python3.9/shutil.py", line 673, in _rmtree_safe_fd
        os.unlink(entry.name, dir_fd=topfd)
    FileNotFoundError: [Errno 2] No such file or directory: 'S.gpg-agent.extra'

    I then did: sudo apt update, but it returned more errors:

    Hit:1 http://archive.raspberrypi.org/debian bullseye InRelease
    Hit:2 http://deb.debian.org/debian bullseye InRelease
    Hit:3 http://security.debian.org/debian-security bullseye-security InRelease
    Hit:4 http://deb.debian.org/debian bullseye-updates InRelease
    Ign:5 http://ppa.launchpad.net/ondrej/php/ubuntu kinetic InRelease
    Err:6 http://ppa.launchpad.net/ondrej/php/ubuntu kinetic Release
      404  Not Found [IP: 91.189.95.85 80]
    Hit:7 https://packages.sury.org/php bullseye InRelease
    Reading package lists... Done
    E: The repository 'http://ppa.launchpad.net/ondrej/php/ubuntu kinetic Release' does not have a Release file.
    N: Updating from such a repository can't be done securely, and is therefore disabled by default.
    N: See apt-secure(8) manpage for repository creation and user configuration details.

    Despite the above, I carried on & attempted the final test steps, which ultimately worked.

    I’m concerned that the install isn’t quite right since I’m trying to install nextcloud, but am running into more difficulties there too.

    Would appreciate your thoughts.

    Thanks!

    1. Avatar for Emmet
      Emmet on
      Editor

      Hi Max,

      What operating system are you using? From the “apt update” result you got it looks like you are running Raspberry Pi OS Bullseye.

      This means you should have followed the “Adding the Repository on Raspberry Pi OS” subheading and not “Adding the Repository on Ubuntu“.

      Cheers,
      Emmet

Leave a Reply

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