In this tutorial, we are going to see how to install MongoDB on Ubuntu.

How to install MongoDB on Ubuntu:

Installing MongoDB on Ubuntu or Linux machines takes several steps, this consolidated guide walk you through the detailed overview of each and every step that smoothen the installation process.

Add MongoDB repositories:

At first, we need to install dependencies that are required before adding repositories over HTTPS. To do this we need to run the following command in our terminal:

We can use sudo or root privilege as per our wish. This tutorial uses sudo privilege.

sudo apt-get update
[sudo] password for (name):

Enter the password.

This will produce the following output:

Terminal
Hit:1 http://ppa.launchpad.net/micahflee/ppa/ubuntu focal InRelease
Hit:2 http://np.archive.ubuntu.com/ubuntu focal InRelease
Get:3 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]
Get:4 http://np.archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
Get:5 http://np.archive.ubuntu.com/ubuntu focal-backports InRelease [101 kB]
Get:6 http://security.ubuntu.com/ubuntu focal-security/main i386 Packages [306 kB]
Get:7 http://np.archive.ubuntu.com/ubuntu focal-updates/main i386 Packages [562 kB]
Get:8 http://security.ubuntu.com/ubuntu focal-security/main amd64 Packages [987 kB]
Get:9 http://np.archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages [1,344 kB]
Get:10 http://security.ubuntu.com/ubuntu focal-security/main Translation-en [183 kB]
Get:11 http://np.archive.ubuntu.com/ubuntu focal-updates/main Translation-en [276 kB]
Get:12 http://security.ubuntu.com/ubuntu focal-security/main amd64 DEP-11 Metadata [29.0 kB]
Get:13 http://np.archive.ubuntu.com/ubuntu focal-updates/main amd64 DEP-11 Metadata [279 kB]
Get:14 http://security.ubuntu.com/ubuntu focal-security/main amd64 c-n-f Metadata [8,880 B]
Get:15 http://security.ubuntu.com/ubuntu focal-security/universe i386 Packages [515 kB]
Get:16 http://np.archive.ubuntu.com/ubuntu focal-updates/main amd64 c-n-f Metadata [14.5 kB]
Get:17 http://security.ubuntu.com/ubuntu focal-security/universe amd64 Packages [653 kB]
Get:18 http://np.archive.ubuntu.com/ubuntu focal-updates/universe amd64 Packages [876 kB]
Get:19 http://security.ubuntu.com/ubuntu focal-security/universe Translation-en [107 kB]
Get:20 http://np.archive.ubuntu.com/ubuntu focal-updates/universe i386 Packages [647 kB]
Get:21 http://security.ubuntu.com/ubuntu focal-security/universe amd64 DEP-11 Metadata [63.6 kB]
Get:22 http://np.archive.ubuntu.com/ubuntu focal-updates/universe Translation-en [190 kB]
Get:23 http://security.ubuntu.com/ubuntu focal-security/universe amd64 c-n-f Metadata [12.9 kB]
Get:24 http://np.archive.ubuntu.com/ubuntu focal-updates/universe amd64 DEP-11 Metadata [357 kB]
Get:25 http://security.ubuntu.com/ubuntu focal-security/multiverse amd64 DEP-11 Metadata [2,464 B]
Get:26 http://np.archive.ubuntu.com/ubuntu focal-updates/universe amd64 c-n-f Metadata [19.5 kB]
Get:27 http://np.archive.ubuntu.com/ubuntu focal-updates/multiverse amd64 DEP-11 Metadata [940 B]
Get:28 http://np.archive.ubuntu.com/ubuntu focal-backports/universe i386 Packages [5,536 B]
Get:29 http://np.archive.ubuntu.com/ubuntu focal-backports/universe amd64 Packages [6,588 B]
Get:30 http://np.archive.ubuntu.com/ubuntu focal-backports/universe amd64 DEP-11 Metadata [10.4 kB]
Get:31 http://np.archive.ubuntu.com/ubuntu focal-backports/universe DEP-11 64x64 Icons [5,607 B]
Get:32 http://np.archive.ubuntu.com/ubuntu focal-backports/universe amd64 c-n-f Metadata [580 B]
Fetched 7,791 kB in 10s (788 kB/s)
Reading package lists... Done

Then, run:

sudo apt install dirmngr gnupg apt-transport-https ca-certificates software-properties-common

Output:

