Install MongoDB on Ubuntu 22.04

How to Install MongoDB on Ubuntu 22.04 [Steps Guide]

Explore MongoDB, the popular open-source NoSQL database known for its flexibility and scalability. Ideal for modern web apps, it excels in managing vast volumes of unstructured data. Discover how to install MongoDB on Ubuntu 22.04 easily and unleash its power for your projects.

Have you ever wanted to set up MongoDB on your Ubuntu 22.04 but got tangled up in confusing installation steps and outdated repositories? Don't worry; in this article, we got you covered. MongoDB is a popular open-source, NoSQL document database widely utilized in modern web applications for its scalability, flexibility, and performance. Unlike traditional SQL databases, MongoDB stores data in JSON-like, flexible documents, allowing dynamic schema changes over time.

This comprehensive tutorial will cover the installation of MongoDB, including both the Community and Enterprise editions while emphasizing the importance of securing MongoDB through authentication, SSL/TLS encryption, and firewall rules. The steps provide detailed instructions, commands, and code examples, offering insights from installation to practical usage on Ubuntu 22.04 LTS. Whether you're a developer, manager, or Linux system administrator, this guide ensures a smoother MongoDB installation experience, bypassing the limitations of default repositories and ensuring access to the latest MongoDB version on your Ubuntu system.

For Ubuntu 22.04 users seeking to use MongoDB's capabilities, the installation process can sometimes be confusing due to outdated default repositories. This guide aims to simplify the installation on Ubuntu 22.04, ensuring users access the latest MongoDB version from external sources. By following these steps, you can effortlessly set up and secure MongoDB, enabling you to leverage its robust features seamlessly within your Ubuntu 22.04 environment.

What purposes does MongoDB serve?

MongoDB serve

MongoDB is an ideal choice for creating applications like web or mobile apps that handle large amounts of quickly changing, semi-structured, or unstructured data. Its adaptable data model makes it a favoured option among developers aiming to construct applications capable of efficiently scaling and managing extensive data processing needs.

What is the latest version of MongoDB in 2024

The latest stable release, MongoDB 7.0, launched in August 2013, is significant. It is backed by support for on-premise deployments and MongoDB Atlas, a multi-cloud database service.

For developers, let's see several critical enhancement that comes with this MongoDB 7.0 version that will elevate developers' experience:

i. Developers get a better experience with things like user role variables, support for detailed updates and deletions in time-series collections, improved refresh times for caches, and hosting of upgrades that can be released in MongoDB 7.0

ii. The security is improved with Queryable Encryption. It lets you encrypt data from your side (client) and perform detailed searches on scrambled data.

iii. Performance is boosted by making searches faster with compound wildcard indexing and providing better diagnostics for sharding issues at different levels like metadata, clusters, databases, and collections. Plus, handling time-series data and big datasets is now smoother.

iv. For Data migration, moving data becomes more accessible and faster with cluster-to-cluster sync in MongoDB 7.0. This feature offers flexibility when syncing data between different database clusters.


Before moving forward, ensure you've installed Ubuntu 22.04 LTS with SSH access and have a user account with sudo rights. Also, verify your system's updates by executing the following command:

  • sudo apt update and sudo apt upgrade
  • sudo apt install gnupg2

If you have confirmed your system is up to date, you can continue to the next phase of installing the MongoDB.

Installing MongoDB on Ubuntu 22.04: A Detailed Guide Step-by-Step

The usual Ubuntu repositories often have older MongoDB versions. Instead, we'll install the latest MongoDB packages directly from MongoDB's official source. Installing MongoDB on Ubuntu involves a few simple steps: enabling the MongoDB repository, adding its security key, and installing MongoDB packages.

Step 1: Installing MongoDB.

1. After verifying your system update with the commands given, you can proceed to install all the necessary dependencies for MongoDB by executing the following command:

sudo apt-get install wget curl gnupg2 software-properties-common apt-transport-https ca-certificates lsb-release

During installation, you might encounter a prompt where the terminal will request the password for the sudo user. At that point, input your sudo user's password when prompted. When you are done with this, you can proceed to add the GPG key for Mongo DB.

2. Import the MongoDB GPG Key.

To add a GPG key, use this set of commands, which is designed to fetch and add the GPG key associated with the MongoDB repository. This key serves to authenticate and validate the packages present within that repository.

wget -nc

