How to Install PHP 8.3 on Debian 12

Choose a different version or distribution

Introduction

Before we discuss how to install PHP 8.3 on Debian 12, let's first understand-What is PHP?

PHP is a popular server-side scripting language used for web development. PHP 8.3 is the latest version with improved performance and several new features. Installing PHP 8.3 on Debian 12 will allow you to benefit from the most recent improvements and guarantee that it works with modern web applications.

PHP 8.3 is the primary upgrade for PHP in 2023. Along with a few other updates, deprecations, and new features, it includes typed class constants, a new set of Exceptions in the DateTime extension, and a new json_validate function.

This tutorial will cover the installation process of PHP 8.3 on Debian 12. We will also address few FAQs to install PHP 8.3 on Debian 12.

Advantages

  1. Improved Performance: PHP 8.3 introduces various performance optimizations, resulting in faster execution of PHP scripts. This can enhance the overall speed and responsiveness of your web applications.
  2. New Features and Syntax: PHP 8.3 introduces several new features, syntax improvements, and language enhancements. You can leverage these additions to write cleaner and more efficient code, making development easier and more enjoyable.
  3. Enhanced Security: PHP 8.3 includes security enhancements and fixes for known vulnerabilities. By upgrading to this version, you can ensure your web applications benefit from the latest security patches, keeping them protected from potential threats.
  4. Compatibility: PHP 8.3 maintains backward compatibility with previous versions, ensuring that your existing PHP code and applications will continue to work as expected. You can take advantage of the new features without worrying about breaking existing functionality.
  5. Community Support and Updates: PHP has a large and active community of developers who regularly contribute to its development, provide support, and release updates. By installing PHP 8.3 on Debian 12, you can benefit from ongoing community support and receive timely updates to keep your applications up to date.

Steps to Install PHP 8.3 on Debian 12

Run these commands in the terminal as a user with sudo capability:

# Save existing php package list to packages.txt file
sudo dpkg -l | grep php | tee packages.txt

# Add Ondrej's repo source and signing key along with dependencies
sudo apt install apt-transport-https
sudo curl -sSLo /usr/share/keyrings/deb.sury.org-php.gpg https://packages.sury.org/php/apt.gpg
sudo sh -c 'echo "deb [signed-by=/usr/share/keyrings/deb.sury.org-php.gpg] https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list'
sudo apt update

# Install new PHP 8.3 packages
sudo apt install php8.3 php8.3-cli php8.3-{bz2,curl,mbstring,intl}

# Install FPM OR Apache module
sudo apt install php8.3-fpm
# OR
# sudo apt install libapache2-mod-php8.3

# On Apache: Enable PHP 8.3 FPM
sudo a2enconf php8.3-fpm
# When upgrading from an older PHP version:
sudo a2disconf php8.2-fpm

# Remove old packages
sudo apt purge php8.2*
ℹ️
Note: The next set of operations needs that level of permissions to proceed and are performed as sudo. Here, too, the standard cautions that accompany any system-wide modification are applicable. Prior to proceeding, make sure you backup the system and verify the backups.

1. Pre-requisites

PHP 8.3 is not available in any of the current Ubuntu and Debian versions' default software repositories. This tutorial uses a repository kept up to date by Ondřej Surþ to obtain prebuilt PHP packages. The PHP packages provided by the OS software repositories are identical to the packages in this repository in terms of package names, package configuration, and systemd configuration.

2. List and keep note of existing PHP packages

The following program shows all installed packages containing the word php in the package name when upgrading an existing PHP version. It saves the list to a packages.txt file and prints it to the terminal.

Installing the required PHP 8.3 packages in the following steps will be made easier using this.

When installing PHP on a new system, you do not need to perform this step.

dpkg -l | grep php | tee packages.txt

3. Add ondrej/php repository

There isn't a Debian software repository that currently offers PHP 8.3 for download. Updated Debian software repositories, managed for by Ondřej Surþ, include the primary PHP 8.3 packages together with a number of well-liked PECL packages.

In order to retrieve a list of packages that are available from both the new repository and the existing repositories, the following commands add the repository to the list of software repositories and install it.

