Installing MongoDB driver for PHP on Ubuntu and other Linux distributions

Mongo extension with MongoClient class is deprecated. Instead, the MongoDB extension should be used. Please follow this article for installing MongoDB extension based driver.

Mongo is official MongoDB driver for PHP. Here is detail of this driver on php.net:  http://php.net/manual/en/mongo.manual.php . Mongo include MongoClient class which is required to connect MongoDB and required by several packages that bridge between MongoDB and PHP.

MongoClient works on almost all of Operating Systems including Windows, Mac OS X, Unix, and Linux; little- and big-endian machines. No matter 32 or 64-bit machines. PHP 5.3 through 5.6. To install it, there is a “mongo” package on PECL.

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

  1. Run this command:
    sudo pecl install mongo
  2. If response of above command is error with text “phpize not found” then you need to first run this command and then above one. Please note that this command is useful on Debian based distributions, for others please use appopiate command accordingly :
    sudo apt-get install php5-dev
  3. Then add following line to php.ini
    extension=mongo.so
  4. Then you need to restart your apache or nginx . If you are using apache then use following command to reload it.
  5. service apache2 reload

    After this hopefully your PHPClient class will be working fine and you can test your code which use MongoClient class.

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

9 thoughts on “Installing MongoDB driver for PHP on Ubuntu and other Linux distributions”

  1. It is installed. [pecl/mongo is already installed and is the same as the released version 1.6.14
    install failed] whenever i try to install.

    But not listing in the phpinfo();

    Reply
    • A simple way that works in most of cases is is to place a PHP file having one function call in it
      It will show you lot of information regarding your PHP installation including php.ini path in one of first few rows.

      Reply
  2. Hi, i followed your instructions and changed php.ini file in apache2, but
    echo extension_loaded(“mongo”) ? “loaded\n” : “not loaded\n”;
    returns not loaded

    Reply

Share your thoughts