cat server-6.0.asc | gpg --dearmor | sudo tee /etc/apt/keyrings/mongodb.gpg >/dev/null

3.Once the public GPG key has been successfully added to your Ubuntu system, the following action involves integrating the MongoDB repository into your system configuration using the added GPG key. To do so, execute the following command in your terminal:

sudo sh -c 'echo "deb [ arch=amd64,arm64 signed-by=/etc/apt/keyrings/mongodb.gpg] jammy/mongodb-org/6.0 multiverse" >> /etc/apt/sources.list.d/mongo.list'

This command will include the MongoDB repository in your system's list of repositories managed by your system package manager.

4. Check on the list of updated packages using the below command. Running this command will refresh the package list, including the packages provided within the MongoDB repository. In the terminal, you will be able to see MongoDB displayed.

sudo apt update

5. Install MongoDB. In this step, you can run the following command in the terminal to install it.

sudo apt install MongoDB-org

When asked for confirmation when the command runs, just input the letter “y” and press the Enter key to proceed. From this point, you can wait for the installation to be completed.

sudo apt install mongodb-org -y

6. You can verify the status of the MongoDB service by executing the subsequent command:

sudo systemctl status mongod

The output of this command in the terminal can give you the following output, and you will be able to see that the MongoDB server is inactive.

MongoDB server is inactive

To make the MongoDB server active, you will have to run the below command :

sudo systemctl start mongod

Executing this command, you will initiate the MongoDB service to start. For good practice, it is recommended to recheck the MongoDB server  status by

sudo systemctl status mongod:

The status you need to expect in the output terminal is that it needs to be active (running).

If you wish to set the MongoDB service to automatically start during boot-up, execute the following command:

sudo systemctl enable mongod

Finally, use the version command to confirm the proper installation of MongoDB:

$ mongod --version

mongod --version

Following these steps will effectively complete the installation of MongoDB on an Ubuntu system.

Step 2: Establish a MongoDB database and create a user within it.

At this point, your MongoDB instance should be operational and set up for remote access. Now, let's move on and delve into crafting a database and establishing a user within MongoDB. You can proceed to enter the following command in your terminal to access MongoDB:


As you plan to create a DB, you will enter the MongoDB shell, where you'll encounter helpful information about MongoDB. This includes details such as the MongoDB version, the MongoDB shell version, and a URL to access documentation for Mongosh. Just before the Mongo shell prompt appears, a warning will be visible. It highlights that access control for the database has yet to be activated, thereby restricting read and write access to data and configurations. This warning surfaces due to the absence of authentication, but it will vanish once authentication is enabled for the database.

After installation, three default databases are generated: admin, config, and local. Therefore, to display a list of these existing databases, execute the following command:

show dbs

show dbs

From the output of the three databases displayed on your terminal, you can now create a new database, and you will need to utilize the ‘use‘ command followed by the desired database name. For instance, to create a database named ‘company‘, execute the following command:

Use company

To verify the current database, execute the ‘db‘ command. In this scenario, you'll receive ‘company‘ as the output, confirming the database you're presently using.

Use this command to generate the output of your database:


Creating a new user in a MongoDB database involves using the db.createUser method, which requires specifying the username, password, and assigned roles in JSON format. Here's an example of creating a user named ‘kelvin' with read and write permissions for the ‘company' database: Here is the code to create the new user in the company database:

create the new user

Remember to replace ‘your_password_here' with the password you'd like to set for the ‘kelvin' user.

If you want to list all the users, you can view the list of created users using the db.getUsers() method. And you will get an output in the terminal.

Also, you can alternatively use the command below for listing the users:

Show users.

In case you need to drop/delete a user associated in your DB, for example, you can use the following command ‘db.dropUser’ to drop the user:

dropUser("kelvin", {w: "majority", timeout: 4000})

The output will be as follows:

{ ok: 1 }

Step 3: Enhancing MongoDB security using a password.

After creating the database with the users, it's crucial to activate authentication for the MongoDB database to enhance security in your database. This step allows users to provide a username and password to access the database. Anyone with server access could view or alter the database without authentication.

To establish a password for MongoDB, adhere to these outlined steps:

1. To connect to the MongoDB shell, run the following command:


This command will launch the MongoDB shell, providing a command-line interface to interact with the database.

2. Connecting or Switching to the Admin Database

Next, proceed to connect or switch to the admin database. Executing this command will transition to the admin database, primarily responsible for user and role management within the database system.

Here is the command to use:

