Install PECL Extensions for a Specific PHP Version

When a server has only one PHP version installed, installing a PECL extension is usually straightforward:

sudo pecl install redis

The situation becomes less obvious when the same machine has several PHP versions installed. A server may have PHP 7.4, 8.1, 8.2 and 8.3 side by side, while the plain pecl install command builds against whichever PHP environment the current PECL configuration points to.

If you need an extension for one specific PHP version, you should explicitly select the PHP toolchain used to compile and load that extension.

The PECL php_suffix Method

On systems configured with versioned PHP binaries, PECL can be invoked with a temporary php_suffix configuration value.

sudo pecl -d php_suffix=5.6 install <package>
sudo pecl -d php_suffix=7.0 install <package>
sudo pecl -d php_suffix=7.1 install <package>

The important part is:

-d php_suffix=<version>

The -d option temporarily sets a PEAR/PECL configuration value for that command. In practice, php_suffix is useful on installations where PHP-related commands are versioned, such as php7.1, phpize7.1 and php-config7.1.

Example: Install Redis for a Specific PHP Version

sudo pecl -d php_suffix=7.1 install redis

The same idea can be used for extensions such as Redis, Xdebug, MongoDB, Memcached, Imagick or PCOV, provided that the extension release you install still supports the target PHP version.

This compatibility check matters especially for old PHP branches. A current extension release may no longer compile against PHP 5.x or an early PHP 7.x release.

Uninstall Before Reinstalling

The original workflow may use:

sudo pecl uninstall -r <package>
sudo pecl -d php_suffix=7.0 install <package>

Be careful on a server where the same extension is already used by another PHP version. Do not blindly uninstall a working module until you know which PHP installation owns it.

First Check Which PHP Versions Are Installed

php -v
which php
ls -1 /usr/bin/php*

On Ubuntu or Debian with several PHP versions installed you may see binaries similar to:

/usr/bin/php7.4
/usr/bin/php8.2
/usr/bin/php8.3
/usr/bin/phpize8.2
/usr/bin/phpize8.3
/usr/bin/php-config8.2
/usr/bin/php-config8.3

If the machine uses update-alternatives, you can inspect the currently selected CLI PHP with:

update-alternatives --display php

You generally do not need to change the system-wide default PHP just to compile one extension.

More Explicit Method: phpize + php-config

When several PHP versions coexist, the clearest method is to build the extension using the phpize and php-config belonging to the target version.

For PHP 8.3, install the development package first:

sudo apt install php8.3-dev

Download and extract the PECL source:

pecl download <package>
tar -xf <package>-*.tgz
cd <package>-*

Prepare the build system with the target PHP version:

phpize8.3

Then explicitly configure the build against PHP 8.3:

./configure --with-php-config=/usr/bin/php-config8.3
make
sudo make install

What phpize Does

phpize prepares the build environment required to compile an external PHP extension. Using phpize8.3 ensures that the generated build files use the PHP 8.3 development environment.

What php-config Does

php-config exposes information about a particular PHP installation. For example:

php-config8.3 --version
php-config8.3 --extension-dir

Using --with-php-config=/usr/bin/php-config8.3 removes ambiguity and tells the extension exactly which PHP installation it should build against.

Enable the Extension

A successful compilation does not always mean that PHP is already loading the module.

For example, after installing Redis for PHP 8.3:

php8.3 -m | grep -i redis
php8.3 --ri redis

If the module is not enabled, Ubuntu and Debian commonly use a version-specific configuration directory such as:

/etc/php/8.3/mods-available/

You can create an INI file and enable it for the selected PHP version:

echo "extension=redis.so" | sudo tee /etc/php/8.3/mods-available/redis.ini
sudo phpenmod -v 8.3 redis

CLI and PHP-FPM Are Separate

Testing the CLI does not automatically prove that PHP-FPM is loading the same module.

php8.3 --ini
sudo systemctl restart php8.3-fpm
sudo systemctl status php8.3-fpm

When multiple PHP-FPM versions are running, restart only the service that corresponds to the PHP version you changed.

Find Where the Extension Was Installed

php-config8.3 --extension-dir
php8.3 -i | grep extension_dir

The compiled module will normally appear as a .so file inside that extension directory.

Verification Checklist

php8.3 -v
php-config8.3 --version
php-config8.3 --extension-dir
php8.3 -m | grep -i <extension>
php8.3 --ri <extension>

Quick Reference

PECL suffix method:

sudo pecl -d php_suffix=7.1 install <package>

Explicit build for PHP 8.3:

phpize8.3
./configure --with-php-config=/usr/bin/php-config8.3
make
sudo make install

Enable and verify:

sudo phpenmod -v 8.3 <extension>
php8.3 --ri <extension>

Final Notes

When multiple PHP versions are installed, the important question is not simply whether pecl install succeeded. You need to know which PHP version compiled the extension, where the resulting module was installed, and which PHP configuration loads it.

For older version-aware PECL installations, php_suffix can be convenient. For newer or more complex multi-PHP environments, explicitly selecting the matching phpize and php-config gives you much clearer control.