How to Install MongoDB on Ubuntu 24.04

Choose a different version or distribution

Introduction

Before we begin talking about how to install MongoDB on Ubuntu 24.04, let's briefly understand – What is MongoDB?

MongoDB is a popular database system known for its flexibility and scalability. It belongs to the NoSQL category, allowing for dynamic data storage of various types. By using MongoDB, businesses can store and manage vast amounts of data effectively.

Its document-based structure permits easy database updates, making it ideal for evolving projects. With its user-friendly interface and robust features, MongoDB is a top choice for modern data management needs, offering agility and performance for diverse applications.

In this tutorial, you will install MongoDB on Ubuntu 24.04. We will also address a few FAQs on how to install MongoDB on Ubuntu 24.04.

Advantages of MongoDB

  1. Scalability: MongoDB easily scales to manage large volumes of data.
  2. Flexibility: Supports dynamic schemas, enabling easy data modifications.
  3. Performance: Offers high-speed queries and indexing for efficient data retrieval.
  4. Replication & Load Balancing: Ensures data redundancy and distribution across multiple servers.
  5. Agility: Facilitates rapid development and iteration with its document-based structure, making MongoDB an ideal choice for agile and evolving projects.

1. How To Install MongoDB on Ubuntu 24.04

To install the latest MongoDB on Ubuntu 24.04, add the official MongoDB repository. First, add the MongoDB GPG key to your system keys list. Then, use the apt package manager to install MongoDB.

Let's break down the steps. Ensure gnupg and curl are installed with this command:

sudo apt-get install gnupg curl

Curl will download the MongoDB GPG key, and GnuPG will verify its authenticity.

To import the key, 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 MongoDB 7.0 installation. For older versions, adjust the command accordingly.

Note that the curl command uses the -fsSL option, meaning it will fail silently if it cannot retrieve the GPG key from the server, without showing any errors or adding the key to your Ubuntu GPG key list.

If the key is imported successfully, you will see 'OK' on the terminal.

To verify the key import, navigate to this directory:

cd /usr/share/keyrings

We've added the GPG key, but your Ubuntu system still doesn't know where to find the MongoDB package.

To create the source list for the MongoDB package on Ubuntu 24.04, run 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 will direct apt to the MongoDB package location. After running it, refresh your server’s software catalog.

sudo apt update

Finally, install MongoDB on Ubuntu 24.04 with this command:

sudo apt install -y mongodb-org

To verify MongoDB's successful installation, run this command:

mongod --version

2. Starting the MongoDB Service

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

sudo systemctl start mongod

Now run the daemon-reload command to tell the system to reload its configuration from the disk. This prompts systemd to scan for any changes made to the service configuration files.

sudo systemctl daemon-reload

Now check the status of MongoDB to ensure it started successfully:

sudo systemctl status mongod

Press Q to exit the status window.

To ensure MongoDB starts automatically on system reboot, run this command:

sudo systemctl enable mongod

To stop the MongoDB service at any time, run this command:

sudo systemctl stop mongod

To restart a MongoDB process, use this command:

sudo systemctl restart mongod
💡
Note: If you encounter any errors during MongoDB installation or operation, refer to the /var/log/mongodb/mongod.log file for details and troubleshooting steps. 

To start a mongosh session, use this command:

mongosh

3. Uninstall MongoDB from Ubuntu 24.04

To completely remove MongoDB from your system, you need to delete all related configuration files and applications. This section provides steps for completely uninstalling MongoDB from Ubuntu 24.04.

Begin by stopping the MongoDB service:

sudo service mongod stop

Since MongoDB was installed using the apt package manager, you can uninstall and remove it using the apt purge command:

sudo apt purge "mongodb-org*"

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

To delete the MongoDB databases and log file, execute these commands:

sudo rm -r /var/log/mongodb

sudo rm -r /var/lib/mongodb

MongoDB has been successfully removed from your Ubuntu 24.04 system.

FAQs to Install MongoDB on Ubuntu 24.04

What is the difference between MongoDB and a traditional relational database?

The main difference is that MongoDB is a NoSQL database, which means it stores data in flexible, JSON-like documents rather than traditional tables with rows and columns.

What are the system requirements for MongoDB?

MongoDB requires a 64-bit operating system and at least 2 GB of RAM. For production environments, it is recommended to have at least 8 GB of RAM and a modern CPU.

How do I configure MongoDB?

You can configure MongoDB by editing the configuration file located at /etc/mongodb.conf. This file contains settings for the database, such as the port number and authentication settings.

Can I install MongoDB on a remote server?

Yes, you can install MongoDB on a remote server as long as it is running Ubuntu 24.04. The installation process is the same as on a local machine.

How do I check the version of MongoDB installed?

You can check the version of MongoDB installed by running mongod --version command in the terminal.

Can I install multiple versions of MongoDB on the same system?

Yes, you can install multiple versions of MongoDB on the same system by using different package repositories or by manually managing the installation.

How do I uninstall MongoDB from Ubuntu 24.04?

To uninstall MongoDB, you can use the sudo apt remove mongodb or sudo apt purge mongodb command to remove the package and its dependencies.

Conclusion

We hope this tutorial helped you understand how to install MongoDB on Ubuntu 24.04.

If you have any queries, please leave a comment below, and we’ll be happy to respond to them for sure.