How to install MongoDB on Ubuntu 18.04/20.04/16.04.

We will see how to install MongoDB on Ubuntu 18.04, Ubuntu 16.04, and Ubuntu 20.04 or you can say that we can install MongoDB of any version on Ubuntu.

MongoDB is a No-SQL database which is written in C++, It uses a JSON like structure. MongoDB is a cross-platform and document-oriented database.

The initial release of the MongoDB was on 11 February 2009, you can find the main website of the MongoDB as well the git repository

So let’s install MongoDB on the Ubuntu 18.04, Ubuntu 16.04, and on Ubuntu 20.04.

Also, find the cheat sheet for the Quick Installation, kindly come to read the whole post for the better knowledge of MongoDB installation.

Cheat Sheet of How to Install MongoDB on Ubuntu.

STEP 1: Install the GnuPG and add the key to your Ubuntu Server.

For Ubuntu 16:

$ sudo apt-get install gnupg

For Ubuntu 18 and Ubuntu 20:

$ sudo apt install gnupg

Add the Key to Ubuntu Server 16/18/20

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

STEP 2: Add the repository into your Ubuntu Server

For Ubuntu 20:

$ 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

This will add the repo into the file “/etc/apt/sources.list.d/mongodb-org-4.4.list”

For Ubuntu 18:

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

For Ubuntu 16:

$ echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list

STEP 3: Update the repository and Install MongoDB 4.4 on Ubuntu

For Ubuntu 16:

$ sudo apt-get update
$ sudo apt-get install -y mongodb-org

For Ubuntu 18/20:

$ sudo apt update
$ sudo apt install -y mongodb-org

STEP 4: Start the Service of MongoDB Server on Ubuntu

$ sudo systemctl start mongod

Check the Status:

$ sudo systemctl status mongod

Restart the service:

$ sudo systemctl restart mongod

Stop the service:

$ sudo systemctl stop mongod

Start the service at the time of Booting

$ sudo systemctl enable mongod

STEP 5: Use the “mongo” command in the terminal or BASH to connect with MongoDB.

$ mongo

STEP 1: Install the GnuPG and add the key to your Ubuntu Server.

For Ubuntu 16:

$ sudo apt-get install gnupg

For Ubuntu 18 and Ubuntu 20:

$ sudo apt install gnupg

After the installation of the GnuPG package let’s add the key to our server.

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

After adding it will give the output as “OK” on your terminal or bash

For the different version, we have to just make the changes in the above command, let assume we want to install version 4 of the MongoDB.

So the command to install MongoDB 4 on Ubuntu will be:

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

STEP 2: Add the repository into your Ubuntu Server

For Ubuntu 20:

$ 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

So we have added the repository of MongoDB 4.4 into the file /etc/apt/sources.list.d/mongodb-org-4.4.list.

Now suppose you have to Install 4.0 version of MongoDB, so you will just make the changes in the above repo URL like:

$ echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list

For Ubuntu 18:

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

For Ubuntu 16:

$ echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list

STEP 3: Update the repository and Install MongoDB 4.4 on Ubuntu

So first, we will update the repository of MongoDB by using the update command.

For Ubuntu 16:

$ sudo apt-get update

For Ubuntu 18/20:

$ sudo apt update

After successfully Update of the repo we will install the MongoDB server in Ubuntu.

For Ubuntu 16:

$ sudo apt-get install -y mongodb-org

For Ubuntu 18/20:

$ sudo apt install -y mongodb-org
How to install MongoDB on ubuntu 18.04, 20.04, 16.04.

STEP 4: Start the Service of MongoDB on Ubuntu

So after the successful installation, we will start the services of the MongoDB server by “systemctl command”

$ sudo systemctl start mongod
$ sudo systemctl status mongod

So, here we can see that the service of MongoDB is active and running on the Ubuntu server.

If we have to restart the service of MongoDB then use the restart option with systemctl.

$ sudo systemctl restart mongod

If we have to stop the service of MongoDB then use the stop option with the systemctl command.

$ sudo systemctl stop mongod

What if we want to start the service when my server got rebooted then use the enable option with systemctl.

$ sudo systemctl enable mongod

So we have successfully installed the MongoDB server and use “mongo” in terminal or bash to check are you able to connect.

$ mongo

Allow MongoDB to connect from Remote IP

If you have to connect your MongoDB server from a Remote IP, then you have to make some configuration changes and restart the service of MongoDB.

So first we will look into the configuration file of the MongoDB server “/etc/mongod.conf”

# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1

So you will find that the bind IP is 127.0.0.1 which will allow to local only, and we want to allow to any remote IP then change the bind IP to “0.0.0.0”

# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0

After that, we will require a restart to MongoDB service, so that we can access the port 27017 to any remote IP.

$ sudo systemctl restart mongod

If still not working then check your firewall, which will be active, now you have two option can inactive the firewall or you can allow the 27107 port number.

To inactive the firewall use “sudo ufw disable”, but it can be dangerous if your server has another application or you are in the production environment.

So it’s better to allow the port 27017 for remote IP’s only.

$ sudo ufw allow 27017

So by allowing this anyone can access the 27017 port to your server then check the status of ufw.

$ sudo ufw status

Output:
Status: active
To Action From
-- ------ ----
27017 ALLOW Anywhere
27017 (v6) ALLOW Anywhere (v6)

If you are facing any issue while installing MongoDB let us know, we will help you out.

Thanks to reading the Article, learn Linux basic command, and how to install MySQL.

install MongoDB on Ubuntu, install MongoDB, install MongoDB on Ubuntu 18.04, install MongoDB on Ubuntu 20.04, install MongoDB on linux, MongoDB install Ubuntu

4 Replies to “How to install MongoDB on Ubuntu 18.04/20.04/16.04.

Give your valuable time