You can support us by downloading this article as PDF from the Link below. Download the guide as PDF

In this guide, we will cover the steps to install MongoDB 4 on Ubuntu 20.04|18.04|16.04 Linux system. MongoDB is an open source NoSQL database system written in C++ that provides scalability, high performance/availability. NoSQL database systems are often referred to as Document-oriented databases.

MongoDB common use case is storage and management of Big Data-sized collections of literal documents like text documents, email messages, XML documents and many others.

To get a highly available MongoDB setup, use How to Setup MongoDB Replication On Ubuntu LTS guide.

Install MongoDB 4.4 on Ubuntu 20.04|18.04|16.04

There are two ways of installing MongoDB on Ubuntu systems.

  • Install MongoDB from apt repository – recommended
  • Install MongoDB from a downloaded.deb package

This guide will demonstrate installation of MongoDB 4 on Ubuntu 18.04 and Ubuntu 16.04 system using apt repository method. Let’s now install MongoDB on Ubuntu 20.04/18.04 / Ubuntu 16.04 Linux.

Step 1: Import MongoDB public GPG Key:

Before you can install any package from MongoDB apt repository, you need to download and import GPG key to your system.

sudo apt update
sudo apt install gnupg
wget -qO - | sudo apt-key add -

Step 2: Add MongoDB 4.4 APT Repository to Ubuntu 20.04|18.04|16.04

After importing GPG key, proceed to add the repository.

Ubuntu 20.04:

echo "deb [ arch=amd64,arm64 ] focal/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list

Ubuntu 18.04:

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

Ubuntu 16.04:

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

Step 3: Install MongoDB 4.4 on Ubuntu 20.04|18.04|16.04

Update package database and install MongoDB packages:

$ sudo apt update
Hit:1 bionic InRelease
Hit:2 focal InRelease
Hit:3 focal InRelease
Hit:4 focal-updates InRelease
Hit:5 focal-updates InRelease
Hit:6 focal-backports InRelease
Hit:7 focal-security InRelease
Hit:8 focal-backports InRelease
Ign:9 focal/mongodb-org/4.4 InRelease
Hit:10 focal-security InRelease
Get:11 focal/mongodb-org/4.4 Release [5,377 B]
Get:12 focal/mongodb-org/4.4 Release.gpg [801 B]
Get:13 focal/mongodb-org/4.4/multiverse amd64 Packages [5,058 B]
Get:14 focal/mongodb-org/4.4/multiverse arm64 Packages [3,962 B]
Fetched 15.2 kB in 2s (6,472 B/s)
Reading package lists... Done
Building dependency tree
Reading state information... Done

The install MongoDB server package on Ubuntu 20.04|18.04|16.04:

sudo apt install -y mongodb-org

To install a specific release, you must specify each component package individually along with the version number, as in the following example:

sudo apt-get install -y mongodb-org=4.4.1 mongodb-org-server=4.4.1 mongodb-org-shell=4.4.1 mongodb-org-mongos=4.4.1 mongodb-org-tools=4.4.1

The service name is mongod , you can start the application by running:

sudo systemctl enable --now mongod

Check status using:

$ systemctl status mongod
● mongod.service - MongoDB Database Server
    Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
    Active: active (<strong>running</strong>) since Thu 2019-10-17 05:57:30 UTC; 32s ago
  Main PID: 20493 (mongod)
    CGroup: /system.slice/mongod.service
            └─20493 /usr/bin/mongod --config /etc/mongod.conf
 Oct 17 05:57:30 ubuntu18 systemd[1]: Started MongoDB Database Server.

The service should be listening on port 27017

$ netstat -tunelp | grep 27017
(Not all processes could be identified, non-owned process info
  will not be shown, you would have to be root to see it all.)
 tcp        0      0*               LISTEN      112        53728      -          

MongoDB main configuration file is /etc/mongod.conf You can tweak the settings to your liking, but remember to restart mongod service whenever you make a change.

Test connection:

$ mongo --eval 'db.runCommand({ connectionStatus: 1 })'
MongoDB shell version v4.4.1
connecting to: mongodb://
Implicit session: session { "id" : UUID("615620f1-c987-4a1c-b9c2-c16cf962065f") }
MongoDB server version: 4.4.1
	"authInfo" : {
		"authenticatedUsers" : [ ],
		"authenticatedUserRoles" : [ ]
	"ok" : 1

You can confirm that everything is working fine from "ok" : 1.You can also try to create a test database and insert some dummy data.

# mongo
To enable free monitoring, run the following command:

> use test_db # This will create database called test_db
switched to db test_db
> db # Show current database
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
> db.files.insert({"name":"rap"}) # Insert data to db
WriteResult({ "nInserted" : 1 })
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
test_db 0.000GB
> db.dropDatabase() # Drop our test db
> exit

That’s all on how to install MongoDB 4.4 on Ubuntu 20.04|18.04|16.04 Linux server. Until next time, stay connected and enjoy your Tech life.

Related guides:

How To Install MongoDB 4 on RHEL 8 / CentOS 8

You can support us by downloading this article as PDF from the Link below. Download the guide as PDF


Please enter your comment!
Please enter your name here