How to install php-mongodb module for PHP 7 on Ubuntu 20.04


Step 1: install php7.4-dev

sudo apt install php7.4-dev

Result:

tutorialspots@tutorialspots:~$ sudo apt install php7.4-dev 
[sudo] password for tutorialspots: 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  autoconf automake autopoint autotools-dev debhelper dh-autoreconf
  dh-strip-nondeterminism dwz libarchive-cpio-perl libdebhelper-perl
  libfile-stripnondeterminism-perl libltdl-dev libmail-sendmail-perl
  libpcre2-dev libpcre2-posix2 libssl-dev libsub-override-perl
  libsys-hostname-long-perl libtool m4 php-pear php-xml php7.4-xml
  pkg-php-tools po-debconf shtool
Suggested packages:
  autoconf-archive gnu-standards autoconf-doc dh-make libtool-doc libssl-doc
  gfortran | fortran95-compiler gcj-jdk m4-doc dh-php libmail-box-perl
The following NEW packages will be installed:
  autoconf automake autopoint autotools-dev debhelper dh-autoreconf
  dh-strip-nondeterminism dwz libarchive-cpio-perl libdebhelper-perl
  libfile-stripnondeterminism-perl libltdl-dev libmail-sendmail-perl
  libpcre2-dev libpcre2-posix2 libssl-dev libsub-override-perl
  libsys-hostname-long-perl libtool m4 php-pear php-xml php7.4-dev php7.4-xml
  pkg-php-tools po-debconf shtool
0 upgraded, 27 newly installed, 0 to remove and 0 not upgraded.
Need to get 6,562 kB of archives.
After this operation, 29.0 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
...

Creating config file /etc/php/7.4/mods-available/xmlwriter.ini with new version

Creating config file /etc/php/7.4/mods-available/xsl.ini with new version
Setting up po-debconf (1.0.21) ...
Setting up libdebhelper-perl (12.10ubuntu1) ...
Setting up m4 (1.4.18-4) ...
Setting up autotools-dev (20180224.1) ...
Setting up libssl-dev:amd64 (1.1.1f-1ubuntu2) ...
Setting up autopoint (0.19.8.1-10build1) ...
Setting up libpcre2-posix2:amd64 (10.34-7) ...
Setting up autoconf (2.69-11.1) ...
Setting up dwz (0.13-5) ...
Setting up libarchive-cpio-perl (0.10-1) ...
Setting up libsub-override-perl (0.09-2) ...
Setting up libsys-hostname-long-perl (1.5-1) ...
Setting up automake (1:1.16.1-4ubuntu6) ...
update-alternatives: using /usr/bin/automake-1.16 to provide /usr/bin/automake (
automake) in auto mode
Setting up php-xml (2:7.4+75) ...
Setting up libfile-stripnondeterminism-perl (1.7.0-1) ...
Setting up php-pear (1:1.10.9+submodules+notgz-1) ...
Setting up libpcre2-dev:amd64 (10.34-7) ...
Setting up libtool (2.4.6-14) ...
Setting up libmail-sendmail-perl (0.80-1) ...
Setting up libltdl-dev:amd64 (2.4.6-14) ...
Setting up php7.4-dev (7.4.3-4ubuntu2.4) ...
update-alternatives: using /usr/bin/php-config7.4 to provide /usr/bin/php-config
 (php-config) in auto mode
update-alternatives: using /usr/bin/phpize7.4 to provide /usr/bin/phpize (phpize
) in auto mode
Setting up dh-strip-nondeterminism (1.7.0-1) ...
Setting up debhelper (12.10ubuntu1) ...
Setting up dh-autoreconf (19) ...
Setting up pkg-php-tools (1.38) ...
Processing triggers for php7.4-fpm (7.4.3-4ubuntu2.4) ...
Processing triggers for libc-bin (2.31-0ubuntu9.1) ...
Processing triggers for man-db (2.9.1-1) ...
Processing triggers for install-info (6.7.0.dfsg.2-5) ...
Processing triggers for libapache2-mod-php7.4 (7.4.3-4ubuntu2.4) ...
Warning: Could not load Apache 2.4 maintainer script helper.
Processing triggers for php7.4-cli (7.4.3-4ubuntu2.4) ...

Step 2: install module mongodb
sudo pecl install mongodb

Result:

