Phone :  +370 (5) 204-1903
Email: sales@1gbits.com

How To Install MongoDB on Ubuntu 20.04

In this tutorial, we will go through the process of installing and setting up MongoDB on your Ubuntu 18.04 server. MongoDB is an open-source and free document database which are used in many modern web applications. It is different from the traditional table-based databases like MySQL and PostgreSQL as it doesn’t rely on the table-based relational database structure.

Ubnutu Tutorials Sep 11, 16 by admin 6 min Read
How To Install MongoDB on Ubuntu 20.04

It is classified as a NoSQL database. Instead of using the traditional method, the data is stored in flexible JSON-like documents where fields differ from document to document. MongoDB does not require a predefined schema before adding data to the database and the data structure can be changed over time.

Now that we got the basics in place, let’s go ahead and start setting up the latest version of MongoDB on Ubuntu 20.04 and manage it as a systemd service.


 Before going further with the tutorial, you will need the following:

  • An Ubuntu 20.04 server
  • A user with Sudo privileges
  • General knowledge of using Linux commands

Installing MongoDB on Ubuntu 20.04

Ubuntu’s official package repositories include a stable version of MongoDB by default. At the writing of this article, the latest stable version is 4.4. That being said, if you want to include the most recent version of MongoDB, you should include the MongoDB dedicated package repository to your APT sources. To check for the latest MongoDB version, click the link here.

To start, import the public GPG key for the latest stable version of MongoDB by running the following command.

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

In the above code, cURL prints the content of the GPG key file and adds the key to your list of trusted keys. The –fsSL is used as a security measure. If by any chance, cURL won’t be able to contact the GPG server, it won’t accidentally add the resulting error code to the trusted keys.

If the key was added successfully, you will see a result as OK. To double-check, enter the following code and you will see the MongoDB key in the output.

apt-key list

At this point, your APT installation still doesn’t know where to find the mongodb-org package you need to install the latest version of MongoDB. To fix this issue, we are going to create a file in the sources.list.d directory with the name mongodb-org-4.4.list.

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

Let me explain a bit of the following code and what it does:

  • The deb part means that the source entry is a regular Debian architecture reference.
  • The arch=amd64,arm64 specifies the architecture the APT data should be downloaded.
  • The next part is the URL from where the APT data can be downloaded from.
  • The next part specifies which version of MongoDB you want (4.4) and the package available for the focal release of Ubuntu - focal/mongodb-org/4.4
  • The multiverse points to Ubuntu’s multiverse repository.

Once the command is run, update the server’s local package index.

sudo apt update

Now you can install MongoDB by:

sudo apt install mongodb-org

When the installation finishes, you will have MongoDB installed on your system. But this is not the end, we will need to configure it before we can use it.

Starting MongoDB and making the configurations

The previous steps made it possible to run MongoDB using systemd commands. To start the MongoDB service enter the following command:

sudo systemctl start mongod.service

Now check the service status using the following command:

sudo systemctl status mongod

You will receive an output like this.

mongod.service - MongoDB Database Server
    Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)
    Active: active (running) since Tue 2021-04-06 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

Now that we know all is in order, let's go ahead and enable MongoDB to start up at boot:

sudo systemctl enable mongod

Now let’s go-ahead to the last part: Manage MongoDB service.

Managing the MongoDB service

As mentioned previously, the installation process described in the installation process configures MongoDB to run as a systemd service. This means that you can manage it using standard systemctl commands as you would with other Ubuntu system services.

As seen in the above step, you can use the systemctl status command to check the status of the MongoDB service:

sudo systemctl status mongod

If you would like to stop the service, you can enter this command:

sudo systemctl stop mongod

To start it, type:

sudo systemctl start mongod

While the service is running, you can restart the server by:

sudo systemctl restart mongod

If you would like to disable the feature to start MongoDB automatically during start up, you can enter the following command:

sudo systemctl disable mongod

The same way you can enable this feature:

sudo systemctl enable mongod


In this tutorial, we were able to add the official MongoDB repository to the APT instance and installed the latest version of MongoDB. Now for the next steps, we would recommend hardening the security behind the MongoDB service so that you can accept remote connections.

Be sure to check the official MongoDB documentation to go further with the next process. We hope you liked the following article, and if so, let us know in the comments below.

author img


Leave A Comment