在ubuntu 16.04上为php5.6安装GNU PG(GPG)

I have 2 versions of php installed on my ubuntu php5.6 and php7. 5.6 is set as active version. Now I am trying to install GPG using below line of code, and it installs it under /etc/php/7.0 directory.

sudo apt-get install php-gnupg

How to install it for php5.6?

Extension dir for php 5.6 is /usr/lib/php/20131226 and extension dir for php 7.0 is /usr/lib/php/20151012 as this command shows:

php -r "print phpinfo();" | grep "extension_dir"

Pecl installs gnupg in /usr/lib/php/20131226/gnupg.so because the pecl was installed when php 5.6 is enabled

pecl list-files gnupg

Conclusion: PHP 7.0 uses a different extension directory than where the gnupg is installed.

First try which didn't work: Create symlink for gnup.so inside the php 7.0 extension directory that points to gnup.so inside php 5.6

sudo ln -s /usr/lib/php/20131226/gnupg.so /usr/lib/php/20151012/gnupg.so

Results in:

Warning: PHP Startup: gnupg: Unable to initialize module
Module compiled with module API=20131226
PHP    compiled with module API=20151012

Second try which also didn't work:

  1. Uninstall pecl extension: sudo pecl uninstall gnupg
  2. Activate php v 7.0
  3. Install gnupg again: sudo pecl install gnupg

Gives same compiling error.

Final solution:

Install a compiled version of the gnupg that works with php 7.0: see php docs here

Check if gnupg is installed

php -r 'var_dump(function_exists("gnupg_decrypt"));';