Debian

How to Install PHP on Debian 12

PHP is a powerful and versatile scripting language that powers some of the biggest websites on the internet, from WordPress and Facebook to Etsy and Slack. PHP handles everything from creating a simple blog page to developing a complex E-commerce platform. PHP includes a wide range of libraries and frameworks that help you to tailor your development experience.

If you are a Debian user and are interested in web development, you should consider installing PHP on your system.

In this guide, you will learn:

How to Install PHP on Debian 12

You can install PHP on Debian 12 from:

How to Install PHP on Debian 12 from Source Repository

Debian 12 includes PHP 8.2.7 as the default choice within its official repository. This version is quite compatible with your Debian system and offers a balance in stability and performance. Besides that, it also installs several PHP extensions automatically with the default PHP package that will help you in working with the language on your system.

Before installing the PHP from the Debian repository, it is recommended to update the packages within the repository from the following command:

sudo apt update && sudo apt upgrade

After updating the repository, you can run the below-given command to install PHP on Debian 12:

sudo apt install php -y

To check PHP version on Debian 12, you can use the below-given command:

php --version

How to Remove PHP from Debian 12

If you have installed PHP from the Debian repository, you can use the following command to quickly remove it from the system:

sudo apt remove php -y

Note: You will not be able to install the latest version of PHP on Debian 12 from the repository method provided above.

How to Install PHP on Debian 12 from Official tar.gz Method

Most Debian users preferred installing the latest version of PHP because it offers a better performance that helps to load applications and web pages quickly. To install the latest version of PHP on Debian 12, you can use the official tar.gz method; the steps to perform PHP installation through this method are provided below:

Step 1: Download Latest tar.gz Source File

First, go to the official PHP website and download the latest tar.gz source file. At the time of writing this article, the latest version of PHP is 8.3.1, which you can download on Debian from the following wget command:

wget https://www.php.net/distributions/php-8.3.1.tar.gz

Step 2: Extract the tar.gz Content

You must extract the downloaded PHP tar.gz content on Debian 12, this can be done using the following command:

tar -xvzf php-8.3.1.tar.gz

Note: Ensure changing the file name in case you have downloaded another version of PHP on Debian.

Step 3: Install Dependencies on Debian 12

You must also ensure installing some dependencies on Debian from the following command, since they will be required for the configuration process:

sudo apt install cmake build-essential autoconf pkg-config bison libxml2-dev re2c libsqlite3-dev -y

Step 4: Configure Packages for PHP

Navigate to the PHP source directory on Debian 12 using the following command:

cd php-8.3.1

Then run the following command to configure packages required for installing PHP on Debian:

./configure

Step 5: Install PHP on Debian 12

After successfully configuring the packages, it’s now time to install PHP on Debian 12 from the following command:

sudo make install

Note: The make install process will take time compiling the files and installing them on your system.

Step 6: Add Path Environment on Debian 12

After the completion of the above process, add the path where PHP is installed to the system’s bashrc file. It will help the system to locate where PHP is installed.

To add path environment for PHP, first open the bashrc file using nano editor from the following command:

sudo nano ~/.bashrc

Then add the path where PHP is installed, you can find the path of PHP using the following command:

which php

By default, the process installs PHP in /usr/local/bin location, so adding the following line inside the bashrc file will allow the system to locate the PHP directory:

export PATH="$PATH:/usr/local/bin/php"

Step 7: Save the Changes

After adding the path, save the changes using CTRL+X, add Y and press Enter to exit, then use the source command to make changes to the system:

source ~/.bashrc

Step 8: Check PHP Version

After the changes, check the PHP version on Debian 12 using the following command to ensure the latest version of PHP is updated on your system:

php --version

How to Remove PHP from Debian 12 Installed Through tar.gz Method

To remove PHP from Debian 12 installed through the tar.gz method, you have to simply remove the directory from the system using the following command:

sudo rm -rf /usr/local/bin/php

How to Use PHP on Debian 12

To use PHP on Debian 12 and ensure it is working on your system, let’s create a test file using nano editor through the following command:

sudo nano testfile.php

Then add a PHP code inside the file, here I am adding a simple PHP code for printing the value on the terminal:

<?php

echo 'Hello Linux Hint Users!'

?>

Save the test file and use the php command followed by the file name to run it on Debian 12:

php testfile.php

How to Switch to Any PHP Version on Debian 12

If you have installed multiple PHP versions on Debian 12, you can switch to any PHP version using the following update-alternatives command:

sudo update-alternatives --set php /usr/bin/php<version>

Replace <version> with the PHP version you want to switch on Debian.

How to Set a Specific PHP Version as a Default One on Debian 12

You can also set a specific PHP version as your default PHP version by using the following command:

sudo update-alternatives --config php

Choose the PHP version you want to make it as a default on Debian from the list and then reboot the system to make the changes.

Conclusion

PHP is a powerful scripting language used for developing web pages and applications. You can install PHP on Debian 12 directly from the source repository, but it will not install the latest version of PHP on your system. For installing PHP latest version on Debian, you have to choose the official tar.gz method. This method may take some time in installation, but you will be able to use the PHP latest version on the system. This guide has provided detailed step-by-step instructions to install PHP on Debian 12 from both these methods. It will help you in selecting a method according to your needs so that you can start your journey in creating powerful web applications with PHP.

About the author

Awais Khan

I'm an Engineer and an academic researcher by profession. My interest for Raspberry Pi, embedded systems and blogging has brought me here to share my knowledge with others.