Install MongoDB on Ubuntu 20.04 LTS - Step by Step Process ?

Install MongoDB on Ubuntu 20.04 LTS - Step by Step Process ?

MongoDB (known as Mongo) is a document-oriented database management system.

It is categorized as a NoSQL database as it is a non-relational database and does not work with the conventional table-based relational database structure. 

It efficiently works will large-scale data. Some of the well-known companies which use MongoDB are Facebook, Cisco, Forbes, Adobe, Nokia, and so on.

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

In this context, we shall look into how to install MongoDB on the Ubuntu 20.04 LTS system. You can follow the same procedure on previous Ubuntu releases: Ubuntu 18.04 LTS and 16.04 LTS.

Main MongoDB Commands

It's also essential to know some commands for managing MongoDB: 

# systemctl status mongod - shows the status of MongoDB;
# systemctl stop mongod - stops MongoDB;
# systemctl start mongod - starts MongoDB; 
# systemctl restart mongod - restarts MongoDB;
# systemctl disable mongod - prevents MongoDB from starting automatically; 
# systemctl enable mongod - enables MongoDB to start automatically.

Methods to install MongoDB on Ubuntu system

We can install MongoDB on Ubuntu through following two ways:

i. Via package manager

ii. Via archive

Note: You will require sudo access to install the MongoDB on the Ubuntu OS.

1. Install MongoDB Using Package Manager

The MongoDB package available in Ubuntu is not managed by MongoDB Inc. Here, we will show you the installation of the official MongoDB package version 4.4. which is managed by MongoDB Inc. Follow the below instructions to install MongoDB using the Ubuntu package manager.

i. Import MongoDB Public Key

First, you will need to add the MongoDB repository key to your apt keyring. By doing this, your system will trust the added repository. 

Run this command to add the MongoDB key:

$ wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -

If it is added successfully, you will see OK in the output.

ii. Add MongoDB Repository

Add MongoDB repository to your system's list of sources. 

To do so, edit the sources.list file using the command below:

$ sudo nano /etc/apt/sources.list

Append the below line in the file:

deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse

Now save the file and close it.

iii. Update Repository Index

Now use the below command to update your system's repository index:

$ sudo apt update

iv. Install MongoDB

Now the MongoDB repository has been added to our system's list of sources, we can install it as follows:

$ sudo apt install mongodb-org

When prompted with the y/n option, press y to continue.

v. Run MongoDB

To run MongoDB, start the mongod service (daemon for MongoDB) using the command below:

$ sudo systemctl start mongod

If the service does not start or you encounter an error like "service not found", issue the command below:

$ sudo systemctl daemon-reload

After starting the mongod service, check its status to verify if it is running fine. 

Use the command below to do so:

$ sudo systemctl status mongod

If it is running fine, you will see the active (running) status.

To start the MongoDB automatically at each boot, the command is:

$ sudo systemctl enable mongod

Now to start the mongo shell from the same system running the mongod process, the command is as follows:

$ mongo

2. Install MongoDB Using Archive

Although, the recommended way of installing MongoDB on Ubuntu is by using apt package manager as it installs the required dependencies during the installation.

However, you can use archive (.tgz tarball) for manually installing it in Ubuntu. 

Follow the below instructions to install MongoDB using the using official MongoDB archive.

i. Install Dependencies

Use the command below to install the dependencies needed for the installation of MongoDB:

$ sudo apt install libcurl4 openssl liblzma5

When prompted with the y/n option, press y to continue.

ii. Download MongoDB archive

Now you will need to download the MongoDB archive (.tgz tarball). 

Use the below command to download the current latest version of MongoDB to your system:

$ wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu2004-4.4.4.tgz

Alternatively, you can also download MongoDB by accessing their official Downloads page. Access the page and choose the MongoDB version you want to download, your OS platform, and the package (tgz).

Then click Download to download the package.

iii. Extract MongoDB Archive

Extract the MongoDB archive using the command below:

$ tar -xvzf mongodb-linux-x86_64-ubuntu2004-4.4.4.tgz

iv. Copy Binaries to a Directory in PATH environment Variable

Now copy the binaries located in the linux-x86_64-ubuntu2004-4.4.4/bin to one of the directory in the $PATH like /usr/local/bin.

$ sudo cp -v mongodb-linux-x86_64-ubuntu2004-4.4.4/bin /usr/local/bin/

v. Run MongoDB

To run MongoDB, follow the below steps:

1. Create directories for storing data and logs. 

Run the below commands to do so:

$ sudo mkdir -p /var/lib/mongo
$ sudo mkdir -p /var/log/mongodb

2. Use the below command to assign your user account read/write permissions to the MongoDB data directory:

$ sudo chown `whoami` /var/lib/mongo

Then assign permission to log directory:

$ sudo chown `whoami` /var/log/mongodb

3. Run the mongod process (daemon for MongoDB) using the command below:

$ mongod --dbpath /var/lib/mongo --logpath /var/log/mongodb/mongod.log --fork

4. Now to start the mongo shell from the same system running the mongod process, the command is as follows:

$ mongo

How to Uninstall MongoDB from Ubuntu Linux System ?

If you no longer need MongoDB, you can completely uninstall it from your Ubuntu system. To do so, first stop the mongod process:

$ sudo service mongod stop

To remove MongoDB that you have installed using package manager, the command is as follows:

$ sudo apt purge mongodb-org

If you want to remove the logs and data directories too, use the commands below:

$ sudo rm -r /var/lib/mongodb
$ sudo rm -r /var/log/mongodb

If you have installed MongoDB through the archive, you can uninstall it by removing the MongoDB binaries from /usr/local/bin.

[Need urgent assistance in fixing Missing Debian Software Packages? We can help you. ]


This article covers how to install MongoDB on Ubuntu 20.04 LTS using either the apt package manager or by downloading and installing through the archive. For more information, visit MongoDB's official documentation.

MongoDB is an open-source and cross-platform document-oriented database system written in C++. It stores data in collections of JSON-like, flexible documents and used to create powerful websites and applications. 

Due to its scalability and high performance, it is used for building modern applications that require powerful, mission-critical and high-availability databases.

To Configure MongoDB on Ubuntu:

MongoDB default configuration file is located at /etc/mongod.conf

By default, each user will have access to all databases and perform any action. 

For production environments, it is recommended to enable the MongoDB authentication.

i. You can do it by editing the file /etc/mongod.conf:

$ nano /etc/mongod.conf

2. Add the following lines:

  authorization: enabled

3. Save and close the file then restart the MongoDB service to apply the changes:

$ systemctl restart mongod

Advantages of MongoDB:

1. Absence of a schema

2. Based on the collections of various documents

3. A clear structure of every object

4. Highly scalable

5. Internal memory is used to store data, which lets us get data faster.

6. Data is stored as JSON objects.

7. MongoDB supports document-based queries

Search by typing a one letter Word Term Here