• Get In Touch
June 27, 2016

How to Install MongoDB on Ubuntu-16.04

Want your very own server? Get our 1GB memory, Xeon V4, 25GB SSD VPS for £10.00 / month.
Get a Cloud Server

In this tutorial, we will learn how to install MongoDB on Ubuntu-16.04.

Requirements

A server running Ubuntu-16.04.
A non-root user account with sudo privilege set up on your server.

Adding the MongoDB Repository

Before starting, you will need to update your system. You can do this by running the following commands:

sudo apt-get update -y

sudo apt-get upgrade -y

By default, MongoDB is available in Ubuntu repository, but the official MongoDB repository provides most up-to-date version and is the recommended way of installing the software.

First, you will need to import the key for the legitimate MongoDB repository.
You can do this by running the following command:

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

You should see the following output:

    Executing: gpg --ignore-time-conflict --no-options --no-default-keyring --homedir /tmp/tmp.AN8by4sfLO --no-auto-check-trustdb --trust-model always --keyring /etc/apt/trusted.gpg --primary-keyring /etc/apt/trusted.gpg --keyring /etc/apt/trusted.gpg.d/awn-testing-ppa.gpg --keyring /etc/apt/trusted.gpg.d/deluge-team-ppa.gpg --keyring /etc/apt/trusted.gpg.d/kubuntu-ppa-backports.gpg --keyring /etc/apt/trusted.gpg.d/transmissionbt-ppa.gpg --keyring /etc/apt/trusted.gpg.d/zorin-os-packages.gpg --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927
    gpg: requesting key EA312927 from hkp server keyserver.ubuntu.com
    gpg: key EA312927: public key "MongoDB 3.2 Release Signing Key " imported
    gpg: Total number processed: 1
    gpg:               imported: 1  (RSA: 1)

Next, you will need to add the MongoDB repository details so apt will know where to download the packages from.

You can do this by running the following command:

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

After adding the repository details, you will need to update the packages list.

To update packages list, run:

sudo apt-get update -y

Installing MongoDB

Now, you can install the MongoDB by running the following command:

sudo apt-get install -y --allow-unauthenticated mongodb-org

You should see the following output:

    Reading package lists... Done
    Building dependency tree       
    Reading state information... Done
    The following packages were automatically installed and are no longer required:
      apport-hooks-elementary contractor javascript-common libgda-5.0-4
      libgda-5.0-common libgranite-common libgranite3 libgsignon-glib1
      libindicate5 libjs-jquery libnoise-core0 libtagc0
    Use 'sudo apt autoremove' to remove them.
    The following additional packages will be installed:
      mongodb-org-mongos mongodb-org-server mongodb-org-shell mongodb-org-tools
    The following NEW packages will be installed:
      mongodb-org mongodb-org-mongos mongodb-org-server mongodb-org-shell
      mongodb-org-tools
    0 upgraded, 5 newly installed, 0 to remove and 168 not upgraded.
    Need to get 47.4 MB of archives.
    After this operation, 218 MB of additional disk space will be used.
    Get:1 http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.2/multiverse amd64 mongodb-org-shell amd64 3.2.6 [5,257 kB]
    Get:2 http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.2/multiverse amd64 mongodb-org-server amd64 3.2.6 [9,541 kB]
    Get:3 http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.2/multiverse amd64 mongodb-org-mongos amd64 3.2.6 [4,337 kB]
    ...................................................................................

The above command will install several packages containing latest stable version of MongoDB along with helpful management tools for the MongoDB server.

Verifying MongoDB

Once MongoDB has been installed, you will need to create system unit file, which determines how to start or stop the service, when should it be automatically started at boot, and whether it is dependent on other software to run.

You can do this by creating mongodb.service file inside /etc/systemd/system directory:

sudo nano /etc/systemd/system/mongodb.service

Add the following content:

    [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

Once you have finishes, you can start the newly created service using the following command:

sudo systemctl start mongodb

You can also check that the service has started properly, run the following command:

sudo systemctl status mongodb

You should see the following output:

    mongodb.service - High-performance, schema-free document-oriented database
       Loaded: loaded (/etc/systemd/system/mongodb.service; disabled; vendor preset:
       Active: active (running) since Fri 2016-06-20 11:30:52 IST; 2min 23s ago
     Main PID: 7078 (mongod)
        Tasks: 16 (limit: 512)
       CGroup: /system.slice/mongodb.service
               └─7078 /usr/bin/mongod --quiet --config /etc/mongod.conf

    June 20 11:30:52 linux systemd[1]: Started High-performance, schema-free document
    lines 1-9/9 (END)

Now, you will need to enable the MongoDB service that start automatically, when system starts.
You can do this by running the following command:

sudo systemctl enable mongodb

You should see the following output:

    ```
    Created symlink from /etc/systemd/system/multi-user.target.wants/mongodb.service to /etc/systemd/system/mongodb.service.
    ```

Conclusion:

I hope you can now easily install MongoDB on your system. MongoDB is a great NoSQL database that can be configured quickly and used in any of our applications. For Node applications, you can start up MongoDB quickly so that we can get to the fun part, building applications.

Want your very own server? Get our 1GB memory, Xeon V4, 25GB SSD VPS for £10.00 / month.
Get a Cloud Server

Share this Article!

Related Posts

Node.js Authentication – A Complete Guide with Passport and JWT

Node.js Authentication – A Complete Guide with Passport and JWT

Truth be told, it’s difficult for a web application that doesn’t have some kind of identification, even if you don’t see it as a security measure in and of itself. The Internet is a kind of lawless land, and even on free services like Google’s, authentication ensures that abuses will be avoided or at least […]

Node.js and MongoDB: How to Connect MongoDB With Node

Node.js and MongoDB: How to Connect MongoDB With Node

MongoDB is a document-oriented NoSQL database, which was born in 2007 in California as a service to be used within a larger project, but which soon became an independent and open-source product. It stores documents in JSON, a format based on JavaScript and simpler than XML, but still with good expressiveness. It is the dominant […]

Using MySQL with Node.js: A Complete Tutorial

Using MySQL with Node.js: A Complete Tutorial

Although data persistence is almost always a fundamental element of applications, Node.js has no native integration with databases. Everything is delegated to third-party libraries to be included manually, in addition to the standard APIs. Although MongoDB and other non-relational databases are the most common choice with Node because if you need to scale an application, […]

Node.Js Vs Django: Which Is the Best for Your Project

Node.Js Vs Django: Which Is the Best for Your Project

Django and NodeJs are two powerful technologies for web development, both have great functionality, versatile applications, and a great user interface. Both are open source and can be used for free. But which one fits your project best? NodeJs is based on JavaScript, while Django is written in Python. These are two equally popular technologies […]

Nodejs Vs PHP:  Which Works Best?

Nodejs Vs PHP: Which Works Best?

Before getting into the “battle” between Node.js and PHP we need to understand why the issue is still ongoing. It all started with the increased demand for smartphone applications, their success forcing developers to adapt to new back-end technologies that could handle a multitude of simultaneous requests. JavaScript has always been identified as a client-side […]