Install MongoDB ( NoSQL ) on Linux (Ubuntu 20.04) - Step by step process ?

NoSQL database is a non-tabular database that stores and manages the data differently than the relational database. 

NoSQL has been adopted by big companies like Amazon, Facebook, Netflix, and Google because of their large volumes of unstructured data which cannot be handle by a relational databases. 

Some of the popular NoSQL databases are, CouchDB, Couchbase, RavenDB, MongoDB, and so on.

Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform MongoDB related queries.

In this context, we shall look into how to install MongoDB NoSQL server on Ubuntu using the apt package manager.

How to install MongoDB NoSQL via the Apt Package Manager ?

To install MongoDB NoSQL on your system, you will need to add the MongoDB package repository. 

After that, you will be able to install the most recent and stable release of MongoDB NoSQL on your system. 

Let's get started with the installation.

1. Import the Public Key for MongoDB

First, we will need to import the public key for the official MongoDB repository. 

To do so, execute the following command in Terminal:

$ wget -qO mongodb.asc https://www.mongodb.org/static/pgp/server-4.4.asc

Then using the below command, add the downloaded key to the apt list of trusted keys:

$ sudo apt-key add mongodb.asc

If you see the OK in the output of the above command, this shows that the key has been added successfully.

Once you have added the key, verify it by executing the following command in Terminal:

$ apt-key list

While adding the key, you may receive an error indicating that gnupg is missing. If this is the case, install gnupg using the below command in Terminal:

$sudo apt-get install gnupg

Once installed, again try to add the key.

2. Create a List File in sources.list.d Directory

Now we will create a list file /mongodb-org-4.4.list for MongoDB in the /etc/apt/sources.list.d directory. 

Execute the following command in Terminal to do so:

$ 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

3. Update Local Repository Index

Now update the local repository index using the following command in Terminal:

$ sudo apt-get update

4. Install the MongoDB Package

Now that our local repository is updated with the MongoDB repository, we can install the latest version of MongoDB. 

i. Here is the command to install the MongoDB in your system:

$ sudo apt-get install mongodb-org

Now you may be asked to choose y/n (yes/no) option to continue the process. 

Press y to continue, after which installation of MongoDB will be started on your system.

ii. Once installed, you can verify the installation and view the installed version of MongoDB as follows:

$ mongod --version

5. Setup Firewall

If a firewall is enabled on your system, MongoDB can still be accessed locally from the same system. 

However, if you need to access MongoDB from another system on the network or from the internet, you will need to add a rule in the firewall.

To allow incoming connection to MongoDB from a specific IP address, add the following rule:

$ sudo ufw allow from ip_address/ to any port 27017

To allow all incoming connection to MongoDB, add the following rule:

$ sudo ufw allow 27017

6. Start Service

Now we can start the MongoDB service. Use the below command to do so:

$ sudo systemctl start mongod.service

After running the above command, if you experience this error:

Failed to start mongod.service: Unit mongod.service not found.

Then execute the below command to reload the systemd and try to start the service again.

$ sudo systemctl daemon-reload

Then execute the below command to verify whether the service is running:

$ sudo systemctl status mongod.serivce

Then you will see that MongoDB service is active and running.

Now execute the below command to enable the MongoDB service to start automatically at boot:

$ sudo systemctl enable mongod.service

In case you need to stop the MongoDB service, execute the below command in Terminal:

$ sudo systemctl stop mongod.service

To restart the MongoDB service, execute the below command in Terminal:

$ sudo systemctl restart mongod.service

How to Uninstall MongoDB ?

In case you no longer need the MongoDB NoSQL, follow the below steps to completely remove it from your system.

1. Stop MongoDB service using the below command:

$ sudo service mongod stop

2. Now, remove MongoDB along with all the configuration files:.

$ sudo apt-get purge mongodb-org*

3. To delete the MongoDB logs, use the below command:

$ sudo rm -r /var/log/mongodb

To delete the MongoDB libraries, use the below command

$ sudo rm -r /var/lib/mongodb

[Need urgent assistance to install MongoDB on your Ubuntu Linux Server? We are available to help you. ]


This article will guide you on how to add MongoDB’s official #repository and install the latest stable release of MongoDB #NoSQL in the #Ubuntu 20.04 LTS system. 

Also, you will learn how to completely uninstall from the system in case you no longer need it.

MongoDB stores data in db folder within data folder. But, since this data folder is not created automatically, you have to create it manually. 

Remember that data directory should be created in the root (/).

To get stats about #MongoDB server, type the command db. stats() in MongoDB client. 

This will show the database name, number of collection and documents in the database.

In MongoDB, data is stored as documents. 

These documents are stored in MongoDB in JSON (JavaScript Object Notation) format. 

#JSON documents support embedded fields, so related data and lists of data can be stored with the document instead of an external table.