Install PHP 8.1 on Raspberry Pi

PHP is a programming language that commonly used to create a web applications and dynamic websites. With each release of PHP, new features and various changes are introduced.

This tutorial shows how to install PHP 8.1 on Raspberry Pi.

Install PHP

Connect to Raspberry Pi via SSH and execute command to download GPG key:

sudo wget -qO /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg

Add PHP repository:

echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/php.list

Update the package lists:

sudo apt update

Next, install PHP 8.1 with command line interface (CLI):

sudo apt install -y php8.1-common php8.1-cli

Check PHP version when installation was finished:

php --version

There are various PHP extensions that provides additional functionality. PHP extensions can be installed using the following syntax:

sudo apt install -y php8.1-extension_name

Execute the following command to install commonly used PHP extensions:

sudo apt install -y php8.1-curl php8.1-gd php8.1-mbstring php8.1-xml php8.1-zip

We can use -m option to check what extensions are installed.

php -m

PHP integration with MySQL or MariaDB

In order to use PHP with MySQL or MariaDB database we need to install the following extension:

sudo apt install -y php8.1-mysql

PHP integration with Apache

If we want to integrate PHP with Apache HTTP server then install the following extension:

sudo apt install -y libapache2-mod-php8.1

Once installation was completed restart Apache:

sudo service apache2 restart

Testing PHP

Create a new main.php file:

nano main.php

Add the following code:

<?php

echo 'Hello world';

Run the following command to test a script:

php main.php

Uninstall PHP

If you want to completely remove PHP anything related to it, execute the following command:

sudo apt purge --autoremove -y php-common mime-support

Remove GPG key and repository:

sudo rm -rf /etc/apt/trusted.gpg.d/php.gpg
sudo rm -rf /etc/apt/sources.list.d/php.list

Remove PHP related file:

sudo rm -rf /var/lib/systemd/timers/stamp-phpsessionclean.timer

Install older versions

PHP 8.0 is older version that is still supported. It can be installed by changing php8.1 to php8.0 in this post presented commands.

The 8 Comments Found

  1. BlackSenator Reply

    Is there a hardware (CPU) requirement? I still had a Pi (1B) that I wanted to use for a simple script (CLI).
    I have tried several times to install PHP 8 based on the current Raspbian OS lite (Buster). When I called `php -v` at the end I got` illegal instruction`. PHP 7.3 runs fine on it, with 7.4 I get the same error as with 8.0.

    • lindevs Reply

      Hi BlackSenator,
      Raspberry Pi 1 Model B has an ARMv6 compatible CPU. The deb.sury.org repository is not supporting ARMv6. So you’ll need at least ARMv7-A compatible CPU.

    • lindevs Reply

      Hi steinmb,
      Right now I unable to find precompiled PHP 8.0 binaries that compatible with ARMv6 architecture. You can try to install PHP from source code. However, compilation can take a while.

      • steinmb Reply

        Thanks for your swift reply: I did try to build but I did run out of memory. Perhaps I could try again but make sure I have a rather large swap disk enabled? Anyone know why deb.sury.org do not build for ARMv6? I mean, not even PHP 7.4.x is available.

    • lindevs Reply

      The deb.sury.org repository contains packages for Debian. The Debian armhf port needs at least ARMv7 compatible CPU. This means that Debian armhf packages can be installed on Raspberry Pi with ARMv7 CPU. Raspberry Pi 1 and Zero has ARMv6 CPU that is incompatible with the Debian armhf port. The deb.sury.org repository don’t provide packages for Debian armel (supports ARMv6 CPU). I gues that BlackSenator installed PHP 7.3 from official Raspbian repository and not from deb.sury.org. You need to increase the swap memory on Raspberry Pi in order to compile PHP 8.0.

  2. Gary Reply

    I am doing a test install of Buster on my model3b Pi. Everything seems to be working (apache2, php8, mariadb). However, I am unable to install phpmyadmin. At the end of the install, I get ERROR 1045 (28000): Access denied for user ‘root’@’localhost’ (using password: NO). All my attempts to get around this failed. I have remote tools for database management and they connect.
    I’m not sure that I need phpmyadmin, but I use it on my other pi computers.
    Is this a compatibility issue that can not currently be solved?

    • lindevs Reply

      Hi, Gary
      The phpMyAdmin available on official Raspbian repository is out of date and not compatible with PHP 8.0. Right now the Raspbian repository for Buster contains phpMyAdmin 4.6.6. So if you tried to install phpMyAdmin via APT package manager you got errors. You’ll have to install phpMyAdmin manually. Don’t forget to remove a previous version.

Leave a Comment

Your email address will not be published.