...
creating mongodb.la
(cd .libs && rm -f mongodb.la && ln -s ../mongodb.la mongodb.la)
/bin/bash /tmp/pear/temp/pear-build-root6ArT42/mongodb-1.8.2/libtool --mode=install cp ./mongodb.la /tmp/pear/temp/pear-build-root6ArT42/mongodb-1.8.2/modules
cp ./.libs/mongodb.so /tmp/pear/temp/pear-build-root6ArT42/mongodb-1.8.2/modules/mongodb.so
cp ./.libs/mongodb.lai /tmp/pear/temp/pear-build-root6ArT42/mongodb-1.8.2/modules/mongodb.la
PATH="$PATH:/sbin" ldconfig -n /tmp/pear/temp/pear-build-root6ArT42/mongodb-1.8.2/modules
----------------------------------------------------------------------
Libraries have been installed in:
   /tmp/pear/temp/pear-build-root6ArT42/mongodb-1.8.2/modules

If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
   - add LIBDIR to the `LD_LIBRARY_PATH' environment variable
     during execution
   - add LIBDIR to the `LD_RUN_PATH' environment variable
     during linking
   - use the `-Wl,--rpath -Wl,LIBDIR' linker flag
   - have your system administrator add LIBDIR to `/etc/ld.so.conf'

See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------

Build complete.
Don't forget to run 'make test'.

running: make INSTALL_ROOT="/tmp/pear/temp/pear-build-root6ArT42/install-mongodb-1.8.2" install
Installing shared extensions:     /tmp/pear/temp/pear-build-root6ArT42/install-mongodb-1.8.2/usr/lib/php/20190902/
running: find "/tmp/pear/temp/pear-build-root6ArT42/install-mongodb-1.8.2" | xargs ls -dils
1446734    4 drwxr-xr-x 3 root root    4096 Nov 22 15:53 /tmp/pear/temp/pear-build-root6ArT42/install-mongodb-1.8.2
1572942    4 drwxr-xr-x 3 root root    4096 Nov 22 15:53 /tmp/pear/temp/pear-build-root6ArT42/install-mongodb-1.8.2/usr
1572943    4 drwxr-xr-x 3 root root    4096 Nov 22 15:53 /tmp/pear/temp/pear-build-root6ArT42/install-mongodb-1.8.2/usr/lib
1572944    4 drwxr-xr-x 3 root root    4096 Nov 22 15:53 /tmp/pear/temp/pear-build-root6ArT42/install-mongodb-1.8.2/usr/lib/php
1572945    4 drwxr-xr-x 2 root root    4096 Nov 22 15:53 /tmp/pear/temp/pear-build-root6ArT42/install-mongodb-1.8.2/usr/lib/php/20190902
1572946 8604 -rwxr-xr-x 1 root root 8806440 Nov 22 15:53 /tmp/pear/temp/pear-build-root6ArT42/install-mongodb-1.8.2/usr/lib/php/20190902/mongodb.so

Build process completed successfully
Installing '/usr/lib/php/20190902/mongodb.so'
install ok: channel://pecl.php.net/mongodb-1.8.2
configuration option "php_ini" is not set to php.ini location
You should add "extension=mongodb.so" to php.ini

Step 3: create file /etc/php/7.4/cli/conf.d/30-mongodb.ini

with content: extension=mongodb.so

We can do this job with these command lines:
sudo chown tutorialspots:tutorialspots /etc/php/7.4/cli/conf.d
sudo echo extension=mongodb.so > /etc/php/7.4/cli/conf.d/30-mongodb.ini

Do the same with file /etc/php/7.4/fpm/conf.d/30-mongodb.ini
sudo echo extension=mongodb.so > /etc/php/7.4/fpm/conf.d/30-mongodb.ini

Step 4: restart php-fpm
sudo service php7.4-fpm restart

Done, Verify:

tutorialspots@tutorialspots:~$ php -m
[PHP Modules]
calendar
Core
ctype
date
dom
exif
FFI
fileinfo
filter
ftp
gettext
hash
iconv
json
libxml
mongodb
openssl
pcntl
pcre
PDO
Phar
posix
readline
Reflection
session
shmop
SimpleXML
sockets
sodium
SPL
standard
sysvmsg
sysvsem
sysvshm
tokenizer
xml
xmlreader
xmlwriter
xsl
Zend OPcache
zlib

[Zend Modules]
Zend OPcache

Leave a Reply