I am very eager to start working with PHP 7 however one issue is getting in the way... I primarily use MongoDB for the database, and the problem is that I don't know how to install the MongoDB driver/client for PHP 7.

My current installation is PHP 5.6 and on my Mac and brew install php56-mongo does the trick.

Can anyone recommend how I can get this working on my Mac or an Ubuntu install?

Thanks in advance and much appreciated!

Solution 1

The Mongo extension for PHP Version 5.99.99 or older has been superseded:


Use the newer one for PHP Version 7.99.99 or older instead:


You can install a PECL/PEAR extension automatically:

pecl install mongodb

or manually.

The classes have been changed too:

new \MongoClient(); // legacy class!

see http://php.net/manual/en/book.mongo.php

new \MongoDB\Driver\Manager(); // new classes! 

see http://php.net/manual/en/set.mongodb.php

Additional information regarding compatibility can be found here:


Solution 2

The MongoDB driver that supports PHP 7 was only released December 22nd - its likely downstream repositories like brew haven't updated.

Update confirmed there is currently no php70-mongo brew script, though there is an active pull request to add one.

You may be able to install it manually via pecl in the meantime:

pecl channel-update pecl.php.net

pecl install mongodb

echo "extension=mongodb.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`

Solution 3

How to connect php 7.0 with MongoDB in ubuntu 16.04 lts?

1)Install LAMP using the following link. It installs Apache2, mysql and php 7.0. https://www.digitalocean.com/community/tutorials/how-to-install-linux-apache-mysql-php-lamp-stack-on-ubuntu-16-04

2)Install the MongoDB community Edition on Ubuntu using the steps in the following link. http://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/

3)Type the following command to get the mongoDB extension from pecl

sudo apt install php-pear

4)Add the following to the php.ini file at /etc/php/apache2/7.0


Important - The classes have been changed too:

new MongoClient();  //Old Class

new MongoDB\Driver\Manager(); // New Class

Refer - http://php.net/manual/en/set.mongodb.php

Solution 4

You can try install mongodb driver with:

sudo apt-get install php-mongodb

Solution 5

No, the legacy driver does not support PHP7, unfortunately. Here's the commit and the JIRA Ticket where this was officially finalized.

The new PHP MongoDB driver can be found in PECL here (or GitHub).

To install, just:

pecl channel-update pecl.php.net

pecl install mongodb

echo "extension=mongodb.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`

The documentation for the new driver can be found here. I'd like to include a note from the documentation:

Ultimately, this extension is not intended to be used alone. Users should considering using this driver alongside one or more userland PHP libraries, such as mongo-php-library.

The new mongodb driver / PHP extension is a lot more low-level than the legacy mongo driver, and you are encouraged to use a higher-level library on top of the driver rather than using it directly in your code.

The Mongo PHP Library (releases) is the official high-level library for PHP, and it's what is recommended to use in your projects. It's still in Beta, but this still seems to be the safest and most-future-proof path forward with PHP7.

It might be possible for someone to port the legacy driver to PHP7, but there probably isn't much of a need for it, as there are many other problems with the legacy driver.

Solution 6

Solution 7

I almost gave up, too. For The MongoDB driver for PHP 7x, Ubuntu 18.04 Pecl will not work. Instead, try:

sudo apt-get install php-mongodb  

Then in the base of your project folder install the mongodb library https://docs.mongodb.com/php-library/current/tutorial/install-php-library/

composer require mongodb/mongodb
composer install

Which accesses the lower level functions provided by the driver.

Lastly, go to php.ini and add

extension = mongo.so

and restart apache

To test, try adding this to a php file:

     require_once __DIR__ . "/vendor/autoload.php";
     $collection = (new MongoDB\Client)->test->users;

Solution 8

Old question, but new excellent solution. Just use Mongostead7 automated script for installing all needed stuff. Worked for me just fine. No additional work needed.

Use it as follows:

sudo curl -sS https://raw.githubusercontent.com/zakhttp/Mongostead7/master/mongoHomestead7.sh | sudo sh

Solution 9

Complementing answers and publishing what worked for me:

1 followed this guide in order to install lamp https://www.digitalocean.com/community/tutorials/how-to-install-linux-apache-mysql-php-lamp-stack-on-ubuntu-16-04 (The third step is needed only for installing the mongo client)

2 $ sudo apt-get install php7.0-dev

3 $ sudo pecl install mongodb

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

