How to Install MongoDB on Ubuntu

April 4, 2024


MongoDB is a document database useful for storing complex data and modifying documents without affecting existing data structures. Its non-relational (NoSQL) nature makes it a popular choice for web development, as it provides high performance, high availability, and automatic horizontal scaling.

This guide will show you how to install MongoDB on Ubuntu.

How to install MongoDB on Ubuntu.


Install MongoDB on Ubuntu

The best way to install MongoDB on Ubuntu is via the official MongoDB repository. Proceed with the steps below to add the repository to the system, install the necessary packages, and start MongoDB.

Step 1: Add MongoDB Repository

The following section explains how to add the repository containing MongoDB installation packages. Complete this step by opening a terminal emulator and executing the following commands:

1. Import the public key to the Ubuntu server:

curl -fsSL | \
   sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg \

2. Create a list file and add the MongoDB repository to the system's sources list.

On Ubuntu 22.04, type the following command:

echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] jammy/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list

On Ubuntu 20.04, replace the jammy reference with focal:

echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] focal/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list

3. Refresh the local package database:

sudo apt update

The APT output shows the newly added MongoDB repository.

Updating repositories after adding the MongoDB repository.

Step 2: Install MongoDB

Once the MongoDB repository is registered on the system, execute the command below to install MongoDB using APT:

sudo apt install mongodb-org

When prompted, type Y and press Enter.

Installing MongoDB using APT.

Confirm the success of the installation by checking the MongoDB version:

mongod --version

The output shows basic information about the installation:

Basic information about the MongoDB installation.

Step 3 (Optional): Lock MongoDB Version

By default, APT downloads and installs updates as they are released. To prevent unintended upgrades of the MongoDB installation, lock the installed version by executing the following commands:

echo "mongodb-org hold" | sudo dpkg --set-selections
echo "mongodb-org-server hold" | sudo dpkg --set-selections
echo "mongodb-org-shell hold" | sudo dpkg --set-selections
echo "mongodb-org-mongos hold" | sudo dpkg --set-selections
echo "mongodb-org-tools hold" | sudo dpkg --set-selections

The commands show no output.

Step 4: Start MongoDB

With MongoDB installed on the system, use the steps below to start a database shell prompt:

1. Start the MongoDB service with the following systemctl command:

sudo systemctl start mongod

2. To enable launching the service at system startup, enter:

sudo systemctl enable mongod
Enabling launch at boot for the MongoDB instance.

3. Start mongosh (the MongoDB shell) by typing:


The shell prompt appears alongside basic information about the MongoDB instance.

MongoDB shell in Ubuntu.

4. Exit the Mongo shell by pressing Ctrl-C. Alternatively, enter the following command:


Configure Firewall for MongoDB

Note: The following section pertains to users who want to connect remotely to a MongoDB instance. You can skip this section if you plan to install and use MongoDB on the same system.

The recommended way to set up remote connections to a MongoDB instance is to grant access to specific IP addresses only. Proceed with the steps below to set up the system to accept connections from a remote client:

1. MongoDB uses port 27017 to communicate. Create a firewall rule that exposes that port to a specific IP address. The example below shows how to create the rule in Ubuntu's default ufw firewall:

sudo ufw allow from [remote-ip-address]/32 to any port 27017

Replace [remote-ip-address] with the actual IP address of a remote system.

2. Open the mongodb configuration file in a text editor such as Nano:

sudo nano /etc/mongod.conf

3. Find the entry labeled bind_ip, insert a comma, and add the IP address of your remote system:

bind_ip =, [remote-ip-address]
Editing mongod.conf to add a remote system that has access to MongoDB.

Save the file and exit.

4. Restart the MongoDB service:

sudo systemctl restart mongod

Setting up MongoDB Authentication

MongoDB comes with authentication turned off by default. The following section explains how to secure it by turning on authentication and creating a root user and password.

1. Start a MongoDB shell:


2. Switch to the admin database:

use admin

3. Create a root user by entering the following command:

db.createUser({user:"root", pwd:"[password]", roles:[{role:"root", db:"admin"}]})

Replace [password] with a complex and secure password of your choosing.

4. Exit the Mongo shell by pressing Ctrl+C.

5. Open the mongodb.service file in a text editor:

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

6. Find the ExecStart entry in the Service section and add the --auth flag immediately after /usr/bin/mongod:

ExecStart=/usr/bin/mongod --auth --config /etc/mongod.conf
Adding authentication in MongoDB.

Save the file and exit.

7. Reload the system:

sudo systemctl daemon-reload

8. Restart the MongoDB service:

sudo systemctl restart mongod

9. Enter the following command to authenticate the user account:

mongo –u "root" –p ––authenticationDatabase "admin"

10. When prompted, type the password you set above. The mongosh shell prompt appears.


After following this tutorial, you should know how to install MongoDB on your Ubuntu server. The article also showed how to enable secure remote access and set up additional user authentication.

If you are starting to learn about MongoDB, read how to create a MongoDB database.

Was this article helpful?
Vladimir Kaplarevic
Vladimir is a resident Tech Writer at phoenixNAP. He has more than 7 years of experience in implementing e-commerce and online payment solutions with various global IT services providers. His articles aim to instill a passion for innovative technologies in others by providing practical advice and using an engaging writing style.
Next you should read
21 Server Security Tips to Secure Your Server
January 11, 2023

Hackers are always on the lookout for server vulnerabilities. Minimize risks and be confident your data is...
Read more
7 Ways to Reduce Server Response Time
December 20, 2023

Reducing server response time to the bare minimum is one of the primary goals of website owners and SEO...
Read more
How to Install & Setup MEAN Stack on Ubuntu
March 28, 2024

The MEAN stack is an open-source JavaScript (JS) framework used for developing robust web applications. It is...
Read more
How To Install Node.js & NPM on Ubuntu
March 18, 2019

Node.js is an open-source cross-platform JavaScript (JS) runtime environment. It is used for building fast...
Read more