How to install MongoDB 5 on Ubuntu 21.04

In this tutorial guide we are going to learn how to install Mongodb on Ubuntu 21.04. Mongodb is an open source NoSQL database that provides high throughput for data driven applications. Unlike relational databases such as MySQL, Oracle and SQL server which store data in tables according to a rigid schema, Mongodb stores data in documents with flexible schema.

Why do we need MongoDB?

  • MongoDB makes it easy for developers to store structured or unstructured data, it uses JSON-like format to store documents. This format directly maps to native objects in most programming languages, making natural database for most developers.
  • MongoDB is built on a scale-out architecture making popular for developers developing scalable applications with evolving data schemas.
  • MongoDB is available in every major public cloud provider such as Azure, AWS, GCP making it easy for developers to deploy to any cloud provider of choice.
  • MongoDB supports rapid iterative development where developers collaborate with larger teams.
  • In MongoDB records are stored as documents in compressed BSON files.

MongoDB documents can be retrieved in JSON formats which has many benefits such as:

  • it is human readable, makes it easy to be read
  • It is a natural form to store data
  • You can nest JSON to to store complex data objects.
  • Documents maps to objects in most popular programming languages.
  • Structured and unstructured information can be stored in the same document
  • JSON has a flexible and dynamic schema, so adding fields or leaving a field out is not a problem.


Installing MongoDB

We are going to install MongoDB 5.0 in our system. For it to reflect the latest version, you must include MongoDB’s dedicated package repository to your APT sources.

First you have to import public GPG key to our repository by running the following command.

$ wget -qO - | sudo apt-key add -

Let us look at the contents we used above

  • wget which is a free software for retrieving files using HTTP, HTTPS, FTP and FTPS, the most widely used Internet protocols.
  • -q0 means quite to standard output silently.

We can check if our public key was added successfully with the following command:

$ apt-key list

The following output is given

$ # apt-key list
Warning: apt-key is deprecated. Manage keyring files in trusted.gpg.d instead (see apt-key(8)).
pub   rsa4096 2021-02-16 [SC] [expires: 2026-02-15]
      F567 9A22 2C64 7C87 527C  2F8C B00A 0BD1 E2C6 3C11
uid           [ unknown] MongoDB 5.0 Release Signing Key <[email protected]>

pub   rsa4096 2012-05-11 [SC]
      8439 38DF 228D 22F7 B374  2BC0 D94A A3F0 EFE2 1092
uid           [ unknown] Ubuntu CD Image Automatic Signing Key (2012) <[email protected]>

pub   rsa4096 2018-09-17 [SC]
      F6EC B376 2474 EDA9 D21B  7022 8719 20D1 991B C93C
uid           [ unknown] Ubuntu Archive Automatic Signing Key (2018) <[email protected]>

Run the following command to create a file sources.list.d directory named mongodb-org-5.0.list.

$ echo "deb [ arch=amd64,arm64 ] focal/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list

Output will look like this:

$ deb [ arch=amd64,arm64 ] focal/mongodb-org/5.0 multiverse

You can now update the system to reflect on the changes

$ sudo apt update

After the update is complete, you can now install mongoDB with the following command:

$ sudo apt install mongodb-org

Sample output will look like this:

# sudo apt install mongodb-org
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  mongodb-database-tools mongodb-mongosh mongodb-org-database mongodb-org-database-tools-extra
  mongodb-org-mongos mongodb-org-server mongodb-org-shell mongodb-org-tools
The following NEW packages will be installed:
  mongodb-database-tools mongodb-mongosh mongodb-org mongodb-org-database mongodb-org-database-tools-extra
  mongodb-org-mongos mongodb-org-server mongodb-org-shell mongodb-org-tools
0 upgraded, 9 newly installed, 0 to remove and 0 not upgraded.
Need to get 147 MB of archives.
After this operation, 464 MB of additional disk space will be used.
Do you want to continue? [Y/n] y

Adding group `mongodb' (GID 120) ...
Adding user `mongodb' to group `mongodb' ...
Adding user mongodb to group mongodb
Setting up mongodb-org-shell (5.0.3) ...
Setting up mongodb-database-tools (100.5.1) ...
Setting up mongodb-org-mongos (5.0.3) ...
Setting up mongodb-org-database-tools-extra (5.0.3) ...
Setting up mongodb-org-database (5.0.3) ...
Setting up mongodb-org-tools (5.0.3) ...
Setting up mongodb-org (5.0.3) ...

Start MongoDB

We have completed the installation of MongoDB what is remaining is to start the service and enable it to start on boot. Use the following command to start the service.

$ sudo systemctl start mongod.service

You can check the status of the mongoDB if running with the following command:

$ sudo systemctl status mongod

SAmple output will look like this:

# sudo systemctl status mongod
● mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)
     Active: active (running) since Tue 2021-10-26 12:28:31 UTC; 7min ago
   Main PID: 3750 (mongod)
     Memory: 62.3M
        CPU: 2.377s
     CGroup: /system.slice/mongod.service
             └─3750 /usr/bin/mongod --config /etc/mongod.conf

Oct 26 12:28:31 ubuntu-21 systemd[1]: Started MongoDB Database Server.

Make sure to see as active and running else the service is not running

Now we can enableMongoDB to start on reboot. Use the following command:

$ sudo systemctl enable mongod

Output will look like this:

# sudo systemctl enable mongod
Created symlink /etc/systemd/system/ → /lib/systemd/system/mongod.service.

That is all for MongoDB.


We have accomplished the task of installing MongoDB in our Ubuntu 21.04 server, You have also enable the MongoDB to start on reboot which eliminates manual operation of enabling it every time.

About Kipkoech Sang

I am a technology enthusiast who loves to share gained knowledge through offering daily tips as a way of empowering others. I am fan of Linux and all other things open source.
View all posts by Kipkoech Sang →

Leave a Reply

Your email address will not be published.