Dec 12, 2022 4 min read

Install MongoDB on Ubuntu 20.04

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

Install MongoDB on Ubuntu 20.04
Install MongoDB on Ubuntu 20.04
Table of Contents


Before we begin talking about how to install MongoDB on Ubuntu 20.04, let's briefly understand – What is MongoDB?

MongoDB is an open-source document database that is free to use. It is a part of the NoSQL database family, which differs from standard table-based SQL databases such as MySQL and PostgreSQL.

Data in MongoDB is saved in flexible, JSON-like documents, with fields that might differ from document to document. The data structure can evolve over time, and there is no requirement for a predefined format.

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

The default Ubuntu repositories contain an outdated version of MongoDB. It is simple to install the most recent version of MongoDB on Ubuntu. We will activate the MongoDB repository, import the repository GPG key, and install the MongoDB server.

Installing MongoDB on Ubuntu 20.04

To install MongoDB on Ubuntu, carry out the subsequent actions as root or as a user with sudo privileges:

1) Installing the prerequisites for adding a new repository through HTTPS is as follows:

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

2) Add the MongoDB repository and import the repository's GPG key with the below command:

wget -qO - | sudo apt-key add -
sudo add-apt-repository 'deb [arch=amd64] focal/mongodb-org/4.4 multiverse'

The most recent MongoDB version is 4.4 as of the time this tutorial was written. Replace 4.4 with your preferred version to install that version.

3) After the repository has been enabled, type the following to install the mongodb-org meta-package:

sudo apt install mongodb-org

Your system will install the following applications:

  • mongodb-org-server⁣ - The mongod daemon, as well as the associated init scripts and configurations.
  • mongodb-org-mongos - The mongos daemon.
  • mongodb-org-shell - MongoDB's interactive JavaScript user interface is called the mongo shell. It is used to carry out administrative activities via the command line.
  • mongodb-org-tools - Stores many MongoDB tools for importing and exporting data, statistics, and other utilities.

4) Type the following commands to launch the MongoDB daemon and have it start on boot:

sudo systemctl enable --now mongod

5) Connect to the MongoDB database server with the mongo tool, and print the connection status to see if the installation was successful:

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

You will get an output like below:


MongoDB shell version v4.4.0
connecting to: mongodb://
Implicit session: session { "id" : UUID("2af3ab0e-2197-4152-8bd0-e33efffe1464") }
MongoDB server version: 4.4.0
  "authInfo" : {
    "authenticatedUsers" : [ ],
    "authenticatedUserRoles" : [ ]
  "ok" : 1

A value of 1 in the ok field signifies success.

Configuring MongoDB

mongod.conf, MongoDB's configuration file, is located in the /etc directory. The document is in YAML format.

In most circumstances, the default configuration settings are adequate. However, we advise uncommenting the security section and turning on authorization for production environments, as illustrated below:

sudo nano /etc/mongod.conf
  authorization: enabled

Role-Based Access Control (RBAC), which limits users' access to database resources and operations, is enabled via the authorization option. Each user will have access to all databases and be able to execute any action if this option is disabled.

For changes to take effect after modifying the MongoDB configuration file, restart the mongod service:

sudo systemctl restart mongod

Visit the Configuration File Options documentation page to learn more about the configuration options included in MongoDB 4.4.

Creating Administrative MongoDB User

If you activated MongoDB authentication, you will need to establish an administrative user who can access and administer the MongoDB instance.

1) To access the mongo shell:


2) To connect to the admin database, enter the following command from inside the MongoDB shell:

use admin

switched to db admin

3) To create a new user named mongoAdmin with the password changeMe and userAdminAnyDatabase role, use the following command:

    user: "mongoAdmin",
    pwd: "changeMe",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]

Successfully added user: {
	"user" : "mongoAdmin",
	"roles" : [
			"role" : "userAdminAnyDatabase",
			"db" : "admin"
Remember to choose a stronger password. You can give the administrative MongoDB user whatever name you prefer.

After that, exit the mongo shell with:


Access the mongo shell using the administrative user you previously created to test the modifications:

mongo -u mongoAdmin -p --authenticationDatabase admin
use admin

switched to db admin

Run show users and you should see details about the recently created user:

show users
	"_id" : "admin.mongoAdmin",
	"userId" : UUID("49617e41-ea3b-4fea-96d4-bea10bf87f61"),
	"user" : "mongoAdmin",
	"db" : "admin",
	"roles" : [
			"role" : "userAdminAnyDatabase",
			"db" : "admin"
	"mechanisms" : [

You can also try accessing the mongo shell without any arguments (just enter mongo) and see if you can list the users using the same commands used above.


In this tutorial, you have learned how to install and configure MongoDB on Ubuntu 20.04. Check out the MongoDB Manual, for more information on this subject.

Please leave a comment below if you have any queries, and we'll be happy to respond to them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Blog - 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.