· Tutorial ·

How to install MongoDB on your Linux server

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

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

Before we start with the tutorial, don't you have an Ubuntu 22.04 Linux Cloud server on which you can install your MongoDB database server? Don't worry, with just one click, you can easily deploy it with SW Hosting.

Your Cloud with Ubuntu 22 from $5.60/month

Get the most out of your project with the fastest disks and most powerful CPUs in the Cloud.

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

Your Cloud MongoDB from $5.60/month

Start your project with the best NoSQL database system. Your MongoDB in a fully scalable and customisable Cloud.

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

What is MongoDB?

MongoDB is the name given to one of the most popular database management systems in recent years. It is also a fundamental part of the well-known "MEAN Stack": a set of technologies made up of MongoDB, Nginx, Angular and NodeJS. And increasingly, companies are adopting it as their preferred choice for web application development.

One of the most important aspects of MongoDB is that it is a NoSQL database management system, so it works drastically different from 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 to relate the various stored entries to each other. Instead, entries are stored as separate objects, often in JSON format.

NoSQL databases greatly facilitate horizontal scalability, allowing the database to be distributed across multiple servers. Unlike a relational database, server synchronisation latency is not a problem: collections (akin 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 public key from MongoDB repository

First we must check if we already have gnupg and curl installed, if not we must proceed to install them:

sudo apt-get install gnupg curl

Once checked, we will need to import the GPG public key from MongoDB, 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 needed to verify the authorship of 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 the specific repository for MongoDB version 6.0 compatible with Ubuntu 22.04.

The above 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 above command will update the list of available repositories and packages, including the new MongoDB repository we added in the previous step.

4. Install MongoDB via 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 that we have added and offers the latest stable version of MongoDB. The package offered by the Ubuntu repositories might be out of date.

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

A best practice is to disable automatic updates that could lead to compatibility problems. 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 the 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 don't have a Cloud server with Linux operating system you can easily deploy it with SW Hosting.

Your Cloud with Ubuntu 22 from $5.60/month

Get the most out of your project with the fastest disks and most powerful CPUs in the Cloud.

Additionally, if you prefer to avoid having to go through all these steps, you can deploy a server with MongoDB installed by default, making use of our easy-to-deploy OneClickApps.

Your Cloud MongoDB from $5.60/month

Start your project with the best NoSQL database system. Your MongoDB in a fully scalable and customisable Cloud.

i