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.