How to install MongoDB PHP extension

→ Are you a new visitor? Please visit the page guidance for new visitors ←

How to install MongoDB PHP extension

In our last tutorials I’ve showed you how to install MongoDB server directly on a linux Debian or Centos. The process is simple and you just need about 10 minutes or so for the average beginner to start working from shell with MongoDB. But what comes next is using a programming language like PHP to connect directly from a website to our noSQL database server. In this tutorial I’m gonna go through the steps you need to run to install MongoDB PHP extension on debian / Ubuntu operating system.

Preparing the needed packages

To install the MongoDB PHP extension automatically without compiling you need to install certain packages that are required. You can do this running the commands:

Install MongoDB PHP Extension

Finally you need to add the extension to your php.ini, I normally add a new file the conf.d folder, but you can add it to the php.ini directly if you wish.

Manually compile MongoDB PHP Extension

There are certain cases where pecl can not be used to install the php extension, it happen with certain extension with me and I was forced to manually compile it. This is not really for beginners as a wrong step can install things badly can cause issues, however for those that feel comfortably with this, then can follow the below steps.

First you need to go to Github and download the MongoDB driver / extension:

For the next command you need to make sure you have the php5-dev installed, you can run the bellow command for this:

Then you can run continue the installation:

And finally again add the extension to php.ini:

When you view your phpinfo() you should be able to find the MongoDB extension loaded and running as expected:

Install MongoDB PHP extension

Install MongoDB PHP extension

That would be all for now, see you again in our next tutorial where I’ll show you how you can use MongoDB with PHP.

Request an article ←