Reading package lists... Done
Building dependency tree
Reading state information... Done
ca-certificates is already the newest version (20210119~20.04.2).
ca-certificates set to manually installed.
dirmngr is already the newest version (2.2.19-3ubuntu2.1).
dirmngr set to manually installed.
gnupg is already the newest version (2.2.19-3ubuntu2.1).
gnupg set to manually installed.
The following packages were automatically installed and are no longer required:
libllvm11 libllvm11:i386 linux-headers-5.11.0-36-generic
linux-hwe-5.11-headers-5.11.0-36 linux-image-5.11.0-36-generic
linux-modules-5.11.0-36-generic linux-modules-extra-5.11.0-36-generic shim
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
python3-software-properties software-properties-gtk
The following NEW packages will be installed:
apt-transport-https
The following packages will be upgraded:
python3-software-properties software-properties-common
software-properties-gtk
3 upgraded, 1 newly installed, 0 to remove and 122 not upgraded.
Need to get 106 kB of archives.
After this operation, 176 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://np.archive.ubuntu.com/ubuntu focal-updates/universe amd64 apt-transport-https all 2.0.6 [4,680 B]
Get:2 http://np.archive.ubuntu.com/ubuntu focal-updates/main amd64 software-properties-common all 0.99.9.8 [10.6 kB]
Get:3 http://np.archive.ubuntu.com/ubuntu focal-updates/main amd64 software-properties-gtk all 0.99.9.8 [66.0 kB]
Get:4 http://np.archive.ubuntu.com/ubuntu focal-updates/main amd64 python3-software-properties all 0.99.9.8 [24.9 kB]
Fetched 106 kB in 2s (54.8 kB/s)
Selecting previously unselected package apt-transport-https.
(Reading database ... 286552 files and directories currently installed.)
Preparing to unpack .../apt-transport-https_2.0.6_all.deb ...
Unpacking apt-transport-https (2.0.6) ...
Preparing to unpack .../software-properties-common_0.99.9.8_all.deb ...
Unpacking software-properties-common (0.99.9.8) over (0.98.9.5) ...
Preparing to unpack .../software-properties-gtk_0.99.9.8_all.deb ...
Unpacking software-properties-gtk (0.99.9.8) over (0.98.9.5) ...
Preparing to unpack .../python3-software-properties_0.99.9.8_all.deb ...
Unpacking python3-software-properties (0.99.9.8) over (0.98.9.5) ...
Setting up apt-transport-https (2.0.6) ...
Setting up python3-software-properties (0.99.9.8) ...
Setting up software-properties-common (0.99.9.8) ...
Setting up software-properties-gtk (0.99.9.8) ...
Processing triggers for dbus (1.12.16-2ubuntu2.1) ...
Processing triggers for shared-mime-info (1.15-1) ...
Processing triggers for desktop-file-utils (0.24-1ubuntu3) ...
Processing triggers for mime-support (3.64ubuntu1) ...
Processing triggers for hicolor-icon-theme (0.17-2) ...
Processing triggers for gnome-menus (3.36.0-1ubuntu1) ...
Processing triggers for libglib2.0-0:amd64 (2.64.6-1~ubuntu20.04.4) ...
Processing triggers for libglib2.0-0:i386 (2.64.6-1~ubuntu20.04.4) ...
Processing triggers for man-db (2.9.1-1) ...

We successfully installed dependencies. Now we have to import the GPG key and add the MongoDB repository.

Import GPG Key:

run the following command to import the GPE key.

wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -

Output:

OK

Add MongoDB repository:

sudo add-apt-repository 'deb [arch=amd64] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse'

Output:

Ign:1 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 InRelease
Get:2 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 Release [5,378 B]
Hit:3 http://security.ubuntu.com/ubuntu focal-security InRelease
Hit:4 http://ppa.launchpad.net/micahflee/ppa/ubuntu focal InRelease
Hit:5 http://np.archive.ubuntu.com/ubuntu focal InRelease
Get:6 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 Release.gpg [801 B]
Hit:7 http://np.archive.ubuntu.com/ubuntu focal-updates InRelease
Hit:8 http://np.archive.ubuntu.com/ubuntu focal-backports InRelease
Get:9 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4/multiverse amd64 Packages [18.0 kB]
Fetched 24.2 kB in 2s (13.6 kB/s)
Reading package lists... Done

Install MongoDB:

