Learning Center
MongoDB

Install MongoDB on Ubuntu 22.04 in 8 Easy Steps

17 Apr 2025
Shahid Ali
Shahid Ali

MongoDB is an adaptive NoSQL database management system with high scalability and performance. Using JSON-like documents instead of a traditional table-based structure differs Mongo from relational databases. This also gives MongoDB enough flexibility to not require a preset schema before adding data to a database. The schema can be altered anytime and as often as necessary without setting up a new database with an updated one.

Basic knowledge of fundamental Linux terminal commands and familiarity with Ubuntu as your OS will help you set up MongoDB. 

And if you’re looking for a reliable, high-performance, and budget-friendly solution for your workflows, Hostman has you covered with Linux VPS Hosting options, including Debian VPS, Ubuntu VPS, and VPS CentOS.

Supported Ubuntu Versions for MongoDB Installation
Copy link

First, we should mention an important detail: Officially, MongoDB supports 64-bit LTS (Long-Term Support) versions of Ubuntu. The two most recent approved versions for MongoDB 8.0 are:

  • Ubuntu 22.04 LTS (Jammy)
  • Ubuntu 24.04 LTS (Noble)

These are advised for manufacturing and development settings alike. Earlier Ubuntu editions like 20.04 are compatible with MongoDB 6.x and 7.x.

You can check your Ubuntu version with:

lsb_release -a

Ensure your system architecture is 64-bit. You can verify your architecture by running:

uname -m

This article will lead you through the MongoDB 8.0 installation on Ubuntu 22.04. But suppose you are installing Mongo on Ubuntu 24.04. The repository will be different from the one you would have used with Ubuntu 22.04. On the 24.04 version, run:

echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-8.0.gpg ] https://repo.mongodb.org/apt/ubuntu noble/mongodb-org/8.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-8.0.list

Using the wrong repository for your Ubuntu version may cause dependency errors or prevent MongoDB from installing correctly. Always match the repository to your system version.

Prerequisites to Ubuntu MongoDB Installation
Copy link

Before proceeding, ensure you have:

  • A cloud server running Ubuntu 22.04.
  • A root user or a user with sudo privileges.
  • Access to the server via SSH.

Step 1: Preparing the Ubuntu System and Authorizing via SSH
Copy link

  1. Connect to your cloud server via SSH:

ssh username@your_server_ip

Replace username and your_server_ip with your actual ones. Then, you will be asked to input the user password. 

  1. Once logged in, update the package index and upgrade installed packages with the commands:

sudo apt update && sudo apt upgrade -y

Step 2: Adding MongoDB Repository
Copy link

  1. Import the MongoDB GPG key for package verification.

To import the MongoDB GPG open key for version 8.0, begin by opening your terminal. Next, input the command below to fetch and add the key:

curl -fsSL https://www.mongodb.org/static/pgp/server-8.0.asc | \
   sudo gpg -o /usr/share/keyrings/mongodb-server-8.0.gpg \
   --dearmor

After this, the system will return an 'OK' status, meaning your key has successfully been added to the MongoDB instance! 

Once you've done so, the key import process should proceed smoothly. However, if your system doesn't have GNU Privacy Guard (gnupg) installed, you may encounter an error. To resolve this, use the following terminal command to install the program:

sudo apt-get install gnupg

Then run the import command again. It should be successful.

  1. Add the MongoDB repository to your system:

You’re at a point where your APT setup isn’t yet aware of where to locate the mongodb-org package for installing MongoDB's latest version.

APT, when fetching and installing packages, scans two specific locations on your server for online sources: the sources.list file and the sources.list.d directory. The sources.list file itemizes active APT data sources, each on a separate line, prioritizing the most preferred ones. Meanwhile, the sources.list.d directory is where you can incorporate additional sources as separate files.

Execute this command to generate a new file named mongodb-org-8.0.list within the sources.list.d directory.

echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-8.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/8.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-8.0.list
  1. Update the package index once more.

Now that you’ve executed the command, proceed by refreshing your server’s local package index. This step is crucial for ensuring APT can locate the mongodb-org package:  

