ManagedCloud Servers

High performance handled and monitored by us 24/7/365. A complete solution to provide you with our in house expertise 24/7 tailored to your specific needs. We'll setup a bespoke server for your site using the latest tehnologies so you can get the most out of your hardware and get your website loading quickly and reliably. Find out more..

cPanelCloud Servers

Recommended - High performance cloud servers with no technical knowledge required. If you're hosting multiple websites already and you're looking to consolidate, or if you're looking to isolate yourself from the shared hosting environment but you don't have the time or knoweldge to manage a server, then the Managed cPanel Servers are for you. Find out more..

UnmanagedCloud Servers

Our unmanaged range gives you complete control at rock bottom prices and our cloud platform boasts super fast multipath 40Gb/s network, the latest Intel Xeon V3 CPUs and enterprise grade redundant SSDs. If you're a sysadmin look no further, we offer some of the best specification to price ratio servers available. Find out more..

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

View Plans

How to Install MongoDB on Ubuntu-16.04

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 -ysudo 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, 20GB SSD VPS for £10.00 / month.

View Plans