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.
cta:cloud_so_ubuntu_22
If you prefer a server with MongoDB already pre-installed, you can make use of our easy-to-deploy OneClickApps.
cta:cloud_app_mongodb
We will review some of the concepts that define this database management system.
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.
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.
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.
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.
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.
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.
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.
cta:cloud_so_ubuntu_22
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.
cta:cloud_app_mongodb