Install the MongoDB PHP Library


The MongoDB PHP Library is a high-level abstraction for the MongoDB PHP driver. As such, you must install the mongodb extension to use the library.

Installing the MongoDB PHP Driver describes how to install the mongodb extension for PHP. Instructions for installing the driver for HHVM may be found in the Installation with HHVM article.


Install the Library

The preferred method of installing MongoDB PHP Library is with Composer by running the following from your project root:

composer require mongodb/mongodb

While not recommended, you may also manually install the package via the source tarballs attached to the GitHub releases.

Configure Autoloading

Once you have installed the library, ensure that your application includes Composer’s autoloader. The require_once statement should point to Composer’s autoloader, as in the following example:

require_once __DIR__ . "/vendor/autoload.php";

Refer to Composer’s autoloading documentation for more information about setting up autoloading.

If you installed the library manually from a source tarball, you will also need to manually configure autoloading:

  1. Map the top-level MongoDB\ namespace to the src/ directory using your preferred autoloader implementation.
  2. Manually require the src/functions.php file, since PHP does not yet support function autoloading.