Install MongoDB on Ubuntu 20.04

MongoDB is an open-source document-oriented database system. It is a NoSQL database.

Instead of storing data in tables of rows or columns like traditional RDBMS databases, MongoDB stores data as documents. Documents consist of fields and value pairs. Documents are stored as JSON format and internal as BSON format. A collection is a group of MongoDB documents.

MongoDB comes with two editions - Community edition and Enterprise. The community edition is completely free.

In this tutorial, you will learn how to install MongoDB Community Edition on Ubuntu 20.04.

Step 1: Import MongoDB public key

MongoDB is available in the Ubuntu repository. But it's not maintained by MongoDB Inc. If you already installed the mongodb package, uninstall it first. Then proceed with the following steps.

Let's first Import the MongoDB public GPG key using the wget command:

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

To verify the added GPG key, type:

$ sudo apt-key list

Step 2: Add MongoDB repository to source list

Let's add the official MongoDB repository to the source list file - this will allow us to fetch the latest official mongodb-org package.

To create a source list file, type:

$ sudo touch /etc/apt/sources.list.d/mongodb-org-5.0.list

Now, add the repository source for Ubuntu 20.04:

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

Now, update the apt index:

$ sudo apt-get update 

Step 3: Install MongoDB CE on Ubuntu 20.04

Once the repository is added, MongoDB can be installed using the apt command. This will install the latest version available in the MongoDB repository.

To install MongoDB on Ubuntu, type:

$ sudo apt-get install mongodb-org

Apt-get will upgrade the MongoDB when newer versions come available in the repo. To prevent unintended upgrades in the production environment, you need to hold the currently installed packages.

The following commands hold MongoDB and its associated components from upgrading.

$ echo "mongodb-org hold" | sudo dpkg --set-selections
$ echo "mongodb-org-database hold" | sudo dpkg --set-selections
$ echo "mongodb-org-server hold" | sudo dpkg --set-selections
$ echo "mongodb-org-shell hold" | sudo dpkg --set-selections
$ echo "mongodb-org-mongos hold" | sudo dpkg --set-selections
$ echo "mongodb-org-tools hold" | sudo dpkg --set-selections

To Install a specific version of MongoDB, install each component separately along with its version number.

$ sudo apt-get install -y mongodb-org=5.0.1 mongodb-org-database=5.0.1 mongodb-org-server=5.0.1 mongodb-org-shell=5.0.1 mongodb-org-mongos=5.0.1 mongodb-org-tools=5.0.1

MongoDB keeps logs in /var/log/mongodb/mongod.log which include incoming connections, commands run and issues encountered. The data is stored in /var/lib/mongodb and configurations in /etc/mongod.conf.

To start MongoDB service, type:

$ sudo systemctl start mongod

To see the status of MongoDB service, type:

$ sudo systemctl status mongod


● mongod.service - MongoDB Database Server
      Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)
      Active: active (running) since Fri 2021-07-30 08:11:58 UTC; 3s ago
    Main PID: 146519 (mongod)
      Memory: 63.8M
      CGroup: /system.slice/mongod.service
              └─146519 /usr/bin/mongod --config /etc/mongod.conf
 Jul 30 08:11:58 localhost systemd[1]: Started MongoDB Database Server.

To enable MongoDB service on boot, type:

$ sudo systemctl enable mongod

To check which MongoDB version is installed, type:

$ mongod --version


db version v5.0.1
 Build Info: {
     "version": "5.0.1",
     "gitVersion": "318fd9cabc59dc9651f3189b622af6e06ab6cd33",
     "openSSLVersion": "OpenSSL 1.1.1f  31 Mar 2020",
     "modules": [],
     "allocator": "tcmalloc",
     "environment": {
         "distmod": "ubuntu2004",
         "distarch": "x86_64",
         "target_arch": "x86_64"

Here we have installed MongoDB version 5.0.1.

How to Run MongoDB

Mongosh is the MongoDB shell used to test queries and do operations with the database. It comes along with the MongoDB package.

To start a MongoDB shell type mongosh without any options.

$ mongosh


$ mongosh "mongodb://localhost:27017"

Configure Remote Access

By default, MongoDB is listening for connections on its localhost and default port, 27017. You can verify using ss -ta command.

To allow the server to listen to the remote connection - edit the /etc/mongod.conf configuration file.

$ sudo vim /etc/mongod.conf
 # network interfaces
   port: 27017

Here, <mongodb_server_ip> will be replace by your MongoDB server IP address.

To verify the remote connectivity, type

$ mongosh "mongodb://<mongodb_server_ip>:27017"

Uninstall MongoDB

To completely uninstall Mongodb, remove MongoDB package, data, and log files.

First, stop MongoDB service:

$ sudo service mongod stop

Remove any MongoDB packages that you had previously installed:

$ sudo apt-get purge mongodb-org*

And finally, remove data and log directories:

$ sudo rm -r /var/log/mongodb
$ sudo rm -r /var/lib/mongodb


In this tutorial, we learned how to Install MongoDB on Ubuntu 20.04 with easy steps. If you have any questions, suggestions, feedback please write them in the comment box below.

Leave a Comment