Installing the PHP mongodb library

PHP7 is a bit confusing. There is a mongodb driver and also a mongodb module. I find myself having to install this a lot, and having difficulty doing so, so here is how to install it for future reference.

From a clean ubuntu/debian box,

sudo apt install php php-pear php-dev apache2

Install Mongodb driver

sudo apt install pkg-config libssl-dev libsslcommon2-dev
sudo pecl install mongodb
sudo nano /etc/php/7.0/apache2/php.ini

Add the following content:

extension=mongodb.so

Reload PHP modules

sudo service apache2 restart

Install composer from https://getcomposer.org/download/ then

sudo mv composer.phar /usr/bin/composer
cd /var/www/myproject/webroot
composer require mongodb/mongodb

And in your main PHP file, put the following

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

I hope this helps speed up the installation for you!

Published 2017-09-12