sudo apt-get update

Step 3: Installing MongoDB on Ubuntu
Copy link

With the package index updated, you’re ready to install MongoDB:

  1. Install MongoDB using the command:

sudo apt-get install -y mongodb-org

When prompted, press Y and then ENTER to confirm your intention to install the package.

Once the installation process is complete, MongoDB will be successfully installed on your system. However, it’s not yet operational. Your next step involves starting MongoDB and verifying its functionality.

  1. Start the MongoDB service and enable it to start automatically on boot.

You'll need to manually start the MongoDB service using the systemctl command. Here's the command you should run:  

sudo systemctl start mongod

Once you’ve verified that the MongoDB service is running as expected, you should enable it to start automatically at boot. Here’s the command to achieve that:

sudo systemctl enable mongod

Executing this command will configure the MongoDB service to start up automatically every time the system boots up or is restarted.

Step 4: Verifying MongoDB Installation
Copy link

Let's now verify MongoDB's running state to be sure it is as expected.

You may do this using the systemctl command without mentioning the .service suffix:

sudo systemctl status mongod

This command will provide a result similar to the following, thereby verifying the operational state of the MongoDB service and free-running conditions:

Image1

An active (running) status indicates MongoDB is functioning properly.

Step 5. MongoDB Configuration
Copy link

The controller of MongoDB's behavior is its main configuration file (/etc/mongod.conf) that is generated automatically during installation.

We want to open and safely edit this file using a terminal-based text editor; nano should fit:

sudo nano /etc/mongod.conf

This file contains multiple sections that influence how MongoDB runs. For instance:

  • systemLog controls logging behavior.
  • net contains network-related settings.

Be cautious when you make changes to a config file. Incorrect modification can cause the MongoDB service to fail or unintentionally expose your server.

Each section includes parameters. For example, within systemLog, the path parameter specifies where logs are saved:

/var/log/mongodb/mongod.log

Instead of deleting any parameter or line, comment it out by adding a # at the beginning. This makes it easy to reverse changes later.

After saving your changes, restart the MongoDB service to apply them:

sudo systemctl restart mongod

Step 6. Creating a New Database
Copy link

Installing MongoDB generates an administrative database generally utilized for administrative needs like authentication. Therefore, it's a good idea to build a different database for your application data. 

First, open the MongoDB shell:

mongosh

Next, the use command is needed to create or switch to a database. If the database exists, this command simply switches to it. Otherwise, it will create a new one.

Switch to the default admin database:

use admin

Create a new database called partners:

use partners

To view a list of all databases on your system:

show dbs

Step 7. Creating a New User
Copy link

MongoDB doesn’t come with a default user account. You need to create users manually and assign them the appropriate roles.

Start by switching to the database where you want the user to be created—typically admin for users with broad access:

use admin

Then create a new user with full permissions using db.createUser():

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

You can format this function across multiple lines or write it in one. Just make sure capitalization is correct, as MongoDB is case-sensitive.

MongoDB provides several built-in roles like dbAdmin, dbOwner, and readWrite. For better security, only assign the minimum required privileges to each user.

To list all users in the current database:

show users

To test your new user’s connection, exit the Mongo shell:

exit

Then, run the following command in your terminal, replacing placeholders with actual values:

mongosh --port [port] -u [username] -p '[password]' '[database]'

Step 8: Managing MongoDB Service
Copy link

To stop MongoDB, use the command:

sudo systemctl stop mongod

To restart MongoDB, use the command:

sudo systemctl restart mongod

To disable MongoDB, use the command:

sudo systemctl disable mongod

Conclusion
Copy link

You have successfully finished this guide on MongoDB installation on Ubuntu 22.04! Whether you are developing a small-scale personal project or a major enterprise system, this is a fundamental first step toward effectively storing, managing, and retrieving your data. MongoDB's adaptability and power will help you now manage many different applications.

Regular upgrading and following best practices can help you keep your MongoDB instance safe; security is something you should be aware of as your system develops rather than a one-time setup. Your managed database environment will determine both industry compliance and private data protection.