Now, let’s install MongoDB. To do this, run the following command.

sudo apt install MongoDB

Output:

Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
libllvm11 libllvm11:i386 linux-headers-5.11.0-36-generic
linux-hwe-5.11-headers-5.11.0-36 linux-image-5.11.0-36-generic
linux-modules-5.11.0-36-generic linux-modules-extra-5.11.0-36-generic shim
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
mongodb-clients mongodb-server
The following NEW packages will be installed:
mongodb mongodb-clients mongodb-server
0 upgraded, 3 newly installed, 0 to remove and 122 not upgraded.
Need to get 21.6 MB of archives.
After this operation, 77.7 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://np.archive.ubuntu.com/ubuntu focal-updates/universe amd64 mongodb-clients amd64 1:3.6.9+really3.6.8+90~g8e540c0b6d-0ubuntu5.3 [21.6 MB]
Get:2 http://np.archive.ubuntu.com/ubuntu focal-updates/universe amd64 mongodb-server all 1:3.6.9+really3.6.8+90~g8e540c0b6d-0ubuntu5.3 [12.5 kB]
Get:3 http://np.archive.ubuntu.com/ubuntu focal-updates/universe amd64 mongodb amd64 1:3.6.9+really3.6.8+90~g8e540c0b6d-0ubuntu5.3 [9,640 B]
Fetched 21.6 MB in 7s (3,291 kB/s)
Selecting previously unselected package mongodb-clients.
(Reading database ... 286538 files and directories currently installed.)
Preparing to unpack .../mongodb-clients_1%3a3.6.9+really3.6.8+90~g8e540c0b6d-0ubuntu5.3_amd64.deb ...
Unpacking mongodb-clients (1:3.6.9+really3.6.8+90~g8e540c0b6d-0ubuntu5.3) ...
Selecting previously unselected package mongodb-server.
Preparing to unpack .../mongodb-server_1%3a3.6.9+really3.6.8+90~g8e540c0b6d-0ubuntu5.3_all.deb ...
Unpacking mongodb-server (1:3.6.9+really3.6.8+90~g8e540c0b6d-0ubuntu5.3) ...
Selecting previously unselected package mongodb.
Preparing to unpack .../mongodb_1%3a3.6.9+really3.6.8+90~g8e540c0b6d-0ubuntu5.3_amd64.deb ...
Unpacking mongodb (1:3.6.9+really3.6.8+90~g8e540c0b6d-0ubuntu5.3) ...
Setting up mongodb-clients (1:3.6.9+really3.6.8+90~g8e540c0b6d-0ubuntu5.3) ...
Setting up mongodb-server (1:3.6.9+really3.6.8+90~g8e540c0b6d-0ubuntu5.3) ...
Setting up mongodb (1:3.6.9+really3.6.8+90~g8e540c0b6d-0ubuntu5.3) ...
Processing triggers for man-db (2.9.1-1) ...
Processing triggers for systemd (245.4-4ubuntu3.11) ...

Now let’s check if MongoDB is properly installed by running:

At first update the repositories:

sudo apt-get update

and then look for the mongo version:

mongo --version

Output:

MongoDB shell version v3.6.8
git version: 8e540c0b6db93ce994cc548f000900bdc740f80a
OpenSSL version: OpenSSL 1.1.1f 31 Mar 2020
allocator: tcmalloc
modules: none
build environment:
distarch: x86_64
target_arch: x86_64

Congratulations MongoDB is successfully installed on your device.

Start the MongoDB instance:

sudo systemctl start MongoDB

Check the status:

sudo systemctl status MongoDB

Output:

● mongodb.service - An object/document-oriented database
Loaded: loaded (/lib/systemd/system/mongodb.service; enabled; vendor prese>
Active: active (running) since Tue 2021-11-16 15:43:15 CST; 58s ago
Docs: man:mongod(1)
Main PID: 149431 (mongod)
Tasks: 23 (limit: 19022)
Memory: 171.7M
CGroup: /system.slice/mongodb.service
└─149431 /usr/bin/mongod --unixSocketPrefix=/run/mongodb --config >

November 16 15:43:15 (uname) systemd[1]: Started An object/document-oriented databa>
lines 1-11/11 (END)

Stop the MongoDB instance:

sudo systemctl stop MongoDB

Restart the MongoDB instance:

sudo systemctl restart MongoDB

References:

Happy Learning 🙂

Leave A Comment