use admin

Next, Substitute ‘admin’ and ‘password’ with your chosen username and password. Running this command will generate a new user with the root role within the admin database, granting comprehensive access to all database resources and functionalities. You will observe this code, and you can update the changes.


 user: "admin",

 pwd: "password",

 roles: [ { role: "root", db: "admin" } ]


If you run the above command, you will get an output with the new user and password

Finally, you can now exit the Mongo shell by running this exit command or using keyboard keys such as CTRL + C:


Step 4: Making Authentication Active.

MongoDB does not require authentication to access the database. To safeguard the database and stop unwanted access, it is advised to enable authentication. To allow MongoDB authentication, take the following actions that can guide you on the authentication process.

1. Modify the configuration file for MongoDB. The MongoDB configuration file will open in the Nano text editor when you run this command:

sudo nano /etc/mongod.conf

2. Locate the configuration file's security section and insert the following lines:



authorization: enabled

Take note of the fact that security starts with no space, yet the authorization parameter is indented.

This will activate MongoDB's authorization function, which demands user authentication using a password to access the database.

3. Once you have edited, you can save the changes and close the text editor.

4. Proceed to restart the MongoDB service by executing the below command. The MongoDB configuration will be updated, and the service will be restarted.

sudo systemctl restart mongod

Step 5: Set up MongoDB for remote access.

By default, MongoDB is configured for local access on the installation server. You'll need to modify the /etc/mongod to enable remote access.conf file, the primary configuration file for MongoDB. This file involves database storage, logging, networking, and process management settings.

To access the configuration file, you can use the text editor by executing the following command:

sudo nano /etc/mongod.conf

Find the section related to ‘network interfaces’ within the configuration file and specifically note the ‘bindIP' value. MongoDB can only accept connections from the same server where it was installed because it is, by default, bound to the loopback address interface,

# network interfaces


port: 27017


To enable remote access, add a comma after the IP address of the Mongo server.

bindIp:, mongo-server-ip

After that, close the configuration file and save the modifications. Restart MongoDB to have these modifications take effect. Use the command below to restart:

sudo systemctl restart mongod

Use the following command to accept inbound connections from a remote computer if UFW is enabled.

sudo ufw allow from remote_machine_ip to any port 27017

Reloading the page will take effect to the changes. Use the command below to reload:

sudo ufw reload

Step 6: Connect to MongoDB from a remote location.

There are two methods for remote access to the MongoDB shell. The default port that MongoDB listens to is 27017, and you may use the Netcat software to open a TCP connection to that port.

Install Netcat on the client's computer using the following instructions if it remains required to be installed.

sudo apt install netcat

Run the following command to then connect to the MongoDB server on port 27017:

nc -zv mongodb_server_ip 27017

The successful connection is shown by the output this command follows.

Connection to mongodb_server_ip 27017 port [tcp/*] succeeded!


As an alternative method, you can use Mongo Shell to log in by executing the following command:

mongosh "mongodb://username@mongo_server_ip:27017"

From here, You will be prompted by the shell to enter the admin user's password automatically.

Step 7: Opening Firewall Port (UFW)

The instructions below can be used to open the MongoDB port (27017) in the firewall on a machine that is running ufw:

1. Verify the firewall's status. You can verify the status using this command below, displaying the firewall's current state. You must first activate the firewall if it isn't running before you can open the MongoDB port.

sudo ufw status

2. Use the following command to activate the firewall if it isn't already functioning. This command will accept inbound connections to the system and turn on the firewall.

sudo ufw enable

3. Open the firewall's MongoDB port. With the help of this command, the firewall's MongoDB port (27017) will open and accept inbound connections.

sudo ufw allow 27017

4. To confirm that the MongoDB port is open, look at the list of available ports.

sudo ufw status

Following these steps, the firewall should open the MongoDB port, allowing you to connect to the database from other systems.


Many system admins, developers, and web applications rely on MongoDB mainly because of its document-oriented database design. Initially, Ubuntu had a repository in its default APT packages for MongoDB. Unfortunately, that version was outdated and has now been removed from the default apt packages. Hence, to install MongoDB on your Ubuntu system, the process involves adding the public GPG key and incorporating the MongoDB repository into the capable packages list. Subsequently, the installation can be initiated with the “install” command. Once installed, ensure the mongod service is both enabled and started. Access the MongoShell by typing the command “mongosh” to utilize MongoDB.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *