Lesson 2: Installing the MongoDB PHP Driver / Learn

Press O for more options
3:14

Video Transcript (English)

In this video, you will learn how to install the official MongoDB PHP extension by using the PECL package manager.

We'll also configure our PHP settings by updating our php.ini file to include the MongoDB extension.

Then we'll use Composer to install the MongoDB PHP library, and include the library in our app.

For this demo, we've already installed a supported version of PHP on our computer. The extension manager to install tools in PHP is called PECL. We will use that to install the MongoDB extension, which we do by running this command in the terminal. If you are using Windows, and cannot use PECL, you will need to download the DLL files.

If you get an error here, you are not alone. You can reference the MongoDB PHP docs for common extension installation errors.

Once the extension has been successfully installed, we need to include it in our CLI environment’s php.ini file so that our PHP installation knows to look for the MongoDB extension.

First let’s find our CLI ini file by running `php –ini`.

Next, let’s open our CLI environment’s php.ini file in a text editor. We add extension=mongodb.so

to the bottom of the file and save it.

Keep in mind that your CLI environment’s php.ini file may reside in a different location, depending on your PHP installation.

Next, we want to double check that the mongodb extension is set up correctly. So, we run php -m and search for mongodb in the output.

Excellent! The extension has been installed. Now, to unlock all of the capabilities of MongoDB Developer Data Platform, we will install the MongoDB PHP library. To do this, we’ll use Composer, a popular dependency manager for PHP.

Let’s start by creating an empty project folder.

Next, we initialize our empty project by running `composer init`.

Now we run `composer require mongodb/mongodb:^1.15`. Adding the caret operator with the version will install version 1.15 or a newer 1.x release of the MongoDB library.

You should get a success message for the installation. To double-check, we’ll run composer show mongodb/mongodb to see information about the installed library.

To make sure that your php scripts load the composer-installed libraries, include this standard line at the top of your php script file.

Alright! Now we’re ready to connect to an Atlas cluster and start working with some data!

In this video, we covered how to install the MongoDB extension by using PECL, add the extension to the php.ini file, and installing the MongoDB PHP library by using Composer.