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.

Raspberry Pi OS Trixie already includes PHP 8.4 and receives security updates with the rest of your system. For most projects, this is the version you should use.

You only need this guide if a program you want to run requires a newer version of PHP than your operating system provides. To work around this, you can add a third-party repository that provides newer PHP versions, such as PHP 8.5.

Ondřej Surý maintains the repository for these PHP packages and is a well-respected contributor to the PHP community.

Please note that adding a third-party repository means trusting it with your whole system, as its packages are installed with full root access. We will set it up so your Raspberry Pi keeps using Raspberry Pi OS’s packages wherever they exist and only uses newer PHP versions from this repository.

Equipment

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

Optional

This tutorial was tested on a Raspberry Pi 5 running the latest version of Raspberry Pi OS Trixie.

Adding the 3rd Party PHP Repository

Adding a new repository in Raspberry Pi OS is straightforward, but it must be done from the terminal.

These steps also work on Ubuntu. Ondřej used to provide PHP for Ubuntu through a separate PPA, but he is moving it to this same repository, and the PPA has no packages for Ubuntu 26.04. Please note that Ubuntu 26.04 already comes with PHP 8.5, so you will only need this repository once a newer PHP version is released.

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 updateCopy

2. Once the package list has finished updating, run the following command to install the lsb-release and curl packages.

sudo apt install -y lsb-release curlCopy

3. To use any third-party repository within Raspberry Pi OS, you need to provide its signing key. This key verifies that the packages you install actually come from that repository.

Ondřej provides this key as a package. By installing the key this way, your Raspberry Pi will get a new key automatically whenever he renews it. Download and install this package by running the following two commands.

curl -sSLo /tmp/debsuryorg-archive-keyring.deb https://packages.sury.org/debsuryorg-archive-keyring.deb
sudo dpkg -i /tmp/debsuryorg-archive-keyring.debCopy

The first command downloads the key package to the /tmp folder. The second command installs it, which saves the key to /usr/share/keyrings/debsuryorg-archive-keyring.gpg.

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/debsuryorg-archive-keyring.gpg] https://packages.sury.org/php/ $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/sury-php.listCopy

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.

If you followed an older version of this guide, this line replaces the one you added back then. You can also delete the old key file by running sudo rm /usr/share/keyrings/suryphp-archive-keyring.gpg.

5. Next, we will tell APT to only use this repository for packages that Raspberry Pi OS does not provide itself. Without this, the repository’s versions of packages such as “php-common” and “libgd3” would replace the ones that come with Raspberry Pi OS.

Begin writing a new APT preferences file by using the following command.

sudo nano /etc/apt/preferences.d/sury-phpCopy

6. Within this file, type in the following lines.

Package: *
Pin: origin packages.sury.org
Pin-Priority: 100Copy

These lines give every package from this repository a priority of 100, lower than the 500 that Raspberry Pi OS’s own packages have. When both have a package, APT will always pick Raspberry Pi OS’s version, so it will only install packages from this repository that Raspberry Pi OS does not have, such as PHP 8.5.

Once done, save and quit by pressing CTRL + X, then Y, followed by ENTER.

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

Running an update requests new package lists from all sources. This will make APT aware of the packages being provided by our new PHP repository.

sudo apt updateCopy

Once the update completes, you can install newer versions of PHP than the one that comes with Raspberry Pi OS.

Raspberry Pi OS’s PHP packages block versions older than PHP 8.4, so with this setup, the repository is only used for newer versions.

Installing PHP 8.5 on a Raspberry Pi

To show you that this repository is working, we will be installing PHP 8.5 on our Raspberry Pi. This version of PHP is not available in Raspberry Pi OS Trixie, so this will only work if this repository has been added to the sources list.

1. To test this, we will install the command-line version of PHP 8.5 on our Raspberry Pi. You can install this on the device by running the following command in the terminal.

sudo apt install -y php8.5-cliCopy

When you install extensions for this version of PHP, always use the package names with the version in them, such as php8.5-mbstring. The names without a version, such as php-mbstring, are for Raspberry Pi OS’s own PHP 8.4.

2. If you see no errors, then you have successfully installed PHP 8.5 on your Raspberry Pi. We can verify it’s working by running a tiny PHP script.

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

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

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.5 to run it by using the following command on your device.

php8.5 test.phpCopy

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

Switching Between PHP Versions

Installing PHP 8.5 does not remove the version of PHP that comes with Raspberry Pi OS. Both can be installed side by side, and each has its own command, such as php8.4 and php8.5. However, the php command and your web server will only use one of them.

