Installing MongoDB Driver (with MongoDB extension) on Ubuntu and other Linux distro.

Previously “Mongo” was the extension used for MongoDB (Legacy) driver for PHP. Now that is depreciated. Now “MongoDB” Pecl extension is used instead of  “Mongo” Pecl extension. And MongoDB Driver is PHP’s official driver for MongoDB. Here is detail of this driver on php.net: http://php.net/manual/en/set.mongodb.php . It supports PHP as well as HHVM.

To have this MongoDB driver working, you need to install pecl “MongoDB” extension.

To install PHP MongoDB on Linux, follow below mentioned steps:

  1. Run this command:
    sudo pecl install mongodb
  2. Then add following line to php.ini
    extension=mongodb.so
  3. Then you need to restart your apache or nginx . If you are using apache then use following command to reload it.
  1. service apache2 reload

    To test your installation try instantiating “MongoDB\Driver\Client” by using code similar to this one:

    $client = new MongoDB\Driver\Client("mongodb://localhost:27017");

    If it says that MongoDB\Driver\Client not found or not defined then there is chance that MongoDB driver is not properly installed. However parameters to this constructor can be changed depends on your MongoDB URL.

    In case of any problem, feel free to let me know in comments section.

3 thoughts on “Installing MongoDB Driver (with MongoDB extension) on Ubuntu and other Linux distro.”

  1. MongoDB\Driver\Client is deprecated and WILL CAUSE AN ERROR even if the mongodb extension is installed correctly.

  2. thanks brother haafiz

    i am using ubuntu16 + php7 + nginx
    when i run command ” sudo pecl install mongodb ”
    i get this Error:
    running: phpize
    sh: 1: phpize: not found
    ERROR: `phpize’ failed

Share your thoughts