sudo apt install apt-transport-https
sudo curl -sSLo /usr/share/keyrings/deb.sury.org-php.gpg https://packages.sury.org/php/apt.gpg
sudo sh -c 'echo "deb [signed-by=/usr/share/keyrings/deb.sury.org-php.gpg] https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list'
sudo apt update

4. Install New PHP 8.3 packages

Apt can now install PHP packages since Ondrej's PHP repository was added. Every PHP 8.3 package adheres to the php8.3-XYZ format. The PHP versions supplied by the OS's own software repositories are matched by these names and take precedence over them.

sudo apt install php8.3-common php8.3-cli php8.3-fpm php8.3-{curl,bz2,mbstring,intl}
  • A meta-package called php8.3-common installs multiple PHP extensions. Later on, you can disable specific extensions one at a time. Included at all times are the PHP Core extensions Date, Phar, JSON, ctype, and random. It is neither feasible or required to install them individually.
  • It is also possible to install specific packages in place of php8.3-common. Installing php8.3-common is similar to installing every extension, as indicated below:
apt install php8.3-{calendar,ctype,exif,ffi,fileinfo,ftp,gettext,iconv,pdo,phar,posix,shmop,sockets,sysvmsg,sysvsem,sysvshm,tokenizer}
  • Installing php8.3-cli also creates a symlink from /usr/bin/php to /usr/bin/php8.3. For more details, see Running PHP 8.3 Alongside Other Versions.
  • Installing PHP's FPM SAPI for web server integration is done with php8.3-fpm. Refer to Web Server Integration for other methods.

5. Additional PHP Extensions

The following are some of the PECL extensions and their package names to be used with apt install to install.

Extension name Package name
Xdebug php8.3-xdebug
Redis php8.3-redis
PCov php8.3-pcov
Image Magick php8.3-imagick
APCu php8.3-apcu

The following runs a simple search on apt:

sudo apt search php8.3-apcu 
//Search for "apcu" under PHP 8.3

The packages.txt file created in step 2 should come handy here, to refer to the existing PHP packages.

6. Web Server Integration

PHP is combined with a web server in the majority of use cases. The most popular method of integration is using PHP-FPM over a Fast CGI interface, however PHP can also be integrated with other SAPIs.

Apache web server

If the Apache web server (apache2) is installed, a new php8.3-fpm.conf file will be created after the installation of the php8.3-fpm package. This file allows you to easily toggle PHP 8.3 integration:

sudo a2enconf php8.3-fpm
sudo a2disconf php8.2-fpm  # When upgrading from an older PHP version
sudo systemctl restart apache2

When Apache is configured to run PHP as an Apache module (commonly called as mod_php or mod_php8), install libapache2-mod-php8.3 package instead ofphp8.3-fpm:

sudo apt install libapache2-mod-php8.3
sudo a2enmod php8.3
sudo a2dismod php8.2 # When upgrading from an older PHP version
sudo systemctl restart apache2

Nginx, Caddy, Litespeed, and other servers over Fast CGI

The php8.3-fpm script installs PHP-FPM and creates a systemd service at /run/php/php8.3-fpm.sock to handle PHP 8.3 FPM.

In order to integrate PHP with web servers that use Fast CGI, modify or adjust the UNIX socket path to point to this address.

For example, on Nginx, this involves changing the fastcgi_pass directive:

 fastcgi_pass unix:/run/php/php8.1-fpm.sock;
 fastcgi_pass unix:/run/php/php8.2-fpm.sock;

7. Test PHP 8.3 Installation

The crucial opportunity to determine whether the fresh installation was successful arrives after installing every package required.

When you type php -v into the terminal, the following should appear:

PHP -m can be used to list the modules in the list. There will be multiple packaged PHP extensions in this list. For information on moving PHP INI directives and enabled extensions to the latest PHP versions, see Migrate Configuration.

8. Migrate Configuration

This step is only necessary when upgrading from an earlier version of PHP to PHP 8.3.

The new PHP 8.3 installation's configuration files are located in the /etc/php/8.3 directory. The /etc/php directory should also contain any installed instances of PHP.

