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 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:// --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 xenial/mongodb-org/3.3 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.3.list

Ubuntu 14.04

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

Ubuntu 12.04

$ sudo echo "deb 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:

Description=High-performance, schema-free document-oriented database

ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf

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
  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.

If you liked this article, then do subscribe to email alerts for Linux tutorials. If you have any questions or doubts? do ask for help in the comments section.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

6 thoughts on “How to Install MongoDB on Ubuntu 16.04/14.04 LTS”

  1. Facing following error
    sudo apt-get install -y mongodb-org
    Reading package lists… Done
    Building dependency tree
    Reading state information… Done
    E: Unable to locate package mongodb-org

    Is is there any way to install mongodb offline????

    • @A

      Which version of Ubuntu are you installing on? You have probably not added the appropriate repository, resulting into the error your facing.

      But if you have added the repo, try to update the system package list and install the package as shown above.


Got something to say? Join the discussion.

Have a question or suggestion? Please leave a comment to start the discussion. Please keep in mind that all comments are moderated and your email address will NOT be published.

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