22.12.2023

How to Install MongoDB on Ubuntu?

Overview

Installation of MongoDB involves several steps, such as choosing whether to use community or commercial versions, downloading package with wrote repository and sign before on Ubuntu, setting up replica sets (if required), and verifying the installation with a connection test. In that instruction we will consider installation for Ubuntu.

Warning! Commonly in repositories actively distribute package with label mongodb, which don't maintained by official developer! Instead of that we will add official repository and download mongodb-org. If you already have installed mongodb unofficial package, than you have to purge them from the system by entering command:

apt purge mongodb -y

After that command package with dependencies will be deleted!

Installation

First of all we need to make sure that we use latest version of package in the system:

apt update && apt upgrade -y

Screenshot №1 — Update package

Than, let's talk about how we will securely download package. For that Linux use official repository and open realisation of pgp sign! If you don't have that package, than install it:

apt install gnupg curl

Check version of using OS:

lsb_release -a

Screenshot №2 — Version of OS

Download public key which allowed us to confirm authenticity and integrity of transfer data from insecure network:

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

Screenshot №3 — Curl download

That command prefer to use from user with administrative privileges or use command sudo. In our case we use root, therefore command without sudo. In the previous action we downloaded pgp-key and add them into the trusted sign. Then we need to create list in the repository:

touch /etc/apt/sources.list.d/mongodb-org-7.0.list

Add repository with sign in the file like in the command below:

nano /etc/apt/sources.list.d/mongodbb.list

Insert in that file row below:

deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse

Screenshot №4 — Add repository

We indicate architecture, sign and URL address therefore we need to update indexes:

apt update

Next we need to install package from repository, by using apt manager, for download the latest version:

apt install mongodb-org -y

Screenshot №5 — APT install

If you want to download dedicated version of MongoDB, than you can type:

apt install mongodb-org=6.0.12

That command line help to download dedicated version but don't save from unexpected update, that can be useful in the situations when new version unsuitable, than we need to setup status of packet:

echo "mongodb-org hold" | sudo dpkg --set-selections

Option --set-selections help to indicate status of package, that can be install, deinstall or hold. In the official package there is configuration file that we can modify:

nano /etc/mongod.conf

Now we need to start daemon in the initialisation system we will use command:

systemctl start mongod

Then check the loading of daemon typing command before:

sudo systemctl status mongod

Screenshot №6 — Status of the daemon

Alright! Now we need to start MongoSH, that can help to manage DB server:

mongosh

Screenshot №7 — MongoSH

 

All process involves several crucial steps to ensure a secure and reliable setup. The decision between community and commercial versions, proper repository selection, and attention to package authenticity are fundamental considerations. Notably, caution is advised against using unofficial MongoDB packages distributed in certain repositories, as these may lack official maintenance and support.