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

49,291

Solution 1

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):

vagrant@homestead:~$ 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).

Solution 2

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>

Solution 3

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

Share:
49,291

Related videos on Youtube

Oleg Abrazhaev
Author by

Oleg Abrazhaev

php, java, javascript, scala

Updated on August 05, 2020

Comments

  • Oleg Abrazhaev
    Oleg Abrazhaev almost 4 years

    I have installed both php5.6 and php7.0 from PPA on Ubuntu according to this manual

    http://lornajane.net/posts/2016/php-7-0-and-5-6-on-ubuntu

    But I didn't get how to install extensions using pecl for php5.6 or php7.0.

    For example I have already installed version of libevent or amqp in php5.6.

    Now when I type pecl install libevent and my active php version is php7.0 (using update-alternatives --set php /usr/bin/php7.0),peclreturns message thatlibevent` already installed.

    But it was installed only for php5.6 (when this version was active) and now I want to do it for php7.0.

    Which commands could help me?

    UPD

    I have found this commands for switch pecl to php7.0 and packet them to executable bash scripts:

    #!/bin/bash
    
    sudo update-alternatives --set php /usr/bin/php7.0
    
    sudo pecl config-set php_ini /etc/php/7.0/cli/php.ini
    sudo pecl config-set ext_dir /usr/lib/php/20151012/
    sudo pecl config-set bin_dir /usr/bin/
    sudo pecl config-set php_bin /usr/bin/php7.0
    sudo pecl config-set php_suffix 7.0
    

    and for php5.6

    #!/bin/bash
    
    sudo update-alternatives --set php /usr/bin/php5.6
    
    sudo pecl config-set php_ini /etc/php/5.6/cli/php.ini
    sudo pecl config-set ext_dir /usr/lib/php/20131226/
    sudo pecl config-set bin_dir /usr/bin/
    sudo pecl config-set php_bin /usr/bin/php5.6
    sudo pecl config-set php_suffix 5.6
    

    But they are not help, pecl still gives me list of already installed extensions to php5.6, even if I switched to php7.

    pecl list
    Installed packages, channel pecl.php.net:
    =========================================
    Package  Version State
    amqp     1.7.1   stable
    libevent 0.1.0   beta
    stats    1.0.3   stable
    

    It should be empty for php7.0 !

    How to solve the problem?

    UPD

    For amqp I have just installed php-amqp package without using pecl.

    apt-get install php-amqp
    

    And libevent still not exists for php7. But I hadn't found a way to switch pecl installation between 5.6 and 7 version, so question is still open.

    • Archish
      Archish over 7 years
      why you have two version of php?
    • Oleg Abrazhaev
      Oleg Abrazhaev over 7 years
      I need both for my specific needs. For example, I have 2 projects, one should run with 5.6, second one with 7. And I haven't time to migrate first one to php7.
    • ChickenFeet
      ChickenFeet almost 7 years
      Did you find any solution?
    • Oleg Abrazhaev
      Oleg Abrazhaev almost 7 years
      ChickenFeet, I think the first answer here is a way to go
    • Ejaz
      Ejaz over 4 years
      @OlegAbrazhaev Matt's answer should be accepted.
  • msbit
    msbit over 6 years
    This is the best answer in my opinion. It does, however, appear to remove the previous extension object, which is annoying.
  • Mahn
    Mahn about 5 years
    What can be done, however, is to instruct PECL to 'deregister' the extension so that it can be installed again without removing the existing one. See @Matt Zuba answer below.
  • Professor Falken
    Professor Falken almost 5 years
    Thank you! This is super useful for Debian 10, where most PHP extensions do not work for any versions < 7.3 using the deb.sury.org pacakges. That does not work out so well when upgrading old EOL Debian 8 servers, which run PHP 5.6. Using this method I can build extensions for 5.6, 7.0, 7.1, or 7.2! ++
  • Ejaz
    Ejaz over 4 years
    This helped me. pecl install kept installing mcrypt for unintended PHP version.
  • Alfchee
    Alfchee over 4 years
    Thanks a lot!!! this was very useful for a configuration I'm doing with Ansible and I was needed to use the 7.2 version
  • Lasse
    Lasse over 4 years
    Works wonders when pecl acts up after downgrading the php version through the Laravel Forge controlpanel.
  • Scott Anderson
    Scott Anderson over 3 years
    Hey, I am not so good at messing about in the core Linux file system, what if at some point in the future I wanted to actually remove & purge the php-xdebug ext for the version I am using?