Raspberry Pi

How to install latest PHP on Raspberry Pi

PHP is a general-purpose language that is used in web development, also it is the reason behind interactive websites, moreover, the PHP language is not only used for writing the scripts of the websites but also to modify the data of a database, to copy, paste and close files on the server. It is also used to collect data from the users on a dynamic website and update it on the database.

PHP is compatible with different operating systems like Windows, macOS, Ubuntu, and Raspberry Pi OS, also is compatible with different servers like Apache. In this write-up, PHP’s latest version installation has been explained on Raspberry Pi.

How to install the latest PHP on Raspberry Pi

The pre-included repository of Raspberry Pi does not contain the latest release of PHP so we will not install the PHP on Raspberry Pi using the repository of Raspberry Pi. First, we will update the repository of the Raspberry Pi using the command:

$ sudo apt update

We will download the dependencies needed to install the packages of PHP on Raspberry Pi using the command:

$ sudo apt install apt-transport-https lsb-release ca-certificates wget -y

After installation of these dependencies, we will download the gpg package from https://packages .sury.org and save it to /etc/apt/trusted.gpg.d/php.gpg using the command:

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

Import the downloaded package to the Raspberry Pi repository using the command:

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

After configuring the PPA repository, we will update the Raspberry Pi repository using the apt package manager:

$ sudo apt update

We can see that 19 packages needed upgradation, so we will upgrade them using the command:

$ sudo apt full-upgrade

To install the php8.1, run the command:

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

To authenticate the installation of the latest version of the PHP on Raspberry Pi, we will check its version:

$ php -v

To display the loaded PHP modules, use the command:

$ php -m

To remove the package of PHP 8.1 from the repository of the Raspberry Pi, use the command:

$ sudo apt purge php8.1-common php8.1-cli -y

Conclusion

PHP is used in web development to create interactive dynamic websites; it is also used to take the input from the users on the websites and update the information according to the information (gathered from the users) in the databases. In this write-up, the latest PHP version has been installed by adding a PPA repository because in the repository of Raspberry Pi the 8.1 version of PHP is not available.

About the author

Hammad Zahid

I'm an Engineering graduate and my passion for IT has brought me to Linux. Now here I'm learning and sharing my knowledge with the world.