MongoDB installation failed on Ubuntu 16.04

Solution 1:

TL;DR;

Try running this command,

sudo apt-get install libcurl3 openssl

and then

sudo apt-get install mongodb-org

Logs:

So I've followed official instructions on Mongo 4.0 here and got this after running sudo apt-get install mongodb-org

The following packages have unmet dependencies:
 mongodb-org : Depends: mongodb-org-server but it is not going to be installed
E: Unable to correct problems, you have held broken packages.

Then I manually added mongodb-org-server to installation command:

sudo apt-get install mongodb-org mongo-db-server

but then I got

mongodb-org-server : Depends: libcurl3 (>= 7.16.2) but it is not going to be installed
E: Unable to correct problems, you have held broken packages.

After manually installing these:

sudo apt-get install libcurl3 openssl

I got it working with single:

sudo apt-get install mongodb-org

command.

Solution 2:

Install mongodb in Ubuntu 12.04, 14.04 ,16.04

  1. Import the public key used by the package management system

    sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5
    
  2. Create a list file for MongoDB.

    In Ubuntu 12.04 (deprecated):

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

    In Ubuntu 14.04:

    echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.6 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.6.list
    

    In Ubuntu 16.04:

    echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.6 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.6.list
    
  3. Reload local package database.

    sudo apt-get update
    

    It will throw a warning

    W: http://repo.mongodb.org/apt/ubuntu/dists/trusty/mongodb-org/3.0/Release.gpg: Signature by key 492EAFE8CD016A07919F1D2B9ECBEC467F0CEB10 uses weak digest algorithm (SHA1)
    

    Just Ignore It.

  4. Install the MongoDB packages.

    sudo apt-get install -y mongodb-org
    

After installation you can start MongoDB using

sudo service mongod start

To stop MongoDB use

sudo service mongod stop

To restart MongoDB use

sudo service mongod restart

If that didn't work, check here to see if any of the steps have been updated: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/

Solution 3:

I tried running the install instructions posted by t9toqwerty / muru. But still got the message

the following packages have unmet dependencies:

What worked for me was to manually install MongoDB from the tarball. Original link is: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-linux/

1) Download Binaries

curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.4.9.tgz

2) Extract Files

tar -zxvf mongodb-linux-x86_64-3.4.9.tgz

3) Copy Archive to Target Directory

mkdir -p mongodb
cp -R -n mongodb-linux-x86_64-3.4.9/ mongodb

4) Place location in the PATH variable.

export PATH=<mongodb-install-directory>/bin:$PATH

(Replace <mongodb-install-directory> with the path to the extracted MongoDB archive. For me this was /home/myusername/mongodb/mongodb-linux-x86_64-3.4.9)

Next comes running it:

1) Create Data Directory

mkdir -p /data/db

2) Set Permissions

This is making sure the user has read and write permissions for the directory. Find the directory in the terminal, and enter

chmod 777 /data/db

3) Run it!

mongod

If you didn't set up the PATH variable in step 4, run mongo by entering the path to your bin file— like so

<path to binary>/mongod