How to Install PHP 8 on Debian 12 Step-by-Step

This guide explores various ways of installing PHP 8 on Debian 12, codenamed Bookworm, which is the latest Debian release.

PHP is an open-source and widely used server-side scripting language often used in web development as a backend technology to execute server-side scripts.

A recursive acronym for PHP: Hypertext Preprocessor, PHP is commonly used in web development software stacks such as LAMP (Linux Apache MySQL and PHP) and LEMP (Linux Nginx MySQL and PHP ) to support dynamic websites.

PHP was initially released in 1995, and the current stable version is PHP 8.2. It is powered by Zend Engine, a popular runtime for PHP scripting engine. As previously hinted, PHP is mostly used in web servers and is mostly embedded in HTML syntax to parse PHP scripts.

Option 1: Install PHP 8 on Debian 12 from Default Repositories

The default Debian repositories provide PHP 8.2 which is the current stable version of PHP. To install it, log into your Debian system via SSH and update the package lists as shown.

$ sudo apt update

Next, run the command below to install PHP 8.2 with associated new packages and dependencies.

$ sudo apt install php

Apt-Install-php-Debian12

When prompted to continue, type ‘y’ and press ENTER to continue. Once installed, verify that PHP is installed as shown.

$ php -v

The output below confirms that we have successfully installed PHP 8.2.7.

php-command-check-version-debian

To install PHP extensions, which extend the functionality of PHP, use the following syntax:

$ sudo apt install php-{extension1, extension2}

For example, to install a few popular PHP extensions, run the following code:

$ sudo apt install php-{curl,mbstring,zip,xml,soap,cli}

Installing-php-extension-apt-command-debian

Option 2: Install PHP 8 on Debian 12 using Sury PHP PPA

The Ondrej Sury PPA is a repository maintained by a Debian developer called Ondrej Sury. It provides the latest PHP versions and is the go-to repository if you want to install the latest PHP version on your server.

In this section, we will install the latest PHP version which is PHP 8.3 at the time of writing this guide.

To add the repository, import the GPG signing key using wget command.

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

Next, add the Sury PHP PPA repository.

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

Once added, update the package lists in order to sync the PPA with the local package index.

$ sudo apt update

Apt-Update-Post-Adding-Sury-PHP-Repo-Debian

To install PHP 8.3, run the following command

$ sudo apt install php8.3

Apt-Install-PHP-8-3-Debian12

Once again, press ‘Y’ when prompted and hit ENTER. To confirm that PHP 8.3 was installed, once again run the command:

$ php -v

Verify-PHP-Version-Post-Installation-Debian12

Another way of verifying that PHP is installed is by creating an info.php file in the document root

$ sudo nano /var/www/html/info.php

Add the following lines of PHP code

<?php
phpinfo();
?>

Save the changes and exit the file. On your browser, visit the following URL

http://server-ip/info.php

You should see the following PHP info page.

PHP-Info-Page-Debian12

And that’s it. Those are two methods that you can use to install PHP 8 on Debian 12.

Conclusion 

In this guide, we have installed PHP 8 on Debian 12 both from the default repository and Sury PPA which packs with the latest PHP versions. We hope that this tutorial was insightful. Consider sharing it with your friends on various platforms.

Leave a Comment

eleven + 19 =