Raspberry Pi

How to Install and Run Latest PHP on Raspberry Pi

PHP is an open-source general-purpose scripting language used for creating web pages and applications. It can easily integrate with multiple databases like MySQL, Oracle, PostgreSQL, Informix, Microsoft SQL Server, and Sybase. Besides integration, it supports many major protocols, such as LDAP, POP3, and IMAP.

If you are interested in installing the latest version of PHP on Raspberry Pi, follow this guide for further details.

How to Install Latest PHP Version on Raspberry Pi

To install the latest version of PHP on Raspberry Pi, follow the steps, which are mentioned below:

Step 1: Update the Repository

Before installing any new package, it is better to update the Raspberry Pi repository by using the below-written command:

$ sudo apt update

 

If the repository isn’t updated, you can use the following command to upgrade it:

$ sudo apt upgrade

 

Step 2: Download the GPG key

In the Raspberry Pi system, the latest version of PHP can be installed using the third-party repository and to use this repository, you must import a GPG key using the below-mentioned command:

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

 

Step 3: Add PHP Repository

After successfully adding the GPG key, you can add the PHP repository using the following command:

$ 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

 

Step 4: Update Repository

After adding the PHP repository, you must update the system repository through the below-given command:

$ sudo apt update

 

In case of upgrade, you can use the following command:

$ sudo apt upgrade

 

Step 5: Install PHP on Raspberry Pi

After the update, you can apply the following command to install the PHP latest version on your Raspberry Pi system:

$ sudo apt install php

 
Enter “Y” when asked for permission to continue.


You can confirm the installation of PHP latest version on your system through the following command:

$ php --version

 

In the output it can be clearly seen that the latest version of PHP is installed on the system, which is 8.1.13.

Run a PHP Code

To verify if PHP is correctly installed or not, let’s run a simple code. To do it, we are adding a “Hello” message inside the PHP file through the following command.

$ echo "<?php echo 'Hello Linux-hint';” > my_file.php

 
You can replace the message ‘Hello Linux-hint’ with your own. The output will be stored in a new file named “my_file.php”:


Or open nano editor and create a PHP file by using the below-mentioned code:

$ sudo nano my_file.php

 

Then you can type any PHP code of your choice inside this file:


Then run the file using the below-mentioned command:

$ php my_file.php

 
The output will be displayed on the screen.

Conclusion

PHP is a server-side language used for web development. You can install the latest version of PHP on the Raspberry Pi system using a third-party repository. However, before that, you must add a GPG key to the system. Later on, you can add the repository and run the update command to update the changes. Once the repository is updated, you can successfully install the latest version of PHP on the Raspberry Pi system through the “apt” command.

About the author

Zahra Zamir

An Electronics graduate who loves to learn and share the knowledge, my passion for my field has helped me grasp complex electronics concepts and now I am here to share them with others.