MongoDB isn’t in Ubuntu’s own repositories (licensing changes removed it years ago), so you install MongoDB Community Edition from MongoDB’s official apt repository. This guide covers installation, the security settings you must change, and basic operations.

Check compatibility first

MongoDB publishes packages for specific Ubuntu LTS releases and CPU features:

  • Each MongoDB major version (7.0, 8.0, …) supports particular Ubuntu LTS releases. Check the MongoDB installation docs for the matching pair before you start; a brand-new Ubuntu release may not be supported for some months.
  • MongoDB 5.0+ requires a CPU with AVX instructions on x86_64. Most modern VPS CPUs expose AVX; verify with grep -o avx /proc/cpuinfo | head -1. No output means the server can’t run current MongoDB.

1. Add the MongoDB repository

The example uses MongoDB 8.0 on Ubuntu 24.04 (“noble”). Adjust the version and codename to a supported combination:

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

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

sudo apt update

2. Install and start

sudo apt install -y mongodb-org
sudo systemctl enable --now mongod
systemctl status mongod --no-pager

The meta-package installs the server (mongod), the shell (mongosh) and the database tools (mongodump, mongorestore, …).

Connect:

mongosh
db.runCommand({ ping: 1 })

3. Enable authentication (essential)

Out of the box, MongoDB accepts connections without a password from anything that can reach it. It binds to 127.0.0.1 by default, which limits the damage — but enable authentication before you do anything else.

Create an administrator while access control is still off:

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

Then edit /etc/mongod.conf:

security:
  authorization: enabled
sudo systemctl restart mongod
mongosh -u admin -p --authenticationDatabase admin

4. Create an application user

Give each app a user scoped to its own database:

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

The app’s connection string:

mongodb://shop_app:[email protected]:27017/shop?authSource=shop

authSource must name the database where the user was created — a frequent cause of “Authentication failed”.

5. Configuration essentials

/etc/mongod.conf (YAML):

storage:
  dbPath: /var/lib/mongodb
systemLog:
  destination: file
  path: /var/log/mongodb/mongod.log
  logAppend: true
net:
  port: 27017
  bindIp: 127.0.0.1
processManagement:
  timeZoneInfo: /usr/share/zoneinfo

On a small VPS shared with other services, cap the WiredTiger cache (by default it takes about half of RAM minus 1 GB):

storage:
  wiredTiger:
    engineConfig:
      cacheSizeGB: 0.5

6. Remote access — carefully

If an app on another server must connect:

  1. Set bindIp: 127.0.0.1,10.0.0.5 (this server’s private or VPN IP) — avoid 0.0.0.0 unless the firewall is airtight.
  2. Allow only the app server: sudo ufw allow from 10.0.0.6 to any port 27017 proto tcp.
  3. Keep authentication enabled, and consider TLS (net.tls) for traffic crossing the public internet.

Thousands of MongoDB databases have been wiped and held for ransom because they listened on a public IP without authentication. Don’t be one of them.

7. Backups

mongodump --uri="mongodb://admin:[email protected]:27017/?authSource=admin" --gzip --archive=/var/backups/mongo-$(date +%F).gz
mongorestore --uri="mongodb://admin:[email protected]:27017/?authSource=admin" --gzip --archive=/var/backups/mongo-2026-11-01.gz

Schedule it with cron and copy the archives off the server. More in MongoDB backup and restore.

8. System tuning

MongoDB’s startup log lists warnings worth acting on:

  • Use XFS for the data directory if you’re creating a dedicated volume; ext4 works but MongoDB recommends XFS with WiredTiger.
  • Raise open-file limits — the packaged systemd unit already sets LimitNOFILE=64000.
  • Transparent Huge Pages — recommendations differ between MongoDB versions (older versions say disable THP; 8.0 recommends enabling it). Follow the docs for your version.

Managing the service

sudo systemctl restart mongod
sudo tail -f /var/log/mongodb/mongod.log
mongosh --eval 'db.serverStatus().connections' -u admin -p --authenticationDatabase admin

Upgrading

MongoDB upgrades one major version at a time (7.0 → 8.0, not 6.0 → 8.0), and you must set featureCompatibilityVersion between steps. Back up before any major upgrade.

Uninstall

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

Deciding between document and relational databases? See MongoDB vs MySQL. For a self-managed MongoDB server, a KVM VPS gives you the full CPU feature set and root access you need.

  • MongoDB
  • Ubuntu
  • Databases