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 - https://www.mongodb.org/static/pgp/server-4.4.asc | 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 ] https://repo.mongodb.org/apt/ubuntu 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 ] https://repo.mongodb.org/apt/ubuntu 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 ] 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: Install MongoDB 4.4 on Ubuntu 20.04|18.04|16.04

Update package database and install MongoDB packages:

$ sudo apt update
Hit:1 http://repo.mysql.com/apt/ubuntu bionic InRelease
Hit:2 http://mirror.hetzner.de/ubuntu/packages focal InRelease
Hit:3 http://archive.ubuntu.com/ubuntu focal InRelease
Hit:4 http://mirror.hetzner.de/ubuntu/packages focal-updates InRelease
Hit:5 http://archive.ubuntu.com/ubuntu focal-updates InRelease
Hit:6 http://mirror.hetzner.de/ubuntu/packages focal-backports InRelease
Hit:7 http://mirror.hetzner.de/ubuntu/packages focal-security InRelease
Hit:8 http://archive.ubuntu.com/ubuntu focal-backports InRelease
Ign:9 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 InRelease
Hit:10 http://security.ubuntu.com/ubuntu focal-security InRelease
Get:11 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 Release [5,377 B]
Get:12 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 Release.gpg [801 B]
Get:13 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4/multiverse amd64 Packages [5,058 B]
Get:14 https://repo.mongodb.org/apt/ubuntu 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
      Docs: https://docs.mongodb.org/manual
  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 127.0.0.1:27017         0.0.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://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=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:
db.enableFreeMonitoring()
---

> use test_db # This will create database called test_db
switched to db test_db
> db # Show current database
test_db
> 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
bye

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

LEAVE A REPLY

Please enter your comment!
Please enter your name here