For the complete documentation index, see llms.txt. This page is also available as Markdown.

Installing MongoDB

MongoDB is a NoSQL database that has fully flexible index support and a rich queries database.

This document explains how to install MongoDB (as root/sudo).

Step 1 – Setup Apt Repository

First of all, import GPK key for the MongoDB apt repository on your system using the following command. This is required to test packages before installation

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

Then add MongoDB APT repository url in /etc/apt/sources.list.d/mongodb.list.

Ubuntu 18.04 LTS:

echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb.list

Step 2 – Install MongoDB on Ubuntu

After adding required APT repositories, use the following commands to install MongoDB on your systems. It will also install all dependent packages required for MongoDB.

sudo apt update
sudo apt install -y mongodb

If you want to install a specific version of MongoDB, define the version number as follows:

sudo apt install mongodb-org=4.2.1 mongodb-org-server=4.2.1 mongodb-org-shell=4.2.1 mongodb-org-mongos=4.2.1 mongodb-org-tools=4.2.1

Step 3 – Manage MongoDB Service

After installation, MongoDB will start automatically. To start or stop MongoDB use an init script. For example:

And use the following commands to stop or restart the MongoDB service.

Step 4 – Verify MongoDB Installation

Finally, use the below command to check the installed MongoDB version on your system.

And check the status with:

Step 5 - Test MongoDB

Also, connect MongoDB using the command line and execute some test commands for checking proper working.

Last updated

Was this helpful?