How to Install the Latest MongoDB on Linux

How to Install the Latest MongoDB on Linux

If you want MongoDB on Linux and prefer always having the newest stable version, you can set up your system to install updates automatically through MongoDB’s official repository. This guide covers Ubuntu, Debian, Rocky Linux, AlmaLinux, and CentOS Stream. The steps are simple, repeatable, and safe for production or development.

Why use the official MongoDB repository

Most Linux distributions include MongoDB in their default package repositories, but those versions are often outdated. MongoDB maintains its own repo that always carries the latest stable releases. Adding this repo ensures you get security patches and new features without waiting for your distro’s maintainers.

Ubuntu and Debian

1. Import the MongoDB public key

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

2. Add the repository

Replace ubuntu with debian if you are on Debian. Replace jammy with your actual codename.

echo "deb [signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg] \
https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" | \
sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list

To find your codename:

lsb_release -sc

3. Update and install

sudo apt update
sudo apt install -y mongodb-org

MongoDB will now update automatically whenever you run sudo apt upgrade.

4. Start and enable the service

sudo systemctl start mongod
sudo systemctl enable mongod

Check the status:

systemctl status mongod

Rocky Linux, AlmaLinux, and CentOS Stream

1. Create a repo file

sudo tee /etc/yum.repos.d/mongodb-org-7.0.repo <<EOF

[mongodb-org-7.0]

name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/\$releasever/mongodb-org/7.0/x86_64/ gpgcheck=1 enabled=1 gpgkey=https://pgp.mongodb.com/server-7.0.asc EOF

2. Install MongoDB

sudo dnf install -y mongodb-org

3. Start and enable the service

sudo systemctl start mongod
sudo systemctl enable mongod

Verifying your installation

Once launched, verify that MongoDB is responding:

mongosh

If it opens the shell without errors, your setup is complete.

Keeping MongoDB always up to date

Because you added the official repository, updates will appear through your package manager just like any other system update.

On Ubuntu/Debian:

sudo apt upgrade

On Rocky/Alma/CentOS:

sudo dnf upgrade

If a new major version becomes available (for example MongoDB 7 to 8), MongoDB will announce it. Major upgrades usually require a small manual step, but minor updates and patches will install normally.

Leave a Reply

Your email address will not be published. Required fields are marked *