How to Install MongoDB on Ubuntu 24.04

MongoDB is a leading NoSQL database and popular for its flexibility and scalability. Installing MongoDB on Ubuntu 24.04 serves several purposes, particularly for developers and organizations leveraging modern web applications. MongoDB allows for schema-less data storage which is highly scalable and adaptable to complex data types and structures. 

With the release of Ubuntu 24.04, developers and system administrators are looking for reliable methods to install MongoDB on their systems. 

This tutorial will teach the step-by-step procedure to install MongoDB on Ubuntu 24.04.

How to Install MongoDB on Ubuntu 24.04 

The most recommended way to install MongoDB is through the official MongoDB repository. It ensures you receive the most recent and latest stable version and updates. These steps install the latest 7.0 version of MongoDB.

To install MongoDB on Ubuntu 24.04, follow the below steps:

Step 1: Import the MongoDB Public GPG Key

To ensure the validation of the MongoDB packages, first, import the official GPG key using the “curl” command:

curl -fsSL | sudo gpg --dearmor -o /usr/share/keyrings/mongodb-server-7.0.gpg
Install MongoDB on Ubuntu 24.04

Note: By default, curl is not installed, users can install it via the below command:

sudo apt install curl
Install MongoDB on Ubuntu 24.04 a

Users can verify whether the key is successfully imported or not by going to this directory:

cd /usr/share/keyrings

Step 2: Add the MongoDB Repository

Now, create a list file for MongoDB in your sources list directory with the repository URL:

echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] jammy/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
Install MongoDB on Ubuntu 24.04 b

Step 3: Update Package Index 

Now, users need to update the local package database after adding the MongoDB repository. This refreshes your local package database and prepares your system to install the latest MongoDB version:

sudo apt update
Install MongoDB on Ubuntu 24.04 c

Step 4: Install MongoDB

After updating the package list, install MongoDB through the “mongodb-org” package name. With the repository in place and your package index updated, proceed to install MongoDB:

sudo apt install mongodb-org
Install MongoDB on Ubuntu 24.04 d

Step 5: Check MongoDB

To check the MongoDB on Ubuntu 24.04, use the “version” utility with the “mongod” command as below:

mongod --version mongosh --eval 'db.runCommand({ connectionStatus: 1 })'
Install MongoDB on Ubuntu 24.04 e

Step 6: Start and Enable MongoDB Service

After installation, start the MongoDB service and enable it to start on boot:

sudo systemctl start mongod
sudo systemctl enable mongod
Install MongoDB on Ubuntu 24.04 f

Furthermore, to stop/halt the MongoDB service, use the “systemctl” command: 

sudo systemctl stop mongod

If you want to start it again, type the “systemctl” command with the “restart” utility: 

sudo systemctl restart mongod

Step 7: Verify the Installation

Check that MongoDB is installed correctly and is running, and execute the “status” utility as below:

sudo systemctl status mongod
Install MongoDB on Ubuntu 24.04 g

Note: If users face “mongo” not found or command “mono” from deb mono-runtime errors, start and enable the MongoDB services by following Step 4. Or use the “sudo systemctl daemon-reload” command.

In addition, if there is an error during installation or operation, the error log can be found at “/var/log/mongodb/mongod.log”. This file has information about what went wrong. 

Once everything is set up, begin using MongoDB by entering the “mongosh” into the terminal:

Install MongoDB on Ubuntu 24.04 h

How to Configure/Use MongoDB on Ubuntu 24.04?

To manage the MongoDB service on Ubuntu, use the “sudo systemctl start mongod” command to start the service. Furthermore, utilize the “sudo systemctl status mongod” command to check its status.

Creating a MongoDB database and user is a straightforward process that involves initializing the database environment. After that, accessing the MongoDB shell, and executing the necessary commands to set up the database and user with the desired permissions. 

Step 1: Access MongoDB Shell

Access the MongoDB shell using the command line on your local system or through the MongoDB Atlas interface. This shell is where you will execute commands to manage your database.

Install MongoDB on Ubuntu 24.04 i

Step 2: Create the Database

In MongoDB, a database is not explicitly created; it is created automatically when you store data in a collection within that database. 

show db
Install MongoDB on Ubuntu 24.04 j

To switch/create a database, use the command “use <database_name>”. It is created when you insert data into a collection. Let’s, create a database called “ilf” by running the command:

use ilf
Install MongoDB on Ubuntu 24.04 k

Step 3: Create a User

Now, creating users with appropriate roles and permissions is crucial. For instance, create a new user with a password and assign roles:

  user: "ilfUser",
  pwd: "1212",
  roles: [
    { role: "readWrite", db: "ilf" },
    { role: "read", db: "local" }
Install MongoDB on Ubuntu 24.04 l

Note: Users can replace “ilfUser`, “1212”, “ilf”, and “local” with the desired username, password, and database names.

Step 4: Test the User

After creating the user, users can test the permissions. Ensure that the user can perform the assigned roles without any issues:

Install MongoDB on Ubuntu 24.04 m

For basic database operations, use commands

MongoDB OperationsCommands
To switch to a specific databaseuse <database_name>
To list all databasesshow dbs
To delete the userdb.dropUser method
Create a new collectiondb.createCollection(“collectionName”)
Drop an existing one db.collectionName.drop()
To insert documents for a single documentdb.collectionName.insertOne({key: ‘value’})
Querying documentsdb.collectionName.find({query})
Update documents db.collectionName.updateOne({query}, {update})
For deleting documentsdb.collectionName.deleteOne({query})

How to Uninstall/Remove MongoDB from Ubuntu 24.04

To uninstall MongoDB from Ubuntu 24.04, you can follow several methods. The most straightforward approach is to use the “apt” command with the “purge” option, which removes the packages along with their configuration files. 

Stopping MongoDB Service 

Begin by stopping the MongoDB service with the “systemctl” command:

sudo systemctl stop mongod
Install MongoDB on Ubuntu 24.04 n

Remove MongoDB Packages 

To remove all MongoDB packages execute the “purge” option by mentioning the “mongodb-org” package name:

sudo apt purge mongodb-org
Install MongoDB on Ubuntu 24.04 o

Remove Data Directories and Logs

Afterward, it’s important to remove the remaining data directories and logs with 

sudo rm -r /var/log/mongodbsudo rm -r /var/lib/mongodb
Install MongoDB on Ubuntu 24.04 p

Deleting MongoDB Repository

If you’ve added a MongoDB repository, you should also remove it by deleting the corresponding file in /etc/apt/sources.list.d/.

cd /etc/apt/sources.list.d/
sudo rm mongodb-org-7.0.list
Install MongoDB on Ubuntu 24.04 q

Remove Unused Dependencies 

For a thorough cleanup, check for any remaining MongoDB user accounts remove them, and then run the below command:

sudo apt autoremove
Install MongoDB on Ubuntu 24.04 r


To install MongoDB on Ubuntu 24.04, import the MongoDB public GPG key to ensure the validity of the packages. Then, add the MongoDB repository to your system’s software sources. After that, update the package database and install the MongoDB package through the “apt” package manager. Once installed, start and enable the MongoDB service for running on the system boot. Finally, verify the installation and, if necessary, connect to the MongoDB server to begin using the database.