10

I'm trying to install the mongoDB-extension for php7. I started with a brand new installation of Ubuntu-Server 16.04.1. Pre-installed (while OS-installation) packages were LAMP-extension, OpenSSL and MySQL-server. This was installed on a local VirtualBox. It shall to be a development system which is configured equal for every developer.

First I installed MongoDB itself according to these instructions: install mongoDB on Ubuntu

After that, according to this second instruction: installing php7-mongoDB-client-driver,

I installed

  • php-pear
  • phpize

It can be recapped in

$sudo apt-get install php-pear phpize

because these two packages were flagged as missing when using

sudo pecl install mongodb

the first time.

Then I was suggested to install openssl-extension. About that I found an instruction which told me to execute these 3 commands:

  • $sudo apt-get install libssl-dev
  • $sudo apt-get install libcurl4-openssl-dev
  • $sudo apt-get install libcurl4-openssl-dev pkg-config libssl-dev libsslcommon2-dev

(the first package was already installed on my system)

after that

$php -m

should show "openssl" as a single point on the list.

Then I could finally execute

$sudo pecl install mongodb

This installation displayed four instructions at the end:

<Installing '/usr/lib/php/20151012/mongodb.so'>
<install ok: channel://pecl.php.net/mongodb-1.1.8>
<configuration option "php_ini" is not set to php.ini location>
<You should add "extension=mongodb.so" to php.ini>

So I executed:

$php -i | grep extension_dir

which should show up the same path as displayed in the first line.

$pear config-set php_ini /etc/php5/apache2/php.ini
$pecl config-set php_ini /etc/php5/apache2/php.ini

to follow the instructions of the third line.

Added extension=mongodb.so to phpini at /etc/php/7.0/apache2/ to conform to line four.

And finally, after restarted apache2,

$php -i

should display the same version of mongodb similar in the second line.

After all of these steps I found one last package to install which is

$sudo apt-get install php-mongodb

But also this this didn't help.

To check if the extension is working I used either:

$m = new MongoClient();  OR

$m = new Mongo\Driver\Manager();

and then echo $m. I read that Mongo\Driver\Manager is the new version but even with this code it doesn't work. It always tells me that the class "could not be found". Irrespective of the class I tried.

I hope this (hopefully) complete instruction can help someone with a similar problem about mongodb and php.

Additionally I hope that these information can help you to give me a hint what else I can do to fix that problem. Unfortunately I searched a long time but didn't found something to include this extension into this php-version. There where only part but I cant build them together.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Danaq
  • 633
  • 1
  • 6
  • 18
  • Have you check this solid reply? http://stackoverflow.com/a/34508341/3081659 – Noman Aug 16 '16 at 05:12
  • yes, I found that but those steps (listed in the answers below) are the same I proceeded. I installed pecl, mongodb and I don't use any framework. Maybe I missed something but I would say I already tried all of these steps. However, thank u for this hit. – Danaq Aug 16 '16 at 06:55

2 Answers2

4

When using PHP 7+ install new Mongo PHP Adapter

 composer config "platform.ext-mongo" "1.6.16" && composer require alcaeus/mongo-php-adapter

Then install mongodb with PECL

pecl install mongodb

And if you installed correctly the mongodb

$ sudo nano /etc/php/7.0/apache2/php.ini

Add the following line in the file:

extension = mongodb.so;

or with this command

echo "extension=mongodb.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`
Strahinja90bg
  • 151
  • 14
  • SO don't allow edit thare are less than 6 characters : so on the line : extension = mongo.so;, it's extension = mongodb.so; (the alternate way is correct) – Bacteries Nov 09 '18 at 14:33
  • is there a non-pecl way to install MongoDB?? Or I need pear to get pecl? The occasional PHP user finds this very confusing thx. – Mark Aug 17 '20 at 16:02
  • If for some reason that extension isn't loaded and you are on ubuntu, this command works `sudo apt-get install php7.4-mongodb` – Gandalf Jul 20 '21 at 21:00
1

For docker, I just added below line to the Dockerfile:

RUN pecl install mongodb && docker-php-ext-enable mongodb

or

pecl install mongodb-1.5.0

docker-php-ext-enable mongodb
Amin Shojaei
  • 5,451
  • 2
  • 38
  • 46