I'm running Ubuntu 16.04 LTS and I want to use Mongodb with PHP. For this I tried

sudo apt-get install php5-mongo

which is enough for Ubuntu 14.04 LTS would be enough but I was wrong. I'm getting error message as:

E: Unable to locate package php5-mongo

I've just upgraded the machine OS to Ubuntu 16.04 LTS and wanted to use mongodb with PHP 7.0.4.

So what can I do to solve this?

  • I had tried this sudo apt-get install php7-mongo too but not got any positive result – Vikash Kumar Apr 29 '16 at 21:04
  • if you want with php5-mongo then try this sudo add-apt-repository ppa:chris-lea/mongodb-drivers sudo apt-get update sudo apt-get install php5-mongo – bhordupur Apr 29 '16 at 21:31
  • @bhordupur this repository seems to be invalid. Also, have you considered making this an answer instead? W: The repository 'http://ppa.launchpad.net/chris-lea/mongodb-drivers/ubuntu xenial Release' does not have a Release file. N: Data from such a repository can't be authenticated and is therefore potentially dangerous to use. N: See apt-secure(8) manpage for repository creation and user configuration details. E: Failed to fetch http://ppa.launchpad.net/chris-lea/mongodb-drivers/ubuntu/dists/xenial/main/binary-amd64/Packages 404 Not Found – Vivek Kodira Oct 29 '17 at 13:55
  • 1
    @VivekKodira No I did not put that as an answer its because Vicky R was using Ubuntu 16.04 LTS where by default php7.x installed while performing this php-mongodb. I commented that due to he was coming from 14 to 16 but still he could still use that what I commented above. This is why I did not put my comment as answer. – bhordupur Dec 6 '17 at 13:16

To install the mongodb extension, do:

sudo apt-get install php-mongodb
  • 2
    How is this the accepted answer for php7? This command on ubuntu 16.04 will install php5.6 mongo no? – Ligemer Feb 22 '17 at 20:27
  • 2
    @Ligemer 16.04 has php7.0 as default PHP, therefore all meta packages install the 7.0 version of said ones. – Videonauth Feb 22 '17 at 21:01
  • ahh yes, thanks for the reminder, I have php5.6 installed as well from another ppa and isn't the same packages which led me to the error. Some documentation from mongo says to use the pecl extension which is what I ended up using. – Ligemer Feb 22 '17 at 23:56
  • 2
    Don't forget to restart the Apache server after installing the package. – TheKarateKid Apr 25 '17 at 22:55
  • Can I use same command for AWS Linux Ec2? – 151291 May 26 '18 at 7:59

I am sure you found a solution by now. However I want to share how I did it:


I am using php version 7.0 on ubuntu 16.04. I am giving a detailed info for installing the mongo driver/client. First I manually installed mongodb and then the mongodb-php driver for it.

1) Installing mongo db. Enter the following commands:

$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927

$ echo "deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list

$ sudo apt-get update

$ sudo apt-get install -y mongodb-org

In order to properly launch Mongdb as a service, ie automatically starting Mongodb when the system starts, follow the following steps:

Create file mongodb.service in /etc/systemd/system/ by entering the command:

$ sudo nano /etc/systemd/system/mongodb.service

Paste the following contents in it:

Description=High-performance, schema-free document-oriented database

ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf


Then enter the following commands:

$ sudo systemctl start mongodb

$ sudo systemctl enable mongodb

2) Installing the mongo-php driver:

$ sudo pecl install mongodb

Also you might receive error: phpize not found. Phpize is a command which is used to create a build environment. This error could appear at the time of installation of any pecl extension. To solve this problem of the phpize command not found, the user has to install the php5-dev package. To install it enter the command:

 $ sudo apt-get install php7.0-dev

Then in the php.ini file which is in /etc/php/7.0/apache2 directory, add the mongo db extension:

$ sudo nano /etc/php/7.0/apache2/php.ini 

Add the following line in the file:

extension = mongo.so;

(You might need to specify the exact location of the file. In my case the file was in /usr/lib/php/20151012/mongodb.so.)

So the mongo db is installed along with its driver.

3) Now keep in mind that the mongo-php classes have been changed. Most of the available resources in the net give solutions using old classes which is superseded. Below are the links which you can refer to:



Here are some commands for basic database operations:

$mng = new MongoDB\Driver\Manager(); // Driver Object created

To insert data into the database:

$bulk = new MongoDB\Driver\BulkWrite;

$doc = ["_id" => new MongoDB\BSON\ObjectID, "data" => $someData, "info" => $someInfo];


$mng->executeBulkWrite('dbName.collectionName', $bulk);

For fetching data:

$query = new MongoDB\Driver\Query([]); 

$rows = $mng->executeQuery("dbName.collectionName", $query);

foreach ($rows as $row) 
         echo "$row->data - $row->info\n";

or for php 7

sudo apt-get install php7.0-mongodb
  • we have installed mongodb but the error getting loading jumpset on browser like: Error:The MongoDB PECL extension has not been installed or enabled – shivashankar m Aug 23 '17 at 5:14

I'm using this on my Dockerfile:

echo 'deb http://packages.dotdeb.org jessie all' >> /etc/apt/sources.list
echo 'deb-src http://packages.dotdeb.org jessie all' >> /etc/apt/sources.list
curl -OL https://www.dotdeb.org/dotdeb.gpg \
    && apt-key add dotdeb.gpg \
    && rm dotdeb.gpg
apt-get update -y \
    && apt-get install -y \
        php7.0 \
        php7.0-mongodb \

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.