Linux Tutorials, Guides & Latest Tech Stuffs

How to install MongoDB on Ubuntu 18.04 LTS

MongoDB is one of the most popular NoSQL database. It is a open source document oriented database program. MongoDB uses JSON like documents with schemas. This blog post demonstrates how to install MongoDB Server on Ubuntu 18.04 (Bionic Beaver).

Import MongoDB Public key

Public keys are used by package management system. In Ubuntu, package management tools like dpkg and apt check for package consistenecy and authenticity by requiring that distributors sign packages with GPG key.

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

Create a list file for latest MongoDB Version

As now current latest MongoDB version is 4.0. we’ll create /etc/apt/sources.list.d/mongodb-org-4.0.list file.

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

Update repository and Install MongoDB

sudo apt-get update
sudo apt-get install -y mongodb-org

MongoDB Installation Error on Ubuntu 18.04

If you are getting error of unmet dependencies as shown in below output

Reading package lists... Done
Building dependency tree 
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
 mongodb-org : Depends: mongodb-org-server but it is not going to be installed
E: Unable to correct problems, you have held broken packages.

you can install libcurl3 Using following command

sudo apt install libcurl3

Starting MongoDB

After installation of MongoDB, you should check mongod service status first by using command

sudo service mongod status

This will show you following output, As you can see in this output
Active: inactive (dead)
It means currently MongoDB is inactive.

● mongod.service - MongoDB Database Server
   Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: 
   Active: inactive (dead)
     Docs: https://docs.mongodb.org/manual

For starting  MongoDB, We’ll use systemctl

sudo systemctl start mongod

MongoDB running status can be verified by using another systemctl command

sudo systemctl status mongod

This will print following output on terminal

● mongod.service - MongoDB Database Server
Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)
Active: active (running) since Fri 2018-07-20 20:53:13 IST; 2min 58s ago
Docs: https://docs.mongodb.org/manual
Main PID: 4582 (mongod)
CGroup: /system.slice/mongod.service
└─4582 /usr/bin/mongod --config /etc/mongod.conf

Jul 20 20:53:13 nix systemd[1]: Started MongoDB Database Server.

Now You see the MongoDB status is Active: active (running), It means MongoDB is successfully installed and ready to use.

At the end, you need to enable MongoDB using system to start it automatically.

sudo systemctl enable mongod

Pranav K

Pranav K is a software engineer and all-round computer geek. His interests include AWS, Ubuntu and Wordpress

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.