Oct 10, 2023 7 min read

How to Install MongoDB on Ubuntu 20.04

Install MongoDB on Ubuntu 20.04 with our step-by-step tutorial. It is a NoSQL database, ideal for handling vast amounts of unstructured data.

Install MongoDB on Ubuntu 20.04
Table of Contents


Before, we begin talking about the steps to install MongoDB on Ubuntu 20.04. First, let's understand - What is MongoDB?

MongoDB is a popular NoSQL database, ideal for handling vast amounts of unstructured data. It offers high performance, scalability, and flexibility. With its JSON-like documents, MongoDB allows easy integration with web applications. As a leading database solution, it empowers businesses to manage data efficiently and create dynamic applications effortlessly.

Advantages of MongoDB

  1. Flexible schema: MongoDB's schema-free approach allows easy data model changes.
  2. High performance: With its indexing and query optimization, MongoDB ensures speedy data retrieval.
  3. Scalability: Supports horizontal scaling for handling large datasets and high traffic.
  4. JSON-like documents: Simplifies data integration with web applications.
  5. Replication & fault tolerance: Ensures data redundancy and reliability for uninterrupted operations.

Prerequisites to Install MongoDB on Ubuntu 20.04

You will need the following to complete this tutorial:

Step 1 — Installing MongoDB

MongoDB is available in Ubuntu's official package repositories. The most recent stable release of MongoDB, as of this writing, is 4.4, although the version that is currently accessible from the default Ubuntu repositories is 3.6.

You must add MongoDB's dedicated package repository to your APT sources if you want to use the most recent version of this software. After that, you can install mongodb-org, a meta-package that always points to the most recent MongoDB release.

Use the command below to import the public GPG key for the most recent stable MongoDB version. If you want to install a different version of MongoDB than 4.4, make sure to modify the URL component of this script to match that.

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

In various operating systems, there is a command-line tool called cURL that is used to transfer data. It reads whatever information is stored at the URL supplied to it and displays it to the system's output. In the example below, cURL outputs the contents of the GPG key file and then pipes it into the sudo apt-key add - command, which adds the GPG key to your list of trusted keys.

Also, note that this curl command includes the -fsSL options, which tell cURL to fail silently. This means that if cURL is unable to reach the GPG server or the GPG server is unavailable, it will not add the resulting error code to your list of trusted keys.

If the key was successfully added, the command will return OK:



You can use the following command to confirm that the key was added properly if you would like to:

apt-key list

The output will include the MongoDB key somewhere:


pub   rsa4096 2019-05-28 [SC] [expires: 2024-05-26]
      2069 1EEC 3521 6C63 CAF6  6CE1 6564 08E3 90CF B1F5
uid           [ unknown] MongoDB 4.4 Release Signing Key <[email protected]>
. . .

At this stage, your APT installation is still unable to locate the mongodb-org package that is required to install the most recent MongoDB version.

The server's sources.list file and sources.list.d directory are the two locations where APT looks for online sources of packages to download and install. sources.list is a file that includes active APT data sources, one per line, with the most preferred sources mentioned first. You can add such sources.list entries as separate files to the sources.list.d directory.

Execute the following command to generate the mongodb-org-4.4.list file in the sources.list.d directory. There is only one line of text in this file, which reads deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse:

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

APT receives all the information it requires about what the source is and its location in this one line:

  • deb: This indicates that a standard Debian architecture is mentioned in the source entry. In other instances, this section of the line can read deb-src, meaning the source entry contains the source code for a Debian distribution.
  • [ arch=amd64,arm64 ]: This identifies which architectures the APT data must be downloaded to. It specifically mentions the amd64 and arm64 architectures in this instance.
  • https://repo.mongodb.org/apt/ubuntu: This is a URI that represents the location of the APT data. The URL in this instance points to the HTTPS address of the official MongoDB repository.
  • focal/mongodb-org/4.4: Ubuntu repositories can include multiple distinct releases. This specifies that you only want the mongodb-org package in version 4.4 to be made accessible for the focal release of Ubuntu (“Focal Fossa” being the code name of Ubuntu 20.04).
  • multiverse: This section points APT to one of the four main Ubuntu repositories. It is pointing to the multiverse repository in this case.

Update the local package index on your server after running this command so that APT knows where to look for the mongodb-org package:

