(Last Updated On: July 1, 2018)

In this guide, we will cover the steps to install the latest release MongoDB community edition on your Ubuntu 18.04/ Ubuntu 16.04 server. 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.

There are two ways of installing MongoDB on Ubuntu systems.

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

This guide will demonstrate installation of MongoDB on Ubuntu 18.04 and Ubuntu 16.04 system using apt repository method.

Install MongoDB on Ubuntu 18.04 / Ubuntu 16.04 from APT repository

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-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4
Executing: /tmp/apt-key-gpghome.d9nDIyNXqn/gpg.1.sh --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4
gpg: key 68818C72E52529D4: public key "MongoDB 4.0 Release Signing Key <packaging@mongodb.com>" imported
gpg: Total number processed: 1
gpg: imported: 1

Step 2: Add apt repository

After importing GPG key, proceed to add the repository. As of this writing, this is only applicable to Ubuntu 16.04, though the repo will work fine on Ubuntu 18.04.

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

Step 3: Install mongodb

Update package database and install mongodb packages:

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

If you want to install MongoDB server on Ubuntu 18.04 from Ubuntu upstream repositories, use:

$ sudo apt-get install mongodb

The default version installed from this is 3.6, but version installed from MongoDB repository is 4.0

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

$ sudo systemctl start mongod

Enable the service to start on boot using:

$ sudo systemctl enable mongod

Check status using:

$ sudo systemctl status mongod
● mongod.service - MongoDB Database Server
Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
Active: active (running) since Sun 2018-07-01 16:41:28 UTC; 18s ago
Docs: https://docs.mongodb.org/manual
Main PID: 3087 (mongod)
CGroup: /system.slice/mongod.service
└─3087 /usr/bin/mongod --config /etc/mongod.conf

The service should be listening on port 27017

# ss -tunelp | grep 27017
tcp LISTEN 0 128 127.0.0.1:27017 0.0.0.0:* users:(("mongod",pid=3087,fd=11)) uid:113 ino:27258 sk:45 <-> 

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.0.0
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 4.0.0
{
"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