How To Install MongoDB on Ubuntu 24.04


Many modern web applications are turning to MongoDB, a NoSQL database. It doesn’t use the traditional relational database structure. Instead of tables and rows, MongoDB makes use of JSON-like documents for storing the data. These documents are like data packets with built-in flexibility. Unlike relational databases where you need a predefined structure upfront, MongoDB lets you add data without a set schema.

This tutorial will guide you through installing MongoDB on your Ubuntu 24.04. We’ll also cover how to test it out and manage it as a systemd service. With this, you can ensure that the data is always up.

Table of Contents:

1. How To Install MongoDB on Ubuntu 24.04

To install MongoDB stable and latest version on Ubuntu 24.04 you have to add the official MongoDB repository to your system. For this, add the MongoDB GPG key to the system keys list first. Once the GPG key is added, you can get the latest MongoDB using the default apt package manager.

Let’s check each step in detail.

First, ensure the gnupg and curl are installed. If they are missing, you can get them using this command:

sudo apt-get install gnupg curl

The curl will download the GPG key from the MongoDB repository and GnuPG will make sure to check the authenticity of the key. It will verify the signature and check the file if it is tampered or not.

To import the MongoDB GPG key from the official MongoDB repository, run this command:

curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | \

 sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg \

 --dearmor

Note: This tutorial covers the installation of the latest 7.0 version of MongoDB, if you need to install any older version then modify the above command accordingly.

One thing to note is that the curl command is using the -fsSL option here. If it fails to get the GPG key from the server, it will fail silently, and you will not see any error or any mention inside your Ubuntu GPG key list.

However, if the key is successfully imported, you will see OK on the terminal.

You can also check if the key is successfully imported or not by navigating to this directory:

cd /usr/share/keyrings

We have now added the GPG key, but your Ubuntu system still doesn’t know where to find the MongoDB package that you want to install.

Now run the echo command to create the source list for your MongoDB package in Ubuntu 24.04 by running this command:

echo "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" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list

This command alone will tell the apt where to look for the MongoDB package and install it on your system.

Once you’ve run the command, refresh your server’s software catalog.

sudo apt update

Finally, install MongoDB on Ubuntu 24.04:

sudo apt install -y mongodb-org

To check MongoDB’s successful installation, run the version command:

mongod --version

2. Starting the MongoDB Service

The MongoDB service is by default disabled on Ubuntu 24.04, to start it run this command:

sudo systemctl start mongod

Now run the daemon-reload command. This command tells the system to reload its configuration from the disk. This means systemd will scan for any changes that you have made inside the service configuration files.

sudo systemctl daemon-reload

Now verify the status of MongoDB, if it successfully started:

sudo systemctl status mongod

Press Q to exit the above window.

To be on the safe side, you can enable the MongoDB process for auto start upon system reboot by running this command:

sudo systemctl enable mongod

If you need to stop the MongoDB service at any point, run this command:

sudo systemctl stop mongod

To restart any MongoDB process, run this command:

sudo systemctl restart mongod

Note: If you face any type of error while installing or running MongoDB, check the /var/log/mongodb/mongod.log file for the error details and output.

Finally, start the mongosh session by running this command:

mongosh

3. Uninstall MongoDB from Ubuntu 24.04

To completely uninstall MongoDB from your system, you have to remove all its related configuration files and applications. This section will guide you about the complete removal of MongoDB from Ubuntu 24.04.

First, start by stopping the MongoDB service:

sudo service mongod stop

As we have installed MongoDB using the apt package manager, to uninstall and remove it you can use the apt purge command:

sudo apt purge "mongodb-org*"

The purge command will tell apt to remove all MongoDB-related configuration files and packages.

To remove the MongoDB databases and log file, run these commands:

sudo rm -r /var/log/mongodb

sudo rm -r /var/lib/mongodb

Finally, MongoDB is successfully removed from your Ubuntu 24.04 system.

Conclusion

MongoDB is a modern database system that uses a JSON-like document system for storing data. Unlike the traditional way, it does not use tables and rows for database structure. To install MongoDB on Ubuntu 24.04 you have to add the GPG key to the Ubuntu system. After that, you can install it using the Ubuntu default apt package management tool. Once the installation is done, you have to start the MongoDB service manually and enable it for its proper functioning.

Print Friendly, PDF & Email
Categories