How to Install MongoDB on Ubuntu 16.04

MongoDB is a cross-platform, document oriented database that provides high performance, high availability, and easy scalability. MongoDB works on concept of collection and document. This article explains how to install MongoDB on Ubuntu 16.04 and start the MongoDB service on boot.

Adding the MongoDB Repository

MongoDB is generally included in Ubuntu package repositories. However, the official MongoDB repository provides the most up-to-date version in a supported manner.

First, import the key for the official MongoDB repository using the following command:

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

The sample output should be like this:

Executing: /tmp/tmp.Qe1RFZgUh2/gpg.1.sh --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 <packaging@mongodb.com>" imported
gpg: Total number processed: 1
gpg:               imported: 1 (RSA: 1)

To add the MongoDB repository, use 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

Now, update the package list using the following command:

$ sudo apt-get update

Installing and Verifying MongoDB

To install MongoDB, use the following command:

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

The sample output should be like this:

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.

Creating a Service Unit File

To properly launch MongoDB as a service on Ubuntu 16.04, we need to create a unit file describing the service. A unit file tells systemd how to manage the MongoDB resource.

Create a configuration file named mongodb.service:

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

Add the following configuration:

[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

Starting and Enabling MongoDB Service

Start the newly created service with systemctl:

$ sudo systemctl start mongodb

Check if the service has started properly:

$ sudo systemctl status mongodb

The sample output should be like this:

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-05-13 10:33:52 IST; 1min 48s ago
Main PID: 7078 (mongod)
   Tasks: 16 (limit: 512)
   CGroup: /system.slice/mongodb.service
           ??7078 /usr/bin/mongod --quiet --config /etc/mongod.conf

To enable MongoDB to start automatically when the system boots, use:

$ sudo systemctl enable mongodb

The output should confirm the service is enabled:

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

Conclusion

You have successfully installed MongoDB on Ubuntu 16.04 and configured it to start automatically on boot. The MongoDB service is now running and ready to accept connections for database operations.

Updated on: 2026-03-15T01:38:03+05:30

632 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements