w3resource

Install PHP driver for MongoDB

Description

In this tutorial, we will discuss how to install PHP driver for MongoDB on Linux, Windows, and Mac.

Install MongoDB PHP driver on Linux

Install from terminal

You have to execute the following command to install MongoDB PHP driver on Linux :

$ sudo pecl install mongo

You must have internet connection enabled, pecl installed on PHP and must have root privilege to successfully run this command.

Install manually

If you want to compile the driver from source code, you have to do that manually. The advantage of doing this is, latest bug fixes will be included in your installation.

You have to download MongoDB PHP driver from Github. Go to the github website and search for "mongo php driver". Download the package and then execute the following :

$ tar zxvf mongodb-mongodb-php-driver-<commit_id>.tar.gz
$ cd mongodb-mongodb-php-driver-<commit_id>
$ phpize
 $ ./configure  
  $ sudo make install

After executing the previous commands, you have to make a change in your php.ini file. Open the file in your favorite text editor and extension_dir variable is pointing to the location of mongo.so. Also, add 'extension=mongo.so' in a file, save and restart your web server.

Now if you point your browser to a php file containing 'echo phpinfo();' command, you can see that in the output, MongoDB is shown as installed.

Install MongoDB PHP driver on Windows

Pre compiled binaries are available on github. Download the version compatible with your PHP and MongoDB version. Before you select to download a specific binary, remember the following :

Opt for VC6 if you are running Apache web server.

Opt for 'Thread safe' if you are running PHP as an Apache module, but if you are running PHP as CGI, go for ' non-thread safe'.

Opt for VC9 if you are running IIS web server.

After you download the binary suitable for you, unzip it and add 'php_mongo.dll' file to your PHP extension directory, which usually located as 'ext' folder within your PHP installation directory.

Open your 'php.ini' file and add 'extension=php_mongo.dll' to it.

Restart your web server.

Now if you point your browser to a php file containing 'echo phpinfo();' command, you can see that in the output, MongoDB is shown as installed.

mongo php driver installed windows

Install MongoDB PHP driver on Mac

You can install MongoDB PHP driver using 'autoconf'.

You can install MongoDB PHP driver using 'Xcode'.

If you are using XAMPP, you may install MongoDB PHP driver using the following command :

sudo /Applications/XAMPP/xamppfiles/bin/pecl install mongo

If the above command does not work on XMPP or MAMP, you have to download the compatible precompiled binary from github. You have to add 'extension=mongo.so' to your php.ini file and restart your web server.

Previous: Python DRIVER
Next: Introduction to MongoDB



Follow us on Facebook and Twitter for latest update.