How to Install MongoDB on Ubuntu 20.04

Mongo DB is an open-source, document-oriented, cross-platform database. MongoDB is classified as a NoSQL database, used to store high volumes of data. In MongoDB, documents and collections are used instead of traditional tables. In this type of database, information is stored in a flexible format, such as JSON documents, in which fields change from one document to another. This database does not require any predefined schema and the data structure can change over time. It is necessary to know how to install MongoDB on both Windows and Linux operating systems.

This article will show you how to install MongoDB on Ubuntu 20.04 using the command line from the official repository. All commands discussed in this article related to installation of MongoDB will be executed on Ubuntu 20.04.

Install MongoDB on Ubuntu 20.04

Perform the following steps to install MongoDB on your Ubuntu 20.04 system:

First, open the terminal window with the shortcut Ctrl + Alt + t. Log in as a root user on your system.

Add MongoDB Repository

To install the latest MongoDB version on your system, you must first add its repository to Ubuntu 20.04. To add the official MongoDB package repository key using wget, run the following command on the terminal:

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

Next, add the MongoDB repository to Ubuntu 20.04, as follows:

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

Install MongoDB on Ubuntu 20.04

In the previous step, the MongoDB official repository and key have been included in your system. In this step, you will install MongoDB on your Ubuntu 20.04 system. It is recommended that you first update your system, before installing any software packages. To update your system, run the following command:

$ sudo apt update

Next, you will install MongoDB on your system. To install MongoDB, use the following command:

$ sudo apt-get install mongodb-org -y

Manage Services of MongoDB

Once you have finished installing MongoDB, using the following commands, you can start, stop, and enable the MongoDB services automatically when your system boots up.

$ sudo systemctl stop mongod.service
$ sudo systemctl start mongod.service
$ sudo systemctl enable mongod.service

Verify the status of MongoDB services (it will either be running, or not) with the following command:

$ sudo systemctl status mongod

Launch MongoDB Shell

MongoDB direct listens at port 27017. Connect the MongoDB shell via the following command:

$ mongo --host

Use the following command to open the MongoDB console:

$ mongo

Add Admin User

To create and add an admin user, use the following command on the MongoDB shell:

> USE admin

You have now been logged in as an admin user on the MongoDB server. Create a new admin user with the following command:

> db.createUser({USER:"admin", pwd:"new_password_here", roles:[{ROLE:"root", db:"admin"}]})

In the next step, to secure your database, you must enable login authentication. Open the configuration file of MongoDB using the following command:

$ sudo nano /lib/systemd/system/mongod.service

The following output screen will display on the terminal:

Edit the authentication with the following text:

authorization: enabled

Edit and save the file to keep these changes. Run the following command to restart MangoDB services:

$ sudo systemctl restart mongod

Now, only authenticated users have access to the MongoDB database, as follows:

$ mongo -u admin -p --authenticationDatabase admin

After executing the above command, you will be asked to enter the password. Enter the password, and the MongoDB console will display on the terminal.

To exit the MongoDB shell, use the following command:



In this article, you learned how to install and configure MongoDB with Ubuntu 20.04. You can explore more MongoDB shell commands using the command line in Ubuntu 20.04.

About the author

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.