Install MongoDB on Ubuntu 18.04

Filed Under: Ubuntu
Install Mongodb On Ubuntu

MongoDB is a free and open-source NoSQL Database used commonly in today’s web applications.

In this tutorial, we will show you how to install and setup MongoDB on your Ubuntu 18.04 machine in a few simple steps!

Step 1: Install MongoDB on Ubuntu Using Apt

Since the MongoDB package is available in Ubuntu’s official repositories, we can simply use apt to install MongoDB on our system.

But before that, update all system critical packages first.

sudo apt update

Now, install the MongoDB package using:

sudo apt install mongodb
install mongodb on Ubuntu
Install MongoDB on Ubuntu

This will install the latest version of MongoDB on our system.

As for any Database System, there is a server and a service associated with the task of Database Management. The MongoDB server is automatically started after the installation.

To ensure that our installation went smoothly, we need to ensure that the MongoDB service and the server are working correctly.

Step 2: Check the MongoDB Service

To check the status of the MongoDB service, use systemctl:

sudo systemctl status mongodb

You’ll get an output similar to the below screenshot.

Mongodb Ubuntu Check Service
Mongodb Ubuntu Check Service

So now that we know that our service is running, let’s verify if our MongoDB Server is responding to our requests or not.

Step 3: Test the MongoDB Server

By default, the MongoDB Server is located at the localhost and uses port 27017. So the address of the server is:

Let’s test our server by running this command to find the Connection Status using mongo command:

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

You should get a response like the below screenshot, having the “ok” field as 1, ensuring that our Connection is valid.

 Check Connection Status
Mongodb Ubuntu Check Connection Status

Step 4: Allow remote access to the Database

By default, the MongoDB server is only allowed by the ufw firewall to operate locally.

To allow access to MongoDB on it’s port 27017 from anywhere, we need to allow incoming connections on this port.

But this is very insecure, since any person can access the Database. To prevent this, only allow IP Addresses which correspond to trusted machines/servers.

sudo ufw allow from TRUSTED_IP/32 to any port 27017

Replace TRUSTED_IP with any trusted IP address in the above command.

This allows connections from your trusted server to reach the MongoDB Database, since ufw now allows these connections.

But we still need to ensure that the MongoDB server does not only listen locally. Therefore, we need to add our trusted machine’s IP to the mongodb.conf configuration file.

Use your favorite text editor and edit /etc/mongodb.conf.

sudo vi /etc/mongodb.conf

Go to the bind_ip value.

Mongodb Config File
Mongodb Ubuntu Config File

Add your trusted machine’s public IP Address here, by appending to the field using a comma (,).

The modified file will look something like this:

Add Server Address
Mongodb Ubuntu Add Server Address

Exit the editor, after saving changes

Step 5: Restart the MongoDB Service

Now, we are almost done! We simply need to restart the mongodb service to apply our changes.

sudo systemctl restart mongodb

We have now successfully configured our MongoDB Server to accept remote connections!


In this tutorial. we showed you how you can install and configure the latest version of MongoDB on your Ubuntu 18.04 system. We also made sure that we can accept specific remote connections to our database from another trusted server.

I hope this served you well and cleared any of your remaining doubts regarding MongoDB installation!

Generic selectors
Exact matches only
Search in title
Search in content