Installing and maintaining a specific PHP version release

If you require a specific PHP version and want it kept up to date, but not just receive the current release that gets updated when PHP new releases occur (for example the release of PHP 8.2 occurs when you want to stay on PHP 8.1) it is required that, when you install PHP and any modules, you only install version specific packages and not the generic packages. For example, most packages starting php- (e.g. php itself, and php-mbstring) are generic and will always default to the latest release, whereas selecting php8.1-cli, php8.1-fpm, php8.1-mbstring will install (and update) the specific 8.1 release (and in the case of PHP itself, the specific CLI, FPM, etc version of same).

Using this method will ensure that critical installs, e.g. to servers, are not suddenly replaced with a new version of PHP that may contain breaking changes or cause unexpected failures (e.g. a package not being available for your new release or an independent package that needs recompiling e.g. through PECL).

Also, this means that you do not need to try pinning PHP and that you are well set up to support multiple PHP versions on your system (e.g. to allow website installs to point to specific php-fpm versions).

Switch to desired version

Use update-alternatives to switch /usr/bin/php to your desired PHP version:

    1. switch to specific version update-alternatives --set php /usr/bin/php5.6, or
    2. update-alternatives --config php configure the version by hand

Swapping from meta-packages to version specific packages

Use your package manager to search for php-* packages – you will need to remove these for any you have installed outside of the PHP base. This includes PHP itself – install the version specific (apt install php8.1-cliapt-install php8.1-fpm, don’t forget to install the apache module if you need it also version specific).

For each library that you identify, use your package manager to remove it (but don’t purge – you want to keep your settings), and immediately add back the version-specific library, e.g.

Debian

apt remove php-mbstring
apt install php8.1-mbstring

Another way to check if you have libraries remaining that need to be version fixed is to do an update and upgrade (but don’t actually permit it to install yet) or list upgraded packages and see which are going to be version pushed to a later release (i.e. a 7.4 or 8.1 package now being pushed to an 8.2 – that will be from the meta package install).

Caveat: php-common appears to be installed and updated no matter what and has not been seen to be an issue.

To double-check that you’ve covered everything, do a php[version] -m to list the modules installed before you start the removal, and compare with the php[version] -m list when you are finished – e.g. php8.1 -m. This is also a good technique to compare during upgrading, e.g. php7.4 -m compared to php8.1 -m – make sure you’ve got everything installed for the new release.

December 9th, 2023 | Posted in Debian, Linux |

Comments are closed.