How to Install MongoDB on Ubuntu 16.04/14.04 LTS

In this how to guide we shall go through the various steps of installing the latest version of MongoDB in Ubuntu 16.04 and Ubuntu 14.04 LTS systems. Read Also: How to Install MongoDB on...

Spread the love

In this how to guide we shall go through the various steps of installing the latest version of MongoDB in Ubuntu 16.04 and Ubuntu 14.04 LTS systems.

Read Also: How to Install MongoDB on Ubuntu 18.04

MongoDB is an open source, document database based on the cutting edge technology of NoSQL. It supports development of modern web applications, with features such as strong consistency, flexibility, expressive query languages and secondary indexes plus a lot more.

Additionally, it offers organizations great scalability and performance for building modern applications with powerful and mission-critical databases.


Let us now proceed to how you can get it working on your Ubuntu 16.04 and Ubuntu 14.04 LTS systems by following these steps below:

Step 1: Adding MongoDB Repository

First, we are going to install the latest version of MongoDB, so we need to add the official MongoDB repository which has the latest package version:

$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927
Add MongoDB Key

Add MongoDB Key

After that, we now have to add a file containing the MongoDB repository details under our /etc/apt/sources.list.d/ from which package files will be fetched.

Now run the following command as per your version of Ubuntu:

Ubuntu 16.04

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

Ubuntu 14.04

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

Ubuntu 12.04

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

Then save the file and close it.

Next, update the system package list as follows:

$ sudo apt-get update

Step 2: Installing MongoDB Database

Then install latest stable MongoDB package by executing the command below:

$ sudo apt-get install -y --allow-unauthenticated mongodb-org
Install MongoDB in Ubuntu 16.04

Install MongoDB in Ubuntu 16.04

In the command above, the option --allow-unauthenticated helps us to authentic the package that we are going to install without having to prompt us to confirm installation of a non-official Ubuntu repository package.

Now that MongoDB has been successfully installed on your system, there are certain important things we are going to do in order to manage it under systemd unit.

The first thing is to create a systemd unit file which defines a unit in this case our MongoDB service. Systemd unit configuration files are stored in /etc/systemd/system directory and we shall create the mongodb.service file under it, use your editor of choice as follows:

$ sudo vi /etc/systemd/system/mongodb.service 

Then add the following line into that file:

mongodb.service

[Unit]
Description=High-performance, schema-free document-oriented database
After=network.target
[Service]
User=mongodb
ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf
[Install]
WantedBy=multi-user.target
Create SystemD MongoDB Service

Create SystemD MongoDB Service

A quick explanation of the file sections:

  1. Unit: contains a human readable description of the service and dependencies that need to be started for MongoDB service to work as required. The dependency here is the network.target.
  2. Service: specifies how the system should start a service depending on the directives provided. The User directive specifies user to run the MongoDB server and ExecStart directive specifies command to start MongoDB service.
  3. Install: Directs systemd to start the service at boot time when required.

There are many other directives that you can include in the unit file, you can find more by reading about systemd unit and here is a guide to start with: Create and Run New Service Units in SystemD.

At this point, we can now start our new service by running the command below:

$ sudo systemctl start mongodb

To check the status of the service, issue the following command:

$ sudo systemctl status mongodb
Start and Verify MongoDB Service

Start and Verify MongoDB Service

To make the services start at boot time, use the command below:

$ sudo systemctl enable mongodb

All is now set for you to start using MongoDB for developing databases for your applications.

If you are not familiar with how systemd works, then it is very important that you do some research to help you gain knowledge about it because, unlike other Linux init systems, it works in an absolutely different way.

I hope you find this guide useful, for any questions or additional information, you can use the comment section below to air out your concerns.

Facebook Comments
Spread the love

Posted by News Monkey