dmvrtx

PHP 7.4 on Debian Bookworm

Debian 12, or Bookworm, comes with PHP 8.2, which is a great way forward. However, I had to deal with the project that for the time being is stuck with PHP 7.4. Since I’m using VMs for my project work I decided to create one with Debian 12 and PHP 7.4.

Ondřej Surý does an amazing job of keeping a repository with main PHP versions for Debian based systems. For Ubuntu systems PPA approach is a no-brainer, but for Debian some manual work is required to make things work.

First step would be to store a GPG key for repository and add it to the list of apt sources. My preference is to store keys in /etc/apt/keyrings but you can choose another location.

apt-get update
apt-get -y install lsb-release ca-certificates curl
curl -sSLo /etc/apt/keyrings/deb.sury.org-php.gpg https://packages.sury.org/php/apt.gpg
sh -c 'echo "deb [signed-by=/etc/apt/keyrings/deb.sury.org-php.gpg] https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list'
apt-get update

At this point PHP 7.4 can be installed. But apt would also upgrade other packages if they happen to have a higher version in the newly added repository. My preference is to let apt know that I only want to retrieve PHP packages and everything else should come from Debian repositories. This can be achieved with apt pinning.

Let’s tell apt to prefer Debian repositories by default. Create a /etc/apt/preferences.d/stable with the following contents:

Package: *
Pin: release o=Debian
Pin-Priority: 900

However, php-common package from Debian repositories will block installation of PHP 7.4 from Surý’s repository and we need to set pinning for it. At the same time I’d like to receive PHP 8.2 from Debian sources if I need it. I will use the following rules and save them as /etc/apt/preferences.d/php:

Package: php8.2*
Pin: release o=Debian
Pin-Priority: 900

Package: php-*
Pin: origin "packages.sury.org"
Pin-Priority: 900

Package: php7.4*
Pin: origin "packages.sury.org"
Pin-Priority: 900

On my VM I’ve removed PHP 8.2 and had only PHP 7.4 installed, but it is possible to keep both and use update-alternatives command to switch the system’s default version.