How to install MongoDB on your Linux server

Aktualisiert am 2026-10-23 00:01:39 31 Besuche

In this manual, we will teach you how to install MongoDB on your Linux server so that you can start using this NoSQL database.

info In this manual, we provide the installation steps for the Ubuntu 22.04 distribution. For other distributions, especially RedHat, CentOS, or Suse, the steps to follow may be different. Superuser permissions are required to follow the steps in this manual.

Before starting the tutorial, do you not have a Cloud Linux Ubuntu 22.04 server where you can install your MongoDB database server? Don't worry! With just one click, you can easily deploy it with SW Hosting.

If you prefer a server with MongoDB already pre-installed, you can use our easy-to-deploy OneClickApps.

We will review some of the concepts that define this database management system.

What is MongoDB?

MongoDB is the name of one of the database management systems that has gained the most popularity in recent years. Furthermore, it is a fundamental part of the well-known 'MEAN Stack': a set of technologies consisting of MongoDB, Nginx, Angular, and NodeJS. More and more companies are adopting it as their preferred option for web application development.

One of the most important aspects to keep in mind about MongoDB is that it is a NoSQL database management system, so its operation is drastically different from that of MySQL, MariaDB, or PostgreSQL.

What does NoSQL mean?

The term 'No SQL' or 'Non-relational' means that the database does not use tables to store entries or relate them to each other. Instead, entries are stored as independent objects, frequently in JSON format.

NoSQL databases greatly facilitate horizontal scalability, allowing the database to be distributed across multiple servers. Unlike a relational database, latency in server synchronization is not a problem: collections (similar to tables) may be out of sync for brief moments, but the integrity of the stored objects is maintained at all times.

The flexibility of NoSQL databases makes them ideal for applications with large volumes of data or real-time web applications.

How to install MongoDB on Linux Ubuntu 22.04

1. Import the MongoDB repository public key

First, we must check if we already have gnupg and curl installed. If not, we will proceed with their installation:

sudo apt-get install gnupg curl

Once verified, we must import the MongoDB GPG public key by running the following command:

curl -fsSL https://pgp.mongodb.com/server-6.0.asc | sudo gpg -o /usr/share/keyrings/mongodb-server-6.0.gpg --dearmor

This key is necessary to verify the authorship of the installed packages. It is a standard and very common procedure when adding a repository.

2. Add the MongoDB repository

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

:warning: This repository is specific to version 6.0 of MongoDB compatible with Ubuntu 22.04.

The previous command adds the repository to a .list file. This is the procedure recommended by the MongoDB developers.

3. Update the repository list

apt-get update

The previous command will update the list of repositories and available packages, including the new MongoDB repository we added in the previous step.

4. Install MongoDB using the package manager

sudo apt-get install -y mongodb-org

info The package name is 'mongodb-org' and not 'mongodb'. This package comes from the official repositories we added and offers the latest stable version of MongoDB. The package offered by the Ubuntu repositories might be outdated.

5. Disable automatic updates to avoid compatibility issues (recommended)

A recommended practice is to disable automatic updates that could cause compatibility issues. It is better to install updates manually.

To disable updates, enter each of the following commands separately:

echo 'mongodb-org hold' | sudo dpkg --set-selections
echo 'mongodb-org-database hold' | sudo dpkg --set-selections
echo 'mongodb-org-server hold' | sudo dpkg --set-selections
echo 'mongodb-mongosh hold' | sudo dpkg --set-selections
echo 'mongodb-org-mongos hold' | sudo dpkg --set-selections
echo 'mongodb-org-tools hold' | sudo dpkg --set-selections

In the future, to reactivate automatic updates:

echo 'mongodb-org install' | dpkg --set-selections
echo 'mongodb-org-server install' | dpkg --set-selections
echo 'mongodb-org-shell install' | dpkg --set-selections
echo 'mongodb-org-mongos install' | dpkg --set-selections
echo 'mongodb-org-tools install' | dpkg --set-selections

success That's it! If you have followed the instructions correctly, you will have successfully installed the official version of MongoDB.

Remember that if you do not yet have a Cloud server with a Linux operating system, you can easily deploy one with SW Hosting.

Additionally, if you prefer to avoid performing all these steps, you can deploy a server with MongoDB installed by default by using our easy-to-deploy OneClickApps.