Ubuntu – Apt-get fails on 16.04 or 18.04 installing mongodb

16.04aptmongodbpackage-management

When I attempt to install mongodb using the instructions on https://docs.mongodb.com/getting-started/shell/tutorial/install-mongodb-on-ubuntu/ on 16.04 I get the following result on the install step:

jonathan@poseidon:~$ sudo apt-get install mongodb-org
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Package mongodb-org is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source

E: Package 'mongodb-org' has no installation candidate

I checked the HTTP traffic with Wireshark and found that the only traffic was a HTTP GET for the list and a HTTP 304 response which suggests that the problem lies in my local configuration files. My sources.list.d file contains the following entry:

mongodb-org-3.4.list

What do I need to do to continue to troubleshoot this problem?

EDIT:
I tried to do this again on a new install of 16.04 under VirtualBox. I retrieved the key, created the list file, and did sudo apt update with the following results:

jonathan@Ubuntu-16:~$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 0C49F3730359A14518585931BC711F9BA15703C6
[sudo] password for jonathan: 
Executing: /tmp/tmp.uWYpRUWfjK/gpg.1.sh --keyserver
hkp://keyserver.ubuntu.com:80
--recv
0C49F3730359A14518585931BC711F9BA15703C6
gpg: requesting key A15703C6 from hkp server keyserver.ubuntu.com
gpg: key A15703C6: public key "MongoDB 3.4 Release Signing Key <packaging@mongodb.com>" imported
gpg: Total number processed: 1
gpg:               imported: 1  (RSA: 1)
jonathan@Ubuntu-16:~$ echo "deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.4.list
deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.2 multiverse
jonathan@Ubuntu-16:~$ sudo apt update
Ign:1 http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.2 InRelease
Get:2 http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.2 Release [3 462 B]
Hit:3 http://mx.archive.ubuntu.com/ubuntu xenial InRelease               
Get:4 http://security.ubuntu.com/ubuntu xenial-security InRelease [94.5 kB]                   
Get:5 http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.2 Release.gpg [801 B]                      
Get:6 http://mx.archive.ubuntu.com/ubuntu xenial-updates InRelease [95.7 kB]           
Ign:5 http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.2 Release.gpg                       
Get:7 http://mx.archive.ubuntu.com/ubuntu xenial-backports InRelease [92.2 kB]                    
Reading package lists... Done      
W: GPG error: http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.2 Release: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY D68FA50FEA312927
E: The repository 'http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.2 Release' is not signed.
N: Updating from such a repository can't be done securely, and is therefore disabled by default.
N: See apt-secure(8) manpage for repository creation and user configuration details.
jonathan@Ubuntu-16:~$ 

As you can see, the key retrieved was for Mongodb 3.4, not Mongodb 3.2 and this explains the GPG failure in apt update. Any one know where to get the correct key?

Best Answer

Ubuntu 16.04 and MongoDB 4.0

First, remove any existing repository file for MongoDB.

sudo rm /etc/apt/sources.list.d/mongodb*.list

Next, add the key: (without the key, the repository will not load)

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

Now, create a new MongoDB repository list file:

sudo bash -c 'echo "deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/4.0 multiverse" > /etc/apt/sources.list.d/mongodb-org-4.0.list'

Complete the installation with update of repositories then install:

sudo apt update
sudo apt install mongodb-org

Enable the mongod service and start it up:

systemctl enable mongod.service
systemctl start mongod.service

Check your mongodb version:

~$ mongo --version
MongoDB shell version v4.0.5
git version: 3739429dd92b92d1b0ab120911a23d50bf03c412
OpenSSL version: OpenSSL 1.0.2g  1 Mar 2016
allocator: tcmalloc
modules: none
build environment:
    distmod: ubuntu1604
    distarch: x86_64
    target_arch: x86_64

Check if the service is running:

~$ systemctl status mongod.service
● mongod.service - MongoDB Database Server
   Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2019-01-21 00:33:51 MST; 7s ago
     Docs: https://docs.mongodb.org/manual
 Main PID: 2906 (mongod)
   CGroup: /system.slice/mongod.service
           └─2906 /usr/bin/mongod --config /etc/mongod.conf

Ubuntu 18.04 and MongoDB 4.2

First, remove MongoDB from previous if installed:

sudo apt remove --autoremove mongodb-org

Remove any mongodb repo list files:

sudo rm /etc/apt/sources.list.d/mongodb*.list
sudo apt update

Now, add the new key:

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 4B7C549A058F8B6B

Add the new repository:

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

Install MongoDB

sudo apt update
sudo apt install mongodb-org

Enable and start the mongod server service:

systemctl enable mongod.service
systemctl start mongod.service

Check your installation:

~$ mongo --version
MongoDB shell version v4.2.2
git version: a0bbbff6ada159e19298d37946ac8dc4b497eadf
OpenSSL version: OpenSSL 1.1.1  11 Sep 2018
allocator: tcmalloc
modules: none
build environment:
    distmod: ubuntu1804
    distarch: x86_64
    target_arch: x86_64

You can also check your service has started:

~$ systemctl status mongod.service 
● mongod.service - MongoDB Database Server
   Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: e
   Active: active (running) since Mon 2019-12-30 08:05:48 MST; 1min 7s ago
     Docs: https://docs.mongodb.org/manual
 Main PID: 7941 (mongod)
   CGroup: /system.slice/mongod.service
           └─7941 /usr/bin/mongod --config /etc/mongod.conf

Ubuntu 20.04 and MongoDB 4.4

First, remove MongoDB from previous if installed:

sudo apt remove --autoremove mongodb-org

Remove any mongodb repo list files:

sudo rm /etc/apt/sources.list.d/mongodb*.list
sudo apt update

Now, add the new key:

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 656408E390CFB1F5

Add the new repository:

echo "deb [arch=amd64] http://repo.mongodb.org/apt/ubuntu $(lsb_release -sc)/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org.list

Install MongoDB

sudo apt update
sudo apt install mongodb-org

Enable and start the mongod server service:

systemctl enable mongod.service
systemctl start mongod.service

Check your installation:

~$ mongo --version
MongoDB shell version v4.4.3
Build Info: {
    "version": "4.4.3",
    "gitVersion": "913d6b62acfbb344dde1b116f4161360acd8fd13",
    "openSSLVersion": "OpenSSL 1.1.1f  31 Mar 2020",
    "modules": [],
    "allocator": "tcmalloc",
    "environment": {
        "distmod": "ubuntu2004",
        "distarch": "x86_64",
        "target_arch": "x86_64"
    }
}

You can also check your service has started:

~$ systemctl status mongod.service 
● mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset>
     Active: active (running) since Sun 2021-01-24 07:52:50 MST; 27s ago
       Docs: https://docs.mongodb.org/manual
   Main PID: 2353629 (mongod)
     Memory: 59.2M
     CGroup: /system.slice/mongod.service
             └─2353629 /usr/bin/mongod --config /etc/mongod.conf

Jan 24 07:52:50 terrance-ubuntu systemd[1]: Started MongoDB Database Server.

Hope this helps!