Docs Menu

Docs HomePHP Library Manual

Install the MongoDB PHP Library

On this page

  • Installing the Extension
  • Installing the Library
  • Using Composer
  • Manual Installation Without Composer

The MongoDB PHP Library is a high-level abstraction for the PHP driver (i.e. mongodb extension). This page will briefly explain how to install both the mongodb extension and the MongoDB PHP Library.

Linux, Unix, and macOS users can either install the extension with PECL (recommended) or manually compile from source. The following command may be used to install the extension with PECL:

sudo pecl install mongodb

Note

If the build process for either installation method fails to find a TLS library, check that the development packages (e.g. libssl-dev) and pkg-config are both installed.

Once the extension is installed, add the following line to your php.ini file:

extension=mongodb.so

Windows users can download precompiled binaries of the extension from its GitHub releases. After downloading the appropriate archive for your PHP environment, extract the php_mongodb.dll file to PHP's extension directory and add the following line to your php.ini file:

extension=php_mongodb.dll

See Installing the MongoDB PHP Driver on Windows for additional information.

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

composer require mongodb/mongodb

Once you have installed the library, ensure that your application includes Composer's autoloader as in the following example:

<?php
require_once __DIR__ . '/vendor/autoload.php';

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

While not recommended, you may also manually install the library using a source archive attached to the GitHub releases. When installing the library without Composer, you must ensure that all library classes and functions are loaded for your application:

  1. If you are using a PSR-4 autoloader, map the top-level MongoDB\ namespace to the src/ directory. If you are not using an autoloader, manually require _all_ class files found recursively within the src/ directory.

  2. Regardless of whether you are using an autoloader, manually require the src/functions.php file. This is necessary because PHP does not support autoloading for functions.

←  MongoDB PHP LibraryTutorials →