How to Install MongoDB on Ubuntu 24.04

MongoDB is an open-source NoSQL database that stores data in flexible, JSON-like documents. It supports dynamic data structures, ad-hoc queries, indexing, and real-time aggregation for efficient data analysis. Unlike traditional relational databases, which use tables and fixed schemas, MongoDB is highly scalable and suitable for handling unstructured or semi-structured data due to its document-based model with dynamic and hierarchical data structures.
This article explains how to install MongoDB on Ubuntu 24.04. You will configure the MongoDB service, secure the database, and create a sample database to perform essential CRUD operations.
Prerequisites
Before you begin, you need to:
- Have access to an Ubuntu 24.04 instance as a non-root sudo user.
Install MongoDB
MongoDB is not available in the default package repositories on Ubuntu 24.04. Follow the steps below to add the MongoDB repository and install MongoDB.
Visit the MongoDB releases page and verify the latest version to install. For example,
MongoDB 8.0
.Import the MongoDB GPG key.
console$ curl -fsSL https://www.mongodb.org/static/pgp/server-8.0.asc | sudo gpg -o /usr/share/keyrings/mongodb-server-8.0.gpg --dearmor
The above command imports the MongoDB version
8.0
GPG key and adds it to your APT keyrings to ensure package authenticity.Add the MongoDB repository to your APT repository sources.
console$ echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-8.0.gpg ] https://repo.mongodb.org/apt/ubuntu noble/mongodb-org/8.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-8.0.list
Update the APT package index to apply the MongoDB repository changes.
console$ sudo apt update
Install MongoDB.
console$ sudo apt install -y mongodb-org
Verify the installed MongoDB version.
console$ mongod --version
Your output should be similar to the one below.
db version v8.0.5 Build Info: { "version": "8.0.5", "gitVersion": "cb9e2e5e552ee39dea1e39d7859336456d0c9820", "openSSLVersion": "OpenSSL 3.0.13 30 Jan 2024", "modules": [], "allocator": "tcmalloc-google", "environment": { "distmod": "ubuntu2404", "distarch": "x86_64", "target_arch": "x86_64" } }
Manage the MongoDB System Service
MongoDB uses the mongod
system service to run the database server and manage its runtime operations. Follow the steps below to enable the MongoDB service to automatically start at boot and manage its status.
Enable MongoDB to start automatically at boot.
console$ sudo systemctl enable mongod
Start the MongoDB service.
console$ sudo systemctl start mongod
View the MongoDB service status to verify that it’s running.
console$ sudo systemctl status mongod
Your output should be similar to the one below.
● mongod.service - MongoDB Database Server Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; preset: enabled) Active: active (running) since Mon 2025-03-03 21:40:54 UTC; 6s ago Docs: https://docs.mongodb.org/manual Main PID: 2568 (mongod) Memory: 86.3M (peak: 86.5M) CPU: 443ms CGroup: /system.slice/mongod.service └─2568 /usr/bin/mongod --config /etc/mongod.conf
Secure MongoDB
MongoDB does not require authentication by default, which is insecure and exposes your data to unauthorized users. Follow the steps below to create an administrative user, and enable authentication to secure the MongoDB database server.
Access the MongoDB shell.
console$ mongosh
Switch to the
admin
database.> use admin
admin
is the default administrative database for user management and system-level operations.Create a new administrative user with full privileges to access any database. Replace
mongodbadmin
with your desired username and enter a strong password when prompted.> db.createUser( { user: "mongodbadmin", pwd: passwordPrompt(), roles: [ { role: "userAdminAnyDatabase", db: "admin" }, { role: "readWriteAnyDatabase", db: "admin" }, { role: "dbAdminAnyDatabase", db: "admin" } ] } )
Your output should be similar to the one below.
Enter password ********{ ok: 1 }
List all MongoDB users and verify that the
mongodbadmin
user is available.> db.getUsers()
Your output should be similar to the one below.
{ users: [ { _id: 'admin.mongodbadmin', userId: UUID('288c02ec-d4a2-4631-b896-6ce6a537529a'), user: 'mongodbadmin', db: 'admin', roles: [ { role: 'dbAdminAnyDatabase', db: 'admin' }, { role: 'userAdminAnyDatabase', db: 'admin' }, { role: 'readWriteAnyDatabase', db: 'admin' } ], mechanisms: [ 'SCRAM-SHA-1', 'SCRAM-SHA-256' ] } ], ok: 1 }
Exit the MongoDB shell.
> exit
Open the MongoDB configuration file to enable authentication.
console$ sudo nano /etc/mongod.conf
Find the
#security:
section, remove the (#
) comment symbol, and change theauthorization
status toenabled
.yamlsecurity: authorization: enabled
Save and close the file.
Restart the MongoDB service to apply the configuration changes.
console$ sudo systemctl restart mongod
Access and Use MongoDB
Accessing MongoDB through its interactive shell allows you to manage databases, users, and data directly. Follow the steps below to log in, create a database, add users, and perform basic CRUD (Create, Read, Update, Delete) operations.
Log in to the MongoDB shell as the
mongodbadmin
admin user you created earlier.console$ mongosh -u mongodbadmin -p --authenticationDatabase admin
Enter the administrator password you set earlier when prompted.
Create a new
vultr_example_db
database.> use vultr_example_db
Create a new
vultruser
user with full privileges to thevultr_example_db
database.> db.createUser( { user: "vultruser", pwd: passwordPrompt(), roles: [ { role: "readWrite", db: "vultr_example_db" } ] } )
Enter a strong password when prompted.
Your output should be similar to the one below.
Enter password ********{ ok: 1 }
Exit the MongoDB shell.
> exit
Log in to the MongoDB shell as
vultruser
and enter the password when prompted.console$ mongosh -u vultruser -p --authenticationDatabase vultr_example_db
Switch to the
vultr_example_db
database.> use vultr_example_db
Insert a new document into the
messages
collection withGreetings from Vultr
message.> db.messages.insertOne({ message: "Greetings from Vultr" })
Your output should be similar to the one below.
{ acknowledged: true, insertedId: ObjectId('67c5d0fe8da0f3d3af51e944') }
View all documents in the
messages
collection and verify that the new document is available.> db.messages.find()
Your output should be similar to the one below.
[ { _id: ObjectId('67c5d0fe8da0f3d3af51e944'), message: 'Greetings from Vultr' } ]
Update the message from
Greetings from Vultr
toGreetings from Vultr Docs
in the existing document.> db.messages.updateOne( { message: "Greetings from Vultr" }, { $set: { message: "Greetings from Vultr Docs" } } )
Your output should be similar to the one below.
{ acknowledged: true, insertedId: null, matchedCount: 1, modifiedCount: 1, upsertedCount: 0 }
View all documents in the
messages
collection to verify the update.> db.messages.find()
Your output should be similar to the one below.
[ { _id: ObjectId('67c5d0fe8da0f3d3af51e944'), message: 'Greetings from Vultr Docs' } ]
Delete the document from the
messages
collection.> db.messages.deleteOne({ message: "Greetings from Vultr Docs" })
Your output should be similar to the one below.
{ acknowledged: true, deletedCount: 1 }
Exit the MongoDB shell.
> exit
Conclusion
You have installed MongoDB on Ubuntu 24.04, secured the database server with authentication, and performed database operations. You created a database, a user with administrative privileges, and performed CRUD operations, including inserting, updating, and deleting documents. For more information and configuration options, visit the MongoDB Documentation.