💡
NOTE: Do not copy existing PHP INI files to /etc/php/8.3. Instead, compare two corresponding PHP INI files and update the PHP 8.3 variant only when necessary.

When utilizing PHP-FPM, make sure to replicate the correct amount of FPM processes and process models.

For PHP modules, continent toggles are provided by the scripts phpenmod and phpdismod. For instance, with PHP 8.3, the following disables the phar extension for FPM:

sudo phpdismod -v 8.3 -s fpm phar 

After making changes, restart PHP 8.3-FPM:

sudo systemctl restart php8.3-fpm

9. Remove PHP Old Versions

Use the PHP version prefix when running apt purge to get rid of outdated PHP versions. For instance, the packages and configuration for PHP 8.2 are removed by doing the following:

sudo apt purge php8.2*

Running PHP 8.3 Alongside other versions

PHP 8.3 can be installed in addition to other PHP versions. In actuality, this is what occurs when PHP 8.3 is installed without first deleting outdated PHP packages.

All phpX.Y-cli packages install the PHP CLI binary by default to the /usr/bin/phpX.Y directory. This implies that, for PHP 8.3 installations, the last PHP-CLI binary (PHP 8.3 in this example) will be used by any application that invokes PHP CLI as php. When using various PHP versions, make sure to launch the correct PHP binary by directly calling the /usr/bin/phpX.Y executable. /usr/bin/php8.3 can be used to immediately launch PHP 8.3.

sudo update-alternatives --config php

This displays an interactive popup where users can choose the different PHP binary path that PHP is pointing to.

There are 2 choices for the alternative php (providing /usr/bin/php).

  Selection    Path             Priority   Status
------------------------------------------------------------
* 0            /usr/bin/php8.3   83        auto mode
  1            /usr/bin/php8.2   82        manual mode
  2            /usr/bin/php8.3   83        manual mode

To set the path without the interactive prompt:

update-alternatives --set php /usr/bin/php8.2

FAQs to Install PHP 8.3 on Debian 12

How can I install PHP 8.3 on Debian 12 using the official repositories?

As of now, PHP 8.3 may not be available directly from the official Debian 12 repositories. You can refer to external repositories like Ondřej Surý's PPA or Remi's RPM repository for PHP 8.3.

Can I install PHP 8.3 on Debian 12 using a package manager like apt?

Yes, you can use package managers like apt to install PHP 8.3 by adding external repositories that provide PHP 8.3 packages. However, exercise caution and make sure the repositories are trustworthy.

Is it possible to install PHP 8.3 from source on Debian 12?

Yes, you can install PHP 8.3 from source on Debian 12. This requires obtaining the PHP source code, configuring it with the necessary flags, compiling, and installing it manually. It is recommended for advanced users or specific use cases.

Does installing PHP 8.3 on Debian 12 automatically replace older PHP versions?

Installing PHP 8.3 on Debian 12 does not automatically replace older PHP versions. You can have multiple PHP versions coexist on your system, but you need to configure your web server to use the desired version.

What modules and extensions are compatible with PHP 8.3?

PHP 8.3 maintains backward compatibility with most PHP extensions and libraries. However, some older extensions may need to be updated or replaced to work with PHP 8.3. It's always recommended to check the compatibility of your dependencies.

How do I configure PHP 8.3 on Debian 12 to work with Apache or Nginx?

To configure PHP 8.3 on Debian 12 to work with Apache or Nginx, you need to install PHP as a module or a FastCGI process manager and configure the web server accordingly.

Can I use PHP 8.3 alongside other programming languages on Debian 12?

Yes, you can use PHP 8.3 alongside other programming languages on Debian 12. You can configure your web server to handle multiple programming languages simultaneously, allowing you to build complex web applications.

Conclusion

While PHP 8.3 may not be available directly from the Debian 12 repositories, you can use external repositories or compile PHP from source to install it. In this tutorial, we have covered the procedures to install PHP 8.3 on Debian 12.

We would be happy to answer any questions you may have, so please feel free to ask them in the comments section.