1. To see which version the php command currently runs, use the following command.

php -vCopy

This will most likely show PHP 8.5, as the php command uses the newest version of PHP that is installed. It will only still show PHP 8.4 if you have installed Raspberry Pi OS’s php-cli package.

This means that any PHP scripts you run from the terminal or a cron job will now use PHP 8.5. If they need extensions, such as php8.5-mysql, install them, or switch the php command back to PHP 8.4 by using the next step.

2. You can change which version of PHP the php command uses by running the following command.

sudo update-alternatives --config phpCopy

This command lists every version of PHP on your Raspberry Pi. Type in the number next to /usr/bin/php8.5 and press ENTER to switch to PHP 8.5.

There are 2 choices for the alternative php (providing /usr/bin/php).

  Selection    Path             Priority   Status
------------------------------------------------------------
* 0            /usr/bin/php8.5   85        auto mode
  1            /usr/bin/php8.4   84        manual mode
  2            /usr/bin/php8.5   85        manual mode

Press <enter> to keep the current choice[*], or type selection number:Copy

To go back to Raspberry Pi OS’s own PHP, run the same command again and pick /usr/bin/php8.4.

3. Your web server does not use the php command, so it will keep using the same version of PHP until you change it. How you do this depends on how your web server runs PHP. Before switching, install the PHP 8.5 version of every extension your website needs, such as php8.5-mysql.

If you use Apache with its PHP module, as in our Apache guide, install the PHP 8.5 module, disable the PHP 8.4 module, and enable the new one by running the following commands.

sudo apt install -y libapache2-mod-php8.5
sudo a2dismod php8.4
sudo a2enmod php8.5
sudo systemctl restart apache2Copy

To go back to PHP 8.4, run the last three commands again with php8.4 and php8.5 swapped.

4. If your web server uses PHP-FPM, as our NGINX, Lighttpd and Caddy guides do, install PHP-FPM 8.5 together with the extensions your website needs in a single command. For example, the following command installs the same extensions as our Lighttpd guide.

sudo apt install -y php8.5-fpm php8.5-mbstring php8.5-mysql php8.5-curl php8.5-gd php8.5-zip php8.5-xmlCopy

A web server that connects to /run/php/php-fpm.sock will start using PHP 8.5 as soon as it is installed. You can choose which version this connects to with the following command, picking /run/php/php8.4-fpm.sock to go back to PHP 8.4.

sudo update-alternatives --config php-fpm.sockCopy

If your web server’s configuration names a version of PHP instead, such as php8.4-fpm.sock, change it to php8.5-fpm.sock and restart your web server.

Removing the PHP Repository

If you no longer need the newer version of PHP, you can remove it along with the repository. Your Raspberry Pi will then go back to only using the PHP that comes with Raspberry Pi OS.

1. First, if you switched the php command or your web server to PHP 8.5, switch them back to PHP 8.4 by following the steps in the previous section.

If your web server uses PHP-FPM and stops working after the next step, run sudo systemctl restart php8.4-fpm to point it back to PHP 8.4.

2. Next, remove PHP 8.5 by running the following command. Every PHP 8.5 package depends on php8.5-common, so removing it also removes the rest of PHP 8.5, along with its configuration files.

sudo apt purge -y php8.5-commonCopy

3. Now remove the repository’s source and preferences files, then its key package, using the following two commands.

sudo rm /etc/apt/sources.list.d/sury-php.list /etc/apt/preferences.d/sury-php
sudo apt purge -y debsuryorg-archive-keyringCopy

4. Lastly, update the package list so that APT no longer looks for packages from the repository.

sudo apt updateCopy

If you followed an older version of this guide, also remove PHP 8.1 by running sudo apt purge -y php8.1-common before step 3. The repository may have replaced some of Raspberry Pi OS’s own packages too, which will no longer receive updates. Once you have finished the steps above, you can list these packages by using the following command.

apt list --installed 2>/dev/null | grep -F ",local]"Copy

Put each package it lists back to Raspberry Pi OS’s version by running the following command, replacing <PACKAGE> with the package’s name.

sudo apt install -y --allow-downgrades <PACKAGE>/trixieCopy

Conclusion

Installing a newer version of PHP than the one that comes with Raspberry Pi OS is relatively straightforward.

Thanks to the package repository provided by Ondřej Surý, you can run the latest releases of PHP on your Raspberry Pi while the rest of Raspberry Pi OS stays as it is.

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

Be sure to check out some of our excellent PHP-driven projects, such as our Nextcloud guide or our Joomla tutorial.

Leave a Reply

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

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