sudo apt update

After that, you can install MongoDB:

sudo apt install mongodb-org

When prompted, type Y, followed by ENTER, to confirm that you wish to install the package.

Your machine will have MongoDB installed once the command is completed. It is not, however, yet ready for usage. The next step is to launch MongoDB and ensure that it is operating properly.

Step 2 — Starting the MongoDB Service and Testing the Database

The previous step's installation automatically configures MongoDB to run as a daemon controlled by systemd, which means you may manage MongoDB using the various systemctl commands. Nevertheless, this installation procedure does not start the service automatically.

Start the MongoDB service by using the systemctl command as follows:

sudo systemctl start mongod.service

Check the status of the service next. It is worth noting that this command excludes .service from the service file definition. If this suffix is not already present, systemctl will automatically add it to every argument you supply, so it isn’t necessary to include it:

sudo systemctl status mongod

The following output will be returned by this command while the service is active:


● mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)
     Active: active (running) since Tue 2020-06-09 12:57:06 UTC; 2s ago
       Docs: https://docs.mongodb.org/manual
   Main PID: 37128 (mongod)
     Memory: 64.8M
     CGroup: /system.slice/mongod.service
             └─37128 /usr/bin/mongod --config /etc/mongod.conf

Enable the MongoDB service to start at boot after making sure it is functioning as expected:

sudo systemctl enable mongod

By connecting to the database server and running a diagnostic command, you can further confirm that the database is operational. The next command will establish a connection to the database and print its current version, server address, and port. In addition, it will return the outcome of the internal connectionStatus command used by MongoDB:

mongo --eval 'db.runCommand({ connectionStatus: 1 })'

The database connection status will be checked and returned by connectionStatus. When the ok field in the response has a value of 1, it means that the server is functioning as expected:


MongoDB shell version v4.4.0
connecting to: mongodb://
Implicit session: session { "id" : UUID("1dc7d67a-0af5-4394-b9c4-8a6db3ff7e64") }
MongoDB server version: 4.4.0
	"authInfo" : {
		"authenticatedUsers" : [ ],
		"authenticatedUserRoles" : [ ]
	"ok" : 1

Additionally, the database is running on port 27017 on, which is the local loopback address for localhost. This is the default port number for MongoDB.

We will then examine using systemd to manage the MongoDB server instance.

Step 3 — Managing the MongoDB Service

As already mentioned, the installation procedure outlined in Step 1 sets up MongoDB to function as a systemd service. This indicates that you can manage it using regular systemctl commands, just as you would with other Ubuntu system services.

As previously stated, the systemctl status command verifies the MongoDB service status:

sudo systemctl status mongod

The service can be terminated at any moment by typing:

sudo systemctl stop mongod

To restart the service once it has been stopped, type:

sudo systemctl start mongod

Even when the server is already running, you can restart it:

sudo systemctl restart mongod

In Step 2, you allowed MongoDB to start automatically with the server. Type the following if you want to disable this automatic startup:

sudo systemctl disable mongod

Run the enable command again to re-enable it to start up at boot:

sudo systemctl enable mongod

Check out Systemd Essentials: Working with Services, Units, and the Journal for further details on how to manage systemd services.

FAQs to Install MongoDB on Ubuntu 20.04

What is the command to add the MongoDB repository?

Use this command: sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4

How can I create a MongoDB list file?

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

What is the next step after creating the list file?

Update your packages list with: sudo apt update.

How do I install the latest version of MongoDB?

Use: sudo apt install -y mongodb-org.

How can I start the MongoDB service?

Run: sudo systemctl start mongod.

How do I enable MongoDB to start on system boot?

Use: sudo systemctl enable mongod.

Are there any security steps to follow after installation?

Yes, secure your MongoDB instance by enabling authentication, setting up user accounts, and configuring firewall rules to restrict access.


In this tutorial, you installed the most recent version of MongoDB and added the official MongoDB repository to your APT instance. After that, you practiced some systemctl commands and tested Mongo's functionality.

The next thing you should do is follow our instructions on How to Secure MongoDB on Ubuntu 20.04 to strengthen the security of your MongoDB installation. After securing it, you can set up MongoDB to accept remote connections.

If you have any suggestions or queries, kindly leave them in the comments section.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.