How to install php extension using pecl for specific php version, when several php versions installed in system?

Here's what worked best for me when trying to script this (in case anyone else comes across this like I did):

$ pecl -d php_suffix=5.6 install <package>
$ pecl uninstall -r <package>

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

$ pecl -d php_suffix=7.1 install <package>
$ pecl uninstall -r <package>

The -d php_suffix=<version> piece allows you to set config values at run time vs pre-setting them with pecl config-set. The uninstall -r bit does not actually uninstall it (from the docs):

[email protected]:~$ pecl help uninstall
pecl uninstall [options] [channel/]<package> ...
Uninstalls one or more PEAR packages.  More than one package may be
specified at once.  Prefix with channel name to uninstall from a
channel not in your default channel (pecl.php.net)

Options:
  ...
  -r, --register-only
        do not remove files, only register the packages as not installed
  ...

The uninstall line is necessary otherwise installing it will remove any previously installed version, even if it was for a different PHP version (ex: Installing an extension for PHP 7.0 would remove the 5.6 version if the package was still registered as installed).


When pecl throws error is already installed and is the same as the released version

Switch to required php, php-config, phpize versions before installing from pecl

Just run it installation with force flag

sudo pecl install -f <package-name>

I ran into this same issue while updating my Vagrant box with XHGui, as XHGui requires mongodb. I wanted to be able to support profiling on both PHP 5.6 and 7.0.

I dug into the pecl source code, and found that there's a metadata_dir config option. That is a path to a directory where the current state of installed packages. Unfortunately, that isn't already namespaced per PHP version. If you try and set it with pecl config-set, you get an opaque 'failed' error. It turns out that setting isn't whitelisted as being configuable in the \PEAR_Config class:

/**
 * Configuration values that can be set for a channel
 *
 * All other configuration values can only have a global value
 * @var array
 * @access private
 */
var $_channelConfigInfo = array(
    'php_dir', 'ext_dir', 'doc_dir', 'bin_dir', 'data_dir', 'cfg_dir',
    'test_dir', 'www_dir', 'php_bin', 'php_prefix', 'php_suffix', 'username',
    'password', 'verbose', 'preferred_state', 'umask', 'preferred_mirror', 'php_ini'
    );

In PECL's world, 'global' means it can only be set at install time, and not after.

There's an issue in the PPA tracker over at github: https://github.com/oerdnj/deb.sury.org/issues/407

The final suggestion there is to build the extension manually for alternate PHP versions. I ended up using pecl for PHP 7 extensions, and manual builds for 5.6. Make sure you run update-alternatives for php-config and phpize, and not just php before building:

update-alternatives --set php /usr/bin/php5.6
update-alternatives --set php-config /usr/bin/php-config5.6
update-alternatives --set phpize /usr/bin/phpize5.6

Then, extract the extension and build it. These steps from the above issue worked for me with the mongodb extension:

phpize5.6 && ./configure --with-php-config=php-config5.6 && make && sudo make install