In this tutorial, we will explore How to Install MongoDB Community Edition 5/6 on the Ubuntu operating system. MongoDB is a popular, document-oriented, and schema-less database management system widely used in modern web and mobile applications.
MongoDB brings significant improvements and new features, including data-at-rest encryption, easier version management, and much more. By following this guide, you will be able to install MongoDB on Ubuntu and utilize it for your applications.
Let’s proceed with the step-by-step instructions below to install MongoDB 5/6 on Ubuntu.
Applies to
- MongoDB 5
- Ubuntu 20.04 LTS
- Ubuntu 18.04 LTS
- MongoDB 6
- Ubuntu 22.04 LTS
- Ubuntu 20.04 LTS
- Ubuntu 18.04 LTS
Prerequisites
- Operating system: An Ubuntu system.
- User privileges: root or non-root user with sudo privileges.
Step 1 – Prepare
Before starting the installation, ensure that your system is up to date. Use the following command to update it:
sudo apt update && sudo apt upgrade -y
Next, let’s install the required dependencies for MongoDB by using the following command:
sudo apt install wget curl gnupg2 software-properties-common apt-transport-https ca-certificates lsb-release -y
Once you have completed the previous steps, you can proceed to the next step.
Step 2 – Install MongoDB Community Edition on Ubuntu
In this guide, we will install MongoDB Community on your Ubuntu system, these instructions will use the official mongodb-org package, which is maintained and supported by MongoDB Inc. The official mongodb-org package always contains the latest version of MongoDB, and is available from its own dedicated repo.
The mongodb package provided by Ubuntu is NOT maintained by MongoDB Inc. and conflicts with the official mongodb-org package. If you have already installed the mongodb package on your Ubuntu system, you must first uninstall the mongodb package before proceeding with these instructions.
Note: Throughout this tutorial, I will be using Ubuntu 20.04 LTS as the operating system and MongoDB 6 as the version of MongoDB.
Import the public key used by the package management system
In this step, we will import the public key used by the package management system to authenticate the origin of MongoDB software packages.
The package management system uses public keys to verify the integrity and authenticity of software packages before accepting and installing them. Importing the public key ensures that the MongoDB software packages are installed from a trusted source.
To import the MongoDB public GPG Key, use the following command:
For MongoDB 5
curl -fsSL https://pgp.mongodb.com/server-5.0.asc | sudo gpg -o /usr/share/keyrings/mongodb-server-5.0.gpg
--
dearmor
For MongoDB 6
curl -fsSL https://pgp.mongodb.com/server-6.0.asc | sudo gpg -o /usr/share/keyrings/mongodb-server-6.0.gpg
--
dearmor
Once you have completed the previous steps, you can proceed to the next step.
Configure MongoDB Repo
Next, select your version of Ubuntu and create the list file /etc/apt/sources.list.d/mongodb-org-x.0.list for your version of Ubuntu. ( In this example, we are using Ubuntu 20.04 LTS)
For MongoDB 5
Ubuntu 20.04 LTS
echo
"
deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-5.0.gpg ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0 multiverse"
| sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.listUbuntu 18.04 LTS
echo
"
deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-5.0.gpg ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/5.0 multiverse"
| sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list
For MongoDB 6
Ubuntu 22.04 LTS
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.listUbuntu 20.04 LTS
echo
"
deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-6.0.gpg ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/6.0 multiverse"
| sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.listUbuntu 18.04 LTS
echo
"
deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-6.0.gpg ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/6.0 multiverse"
| sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list
And we need to update the system to ensure that the system is updated with the newly added repository by running the command below:
sudo apt update
Install MongoDB Community Edition
At this point, you can install either the latest stable version of MongoDB. To install the latest stable version, execute the following command:
sudo apt-get install mongodb-org -y
Optional. After the installation, if you run the command apt update, it will upgrade packages to newer versions. To prevent unintended upgrades, you can pin the packages to the currently installed version using the following command:
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-org-shell hold"
| sudo dpkg--
set-selections
echo"
mongodb-org-mongos hold"
| sudo dpkg--
set-selections
echo"
mongodb-org-tools hold"
| sudo dpkg--
set-selections
To check the installed version of MongoDB, you can execute the following command:
mongod
--
version
Next, to ensure that MongoDB starts automatically upon system reboot or restart, you can use the following commands to start the MongoDB service and enable its automatic startup:
sudo systemctl start mongod
sudo systemctl enable mongod
Finally, ensure that the MongoDB service is running on your system by executing the following command:
sudo systemctl status mongod
That’s it! You now know how to install MongoDB on your Ubuntu system. If you have any questions or suggestions, please feel free to leave a comment below.
Thank you for reading !!!