Website Vidya
DeploymentStep-by-Step Guide

How to Install MongoDB on Ubuntu 18.04 LTE

MongoDB is a free and opensource No SQL database written in C++ and used in modern web application development. This tutorial will give you a great understanding of how to install MongoDB on Ubuntu 18.04 LTE. Perquisites Before installing M...

RR
Website Vidya TeamLead Systems Educator
Published:
4 min read
How to Install MongoDB on Ubuntu 18.04 LTE
AI Executive SummaryCORE CIS AI

4 min read

MongoDB is a free and opensource No SQL database written in C++ and used in modern web application development. This tutorial will give you a great understanding of how to install MongoDB on Ubuntu 18.04 LTE. Perquisites Before installing M...

Key Highlights
  • Perquisites
  • Steps to Install MongoDB on Ubuntu
Grounded in full engineering runbookCORE CIS Ai Engine

MongoDB is a free and opensource No SQL database written in C++ and used in modern web application development. This tutorial will give you a great understanding of how to install MongoDB on Ubuntu 18.04 LTE.

Perquisites

Before installing MongoDB, we need to ensure that the server is up-to-date. So, let’s first update and upgrade the server:

sudo apt-get update && apt-get upgrade –y

Steps to Install MongoDB on Ubuntu

Step 1 — Adding the MongoDB Repository

MongoDB is included in Ubuntu Package repository by default, but we will use official MongoDB repository since it provides the most up-to-date version of the MongoDB.

So, we will add the official MongoDB repository to our server.

Ubuntu verifies that software packages are signed with GPG keys to ensure the authenticity of the packages. So, we need to first import the key for the official MongoDB repository to ensure it’s authenticity. For that we need to run the following command:

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927

Once the keys are imported successfully, you get to see the output:

gpg: Total number processed: 1
gpg:               imported: 1  (RSA: 1)

Now we need to add MongoDB repository details to let advance packaging tool (APT) know the location on the internet from where the packages can be downloaded.

To add MongoDB repository, run the following command:

echo "deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list

Once done, update the package list by running the following command:

sudo apt-get update

Step 2 — Installing MongoDB

Since we are done with all the major configuration we can proceed further and install MongoDB on Ubuntu. To install MongoDB, run the following command:

sudo apt-get install -y mongodb-org

The command above will install all the latest stable packages required to run the MongoDB No SQL database on Ubuntu Server.

Now let’s start the MongoDB server using systemctl command.

sudo systemctl start mongod

Run the following command to check whether the MongoDB service started successfully or not:

sudo systemctl status mongod

If everything works properly, you get to see the following output:

● mongodb.service - High-performance, schema-free document-oriented database
   Loaded: loaded (/etc/systemd/system/mongodb.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2019-02-08 19:27:20 EDT; 1min 30s ago
 Main PID: 4093 (mongod)
    Tasks: 16 (limit: 512)
   Memory: 47.1M
      CPU: 1.224s
   CGroup: /system.slice/mongodb.service
           └─4093 /usr/bin/mongod --quiet --config /etc/mongod.conf

Now we need to enable the MongoDB that will allow the service to start automatically every time the server is restarted. It can be achieved by issuing the following command:

sudo systemctl enable mongod

Step 3 — Adjusting the Firewall

This is completely optional if you want to adjust your firewall. But if you run your web application and database on the same server, it is highly recommended that you adjust your firewall.

I assume you have completed your initial server setup and enabled the firewall on your server.

The first and the foremost thing is to allow access to the MongoDB on its default port 27017. To allow that run the following command:

sudo ufw allow 27017

To allow access to the MongoDB server from the trusted sources like another web application hosting server, run the following command:

sudo ufw allow from your_other_server_ip/32 to any port 27017

To verify the changes, run:

sudo ufw status

You get to see the following output:

Status: active

To                         Action      From
--                         ------      ----
27017                      ALLOW       Anywhere
OpenSSH                    ALLOW       Anywhere
27017 (v6)                 ALLOW       Anywhere (v6)
OpenSSH (v6)               ALLOW       Anywhere (v6)

You can also read the official MongoDB documentation on how to install MongoDB on Ubuntu and other Linux variants.

You may also like other installation guides:

I hope you would find this tutorial on how to install MongoDB on Ubuntu useful. If you have any question or concern, put them in the comment section below. You can also e-mail me on rajesh.rai@websitevidya.com

Written by

Website Vidya Team

Technology educator and researcher at Website Vidya.

Discussion

0
Share your thoughts, ask a question, or suggest a tip...
Write →

No comments on this guide yet. Be the first to share your thoughts or ask a question!

Recommended Reading10 Guides

Related Technical Guides

Tested runbooks, server administration walkthroughs, and troubleshooting guides

Deployment•

How to Install PDO Extension in PHP

PHP Data Objects (PDO) is one of the most important extensions in PHP for working with databases in a secure, consistent, and modern way. If you are building dynamic websites, APIs, CMS platforms, or enterprise applications, PDO is not opti...

RR
Rajesh Rai
Read More
Deployment•

How to Install PHPMyAdmin on Ubuntu: 4 Simple Steps

If you’re working with MySQL or MariaDB on Ubuntu, managing databases purely from the command line can quickly feel overwhelming. That’s where phpMyAdmin comes in. PHPMyAdmin is a web-based application that provides a Graphical User Interfa...

RR
Rajesh Rai
Read More
Deployment•

25 Best Linux Commands Every Sysadmin Should Know

If you’ve spent any time managing Linux servers, you already know this truth: the command line is not optional. It’s where real troubleshooting happens, where performance issues are understood, and where outages are prevented before users e...

RR
Rajesh Rai
Read More
Deployment•

How to Fix Snapd Consuming High CPU / RAM on Linux

If you’ve ever opened your system monitor and noticed snapd quietly eating up your CPU or RAM, you’re not alone. Many Linux users—especially on Ubuntu and Ubuntu-based distributions—eventually run into this issue and wonder what’s going on ...

RR
Rajesh Rai
Read More
Deployment•

How to Fix Broken Packages in Ubuntu: 7 Simple Ways

If you’ve been using Ubuntu for a while, chances are you’ve run into package-related issues at least once. A failed update, an interrupted installation, or conflicting dependencies can easily leave your system in a messy state. When that ha...

RR
Rajesh Rai
Read More