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

Raspberry Pi

Micro SD Card

Power Supply

Ethernet Cord or Wi-Fi

Optional

HDMI Cord

USB Keyboard

USB Mouse

Raspberry Pi Case

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

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

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

4. 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 script by using the following command within the terminal

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

This script is super simple and will print out the words “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.

Leave a Reply

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