MongoDB_Logo

How to Install MongoDB on Ubuntu 24.04 (Step-by-Step Guide)

MongoDB is a popular NoSQL database used in modern web and application development. If you’re running Ubuntu 24.04 and want to install MongoDB securely and efficiently, this guide will walk you through each step.

Tested on: Ubuntu 24.04 LTS
🕒 Estimated time: 10–15 minutes
🔒 Difficulty: Beginner to Intermediate


📦 Step 1: Import the MongoDB Public GPG Key

Before installing MongoDB, you’ll need to add its official GPG key to ensure package authenticity.

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

📁 Step 2: Add the MongoDB APT Repository

Create a new APT source file for MongoDB:

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

Note: Ubuntu 24.04 is based on Jammy (22.04) for compatibility, so we use the Jammy repository.


🔄 Step 3: Update Your Package List

Update your system’s package cache to include the new MongoDB repository:

sudo apt update

🚀 Step 4: Install MongoDB

Now install the latest stable release of MongoDB:

sudo apt install -y mongodb-org

This command installs:

  • mongod – the database server
  • mongosh – the MongoDB shell
  • Supporting tools and services

▶️ Step 5: Start and Enable the MongoDB Service

Start MongoDB and enable it to launch on boot:

sudo systemctl start mongod
sudo systemctl enable mongod

Verify the service status:

sudo systemctl status mongod

You should see output indicating that MongoDB is active (running).


🧪 Step 6: Test MongoDB

Connect to MongoDB using the shell:

mongosh

Inside the shell, try running:

db.stats()

If you get a response with database statistics, MongoDB is working!


🔐 (Optional) Step 7: Secure MongoDB

To add basic authentication:

  1. Create an admin user:
use admin
db.createUser({
  user: "admin",
  pwd: "strongpassword",
  roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
})
  1. Edit the MongoDB config file:
sudo nano /etc/mongod.conf

Enable authorization by adding or editing:

security:
  authorization: enabled
  1. Restart MongoDB:
sudo systemctl restart mongod

You’ll now need credentials to access the database.


🧹 Uninstall MongoDB (if needed)

To remove MongoDB:

sudo systemctl stop mongod
sudo apt purge mongodb-org*
sudo rm -r /var/log/mongodb /var/lib/mongodb

✅ Conclusion

You’ve successfully installed MongoDB on Ubuntu 24.04! MongoDB is now running and ready to support your web apps, APIs, analytics, and more.

If you’re hosting MongoDB on a Greenhost VPS, you’ll benefit from fast SSD storage, reliable uptime, and full root access.


🟢 Need Help?

Greenhost.cloud offers managed VPS solutions for MongoDB, Node.js, Docker, and more. If you’d prefer a one-click deployment or automated scaling, contact us today.