+(91)70149-37521Subscribe Now

How to Install MongoDB 8.0 on Ubuntu 24.04

MongoDB stores data as flexible documents and is widely used for web applications, APIs, and event data. This tutorial shows how to install MongoDB 8.0 Community Edition on Ubuntu 24.04 LTS from MongoDB’s official repository, start the service, create an administrator, and enable authentication safely. The original guide used MongoDB 5 and the retired apt-key […]

How to Install MongoDB 5 on Ubuntu 20.04

MongoDB stores data as flexible documents and is widely used for web applications, APIs, and event data. This tutorial shows how to install MongoDB 8.0 Community Edition on Ubuntu 24.04 LTS from MongoDB’s official repository, start the service, create an administrator, and enable authentication safely.

The original guide used MongoDB 5 and the retired apt-key command. The updated steps use a dedicated keyring and the official mongodb-org packages. MongoDB 8.0 supports 64-bit Ubuntu 24.04, 22.04, and 20.04 on the platforms listed in its documentation.

Before You Begin

  • Use a supported 64-bit Ubuntu LTS installation.
  • Log in with a sudo-capable account.
  • Take a backup before upgrading an existing database.
  • Keep MongoDB private; do not expose port 27017 to the whole internet.

Confirm the Ubuntu release and architecture:

cat /etc/os-release
dpkg --print-architecture

Install MongoDB 8.0 on Ubuntu 24.04

1. Install the Required Tools

sudo apt-get update
sudo apt-get install -y gnupg curl

2. Import the MongoDB Signing Key

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

curl -f stops on an HTTP error, while the separate keyring limits which repository can use this key.

3. Add the Official MongoDB Repository

For Ubuntu 24.04, whose codename is Noble, run:

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

For Ubuntu 22.04, replace noble with jammy. Do not guess a codename for another distribution; use the matching official MongoDB page.

4. Install MongoDB Community Edition

sudo apt-get update
sudo apt-get install -y mongodb-org

The similarly named Ubuntu mongodb package is not MongoDB Inc.’s official package and can conflict with mongodb-org.

Start and Verify MongoDB

sudo systemctl enable --now mongod
sudo systemctl status mongod --no-pager
mongod --version
mongosh --eval 'db.runCommand({ ping: 1 })'

A successful ping returns ok: 1. If systemd says that mongod.service is missing after installation, run sudo systemctl daemon-reload and try again.

Create the First MongoDB Administrator

MongoDB binds to localhost by default. Keep it that way while creating the first account. Open the shell:

mongosh

Inside mongosh, switch to the admin database and create a user administrator. passwordPrompt() avoids putting the password in shell history:

use admin
db.createUser({
  user: "siteUserAdmin",
  pwd: passwordPrompt(),
  roles: [
    { role: "userAdminAnyDatabase", db: "admin" },
    { role: "readWriteAnyDatabase", db: "admin" }
  ]
})
exit

In a production environment, consider separate administration and application accounts instead of giving one user broad roles.

Enable MongoDB Authentication

Back up the configuration, then edit it:

sudo cp -a /etc/mongod.conf /etc/mongod.conf.backup
sudo nano /etc/mongod.conf

Add or update this YAML section. Spaces matter; use spaces, not tabs:

security:
  authorization: enabled

Restart MongoDB and confirm that it is healthy:

sudo systemctl restart mongod
sudo systemctl status mongod --no-pager
sudo journalctl -u mongod -n 30 --no-pager

Connect and let the shell prompt for the password:

mongosh --authenticationDatabase admin -u siteUserAdmin -p

Create a Least-Privilege Application User

After authenticating as the administrator, create an account limited to one application database:

use myapp
db.createUser({
  user: "myappUser",
  pwd: passwordPrompt(),
  roles: [ { role: "readWrite", db: "myapp" } ]
})

Test it in a new terminal:

mongosh --authenticationDatabase myapp -u myappUser -p

Allow Remote Connections Safely

The safest default is localhost only. If another server must connect, prefer a private network, VPN, or SSH tunnel. Bind MongoDB to the specific private server address, not 0.0.0.0, and restrict the firewall to the application server’s source IP.

For example, if the database server’s private address is 10.0.0.10, edit /etc/mongod.conf:

net:
  port: 27017
  bindIp: 127.0.0.1,10.0.0.10

Then validate by restarting and checking the log. If UFW is active, allow only the trusted application host, shown here as 10.0.0.20:

sudo systemctl restart mongod
sudo ufw allow from 10.0.0.20 to any port 27017 proto tcp
sudo ufw status

Authentication does not make an internet-wide database port safe. Use TLS for traffic that crosses an untrusted network and follow MongoDB’s production security checklist.

MongoDB Service and Log Commands

sudo systemctl start mongod
sudo systemctl stop mongod
sudo systemctl restart mongod
sudo systemctl status mongod
sudo journalctl -u mongod --since today
sudo tail -f /var/log/mongodb/mongod.log

Production Checklist

  • Enable authorization and use separate least-privilege users.
  • Keep port 27017 on a trusted private network.
  • Set up tested, encrypted backups before storing important data.
  • Monitor disk space, memory, connections, and replication health.
  • Use a replica set for availability; a single server is a single point of failure.
  • Test upgrades in staging and read the version compatibility notes.
  • Keep Ubuntu and MongoDB security updates current.

Frequently Asked Questions

Does MongoDB 8.0 support Ubuntu 24.04?

Yes. MongoDB’s version 8.0 documentation lists 64-bit Ubuntu 24.04 LTS, 22.04 LTS, and 20.04 LTS on supported architectures.

What is the MongoDB service name?

The official package uses mongod. Manage it with systemctl.

Should I open port 27017 publicly?

No. Keep MongoDB on localhost or a trusted private network, restrict the firewall, enable authentication, and use TLS where required.

Conclusion

You have installed MongoDB 8.0 on Ubuntu 24.04 from the official repository, created an administrator, enabled access control, and added a limited application user. Before production use, build a tested backup and recovery plan and avoid exposing the database directly to the internet.

For related server protection, see our Linux hardening checklist and UFW firewall guide.


Reviewed and updated: August 2026. The commands and security flow were checked against MongoDB’s official guides for installing MongoDB 8.0 on Ubuntu, SCRAM authentication, and secure IP binding.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to Our Newsletter

Get free how-to tutorials and over 700+ courses. Seo tips, create a wordpress, or learn a new skill.