Installing the PHP 7 MongoDB Client/Driver?

Vladimir Fazilov picture Vladimir Fazilov · Dec 28, 2015 · Viewed 99.3k times · Source

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!

Answer

Daniel W. picture Daniel W. · Dec 29, 2015

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

https://pecl.php.net/package/mongo

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

https://pecl.php.net/package/mongodb

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:

https://docs.mongodb.org/ecosystem/drivers/php/#compatibility