Add the following line in the file:

extension = mongo.so;

(You might need to specify the exact location of the file. In my case the file was in /usr/lib/php/20151012/mongodb.so.)

And thats all for installing just the mongo client for php 7.0

I am complementing the Pransh Tiwari answer

Solution 10

UBUNTU 16.0.4 (07.12.2016)

install PHP-MONGODB drivers : Commandes :
- sudo pecl install mongodb -> résultat : Build process completed successfully Installing '/usr/lib/php/20151012/mongodb.so' install ok: channel://pecl.php.net/mongodb-1.2.0 configuration option "php_ini" is not set to php.ini location You should add "extension=mongodb.so" to php.ini

                    -> la librairie se trouve dans "/usr/lib/php/20151012/mongodb.so"
            - sudo systemctl restart apache2.service
            - create 2 new files called "30-mongodb.ini" in both path to add the extension to your server:
                Commandes :
                    sudo nano /etc/php/7.0/fpm/conf.d/30-mongodb.ini

                        -> add "extension=mongodb.so"

                    sudo nano /etc/php/7.0/cli/conf.d/30-mongodb.ini

                        -> add "extension=mongodb.so"

            - Test if the mongodb extension is running in your server :
                Commandes :
                    php --ini

install DoctrineMongoDBBundle : http://symfony.com/doc/current/bundles/DoctrineMongoDBBundle/index.html, http://www.doctrine-project.org/2016/06/09/odm-1-1-0-and-1-0-6.html,

    Commandes :
        - cd
        - cd my_project_name
        - ls -a composer*
            -> résultat : composer.json  composer.lock
        - sudo nano composer.json
        - Add to the composer.json in "require" array
            "doctrine/mongodb-odm": "^1.0",
            "doctrine/mongodb-odm-bundle": "^3.0"
        - Add to the composer.json in "require" array
            "alcaeus/mongo-php-adapter": "^1.0",
            "ext-mongo": "*"
        - Add a new array :
                "ext-mongo": "1.6.12"
        - Move 
        sudo cp -i /usr/lib/php/20151012/mongodb.so /etc/php/7.0/cli

To give the solution I need at least 10 reputation to post...

Solution 11

I am using php version 7.0 on ubuntu 16.04. I am giving a detailed info for installing the mongo driver/client. First I manually installed mongodb and then the mongodb-php driver for it.

1) Installing mongo db. Enter the following commands:

$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927

$ echo "deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list

$ sudo apt-get update

$ sudo apt-get install -y mongodb-org

In order to properly launch Mongdb as a service, ie automatically starting Mongodb when the system starts, follow the following steps:

Create file mongodb.service in /etc/systemd/system/ by entering the command:

$ sudo nano /etc/systemd/system/mongodb.service

Paste the following contents in it:

Description=High-performance, schema-free document-oriented database

ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf


Then enter the following commands:

$ sudo systemctl start mongodb

$ sudo systemctl enable mongodb

2) Installing the mongo-php driver:

$ sudo pecl install mongodb

Also you might receive error: phpize not found. Phpize is a command which is used to create a build environment. This error could appear at the time of installation of any pecl extension. To solve this problem of the phpize command not found, the user has to install the php5-dev package. To install it enter the command:

 $ sudo apt-get install php7.0-dev

Then in the php.ini file which is in /etc/php/7.0/apache2 directory, add the mongo db extension:

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

Add the following line in the file:

extension = mongo.so;

(You might need to specify the exact location of the file. In my case the file was in /usr/lib/php/20151012/mongodb.so.)

So the mongo db is installed along with its driver.

3) Now keep in mind that the mongo-php classes have been changed. Most of the available resources in the net give solutions using old classes which is superseded. Below are the links which you can refer to:



Here are some commands for basic database operations:

$mng = new MongoDB\Driver\Manager(); // Driver Object created

To insert data into the database:

$bulk = new MongoDB\Driver\BulkWrite;

$doc = ["_id" => new MongoDB\BSON\ObjectID, "data" => $someData, "info" => $someInfo];


$mng->executeBulkWrite('dbName.collectionName', $bulk);

For fetching data:

$query = new MongoDB\Driver\Query([]); 

$rows = $mng->executeQuery("dbName.collectionName", $query);

foreach ($rows as $row) 
         echo "$row->data - $row->info\n";

Solution 12

This worked for me on Ubuntu for PHP7:

sudo apt-get install php7.0-mongodb