By the end of this guide you will have MongoDB 8.0 running as a service on Ubuntu 24.04, installed from the official MongoDB repository (not the outdated Ubuntu universe package), with authentication enabled and a working mongosh connection. This is written for developers and server administrators who need a production-grade NoSQL database on their VPS or dedicated server.

Prerequisites

  • A server or VM running Ubuntu 24.04 LTS (Noble Numbat), x86_64 or ARM64
  • A non-root user with sudo privileges
  • Internet access so apt can reach repo.mongodb.org
  • At least 2 GB of free disk space
Why not apt install mongodb? Ubuntu's own repositories ship an old community build that lags far behind. Installing from the official MongoDB repository gives you the current stable release (8.0) and the latest security updates, and it is the method MongoDB itself documents.

Step 1 — Install the prerequisites

Update the package index and install the two tools we need: curl to fetch the signing key and gnupg to dearmor it into a keyring file.

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

gnupg converts the ASCII-armored public key into a binary keyring that apt can use to verify MongoDB's packages. Without this step, the repository we add later will fail signature verification.

Step 2 — Import the MongoDB public GPG key

Download MongoDB's official signing key and store it in a dedicated keyring file:

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

-fsSL makes curl fail loudly on errors and follow redirects, so a bad download never silently pipes an error page into gpg. The --dearmor flag converts the key from ASCII text into the binary format apt expects. Placing it under /usr/share/keyrings/ with a descriptive name keeps it isolated from other repositories' keys, following modern Debian best practice (the old apt-key add is deprecated).

Step 3 — Add the official MongoDB repository

Create an apt source entry pointing at the MongoDB 8.0 repository for Ubuntu 24.04 (codename noble):

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

Breaking this down: arch=amd64,arm64 limits the repo to the two supported CPU architectures, signed-by binds this source to the key we just imported (so apt will refuse any package not signed by MongoDB), and noble is Ubuntu 24.04's release codename. Using the wrong codename here is the most common cause of "unable to locate package" errors.

Step 4 — Install MongoDB

Refresh the package index and install the mongodb-org metapackage:

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

This installs four packages: mongodb-org-server (the mongod daemon), mongodb-org-mongos (the sharding router), mongodb-org-shell tools, and mongosh (the modern MongoDB shell). The apt-get update must run after adding the source file, or apt will not see the new packages.

Step 5 — Start MongoDB and enable it at boot

sudo systemctl start mongod
sudo systemctl enable mongod

start launches the daemon immediately; enable registers it so MongoDB restarts automatically after a reboot. On a production server, a database that does not survive a reboot is a 3 AM incident waiting to happen.

Step 6 — Verify the installation

Check the service status first:

sudo systemctl status mongod

You should see active (running). Then open the shell and ping the server:

mongosh --eval 'db.runCommand({ ping: 1 })'

Expected output:

test> db.runCommand({ ping: 1 })
{ ok: 1 }

{ ok: 1 } means the server accepted the connection and responded. Confirm the daemon is listening on the default port too:

ss -tulpn | grep 27017

You should see a line showing mongod listening on 127.0.0.1:27017.

Step 7 — Create an admin user

A fresh install has no authentication — anyone who can reach port 27017 has full access. Fix that before exposing anything. Open the shell:

mongosh

Switch to the admin database and create an admin user. Use passwordPrompt() so your password never appears in shell history:

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

This grants database administration plus read/write across all databases. passwordPrompt() asks for the password interactively — type a strong, random one and store it in your password manager.

Step 8 — Enable authentication in mongod.conf

Edit the main configuration file:

sudo nano /etc/mongod.conf

Find the #security: line and change it to:

security:
  authorization: enabled

YAML indentation matters — authorization must sit exactly two spaces under security:. Now restart the daemon:

sudo systemctl restart mongod

From this point on, every mongosh session requires credentials:

mongosh --authenticationDatabase admin -u siteAdmin -p

Test that an unauthenticated connection is now rejected: mongosh --eval 'db.runCommand({ ping: 1 })' should fail with "command ping requires authentication", and the authenticated command should return { ok: 1 }.

Step 9 — (Optional) Allow remote connections

By default MongoDB binds only to localhost, which is the safe choice. If your app runs on another server, open /etc/mongod.conf and edit the network section:

net:
  port: 27017
  bindIp: 127.0.0.1,10.0.0.5

Replace 10.0.0.5 with your server's private IP — never bind to 0.0.0.0 unless a firewall restricts access to port 27017. After changing the bind address, restart and open the port:

sudo systemctl restart mongod
sudo ufw allow from 10.0.0.0/24 to any port 27017

Adjust the ufw rule to your own subnet. A MongoDB instance with no password and an open port is one of the most ransomware-scanned services on the internet — authentication (Step 8) plus a firewall is mandatory for remote access.

Common issues and fixes

1. E: Unable to locate package mongodb-org

The apt index does not know the repo yet. Cause: you skipped sudo apt-get update after Step 3, or the source file contains the wrong codename (noble is required for Ubuntu 24.04). Fix: verify the file with cat /etc/apt/sources.list.d/mongodb-org-8.0.list, correct it, then sudo apt-get update again.

2. gpg: no valid OpenPGP data found

The key download failed — typically no network, a proxy, or a typo in the URL. Run the curl command alone first and confirm it prints a PGP key block (-----BEGIN PGP PUBLIC KEY BLOCK-----). Only pipe into gpg once the download is clean.

3. mongod fails to start (exit status 48 or 100)

Two usual causes: port 27017 is already in use (exit 48), or the data directory has wrong permissions (exit 100). Check the log for the exact error: sudo tail -n 50 /var/log/mongodb/mongod.log. If a stale process holds the port, sudo fuser -k 27017/tcp frees it. If permissions are wrong, sudo chown -R mongodb:mongodb /var/lib/mongodb fixes ownership.

4. MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017 in mongosh

The daemon is not running. Check sudo systemctl status mongod and start it with sudo systemctl start mongod. If it exits immediately, read the log as in issue 3.

5. Authentication failed after enabling authorization

You most likely created the user after enabling authorization, or typed the wrong password. Temporarily comment out the security: block, restart, connect without auth, create the user correctly in the admin database (Step 7), then re-enable authorization and restart.

Conclusion

You now have a production-ready MongoDB 8.0 installation on Ubuntu 24.04: official packages, automatic boot startup, a real admin account, and authentication enforced. From here you can create application databases and users, set up replica sets, or tune mongod.conf for your workload. If you would rather have a team handle your server setup end to end — database installs, hardening, and backups — XpertBees does exactly that for clients every day.