Skip to content

Installing MongoDB on Ubuntu with the official repository

Install MongoDB 7 on Ubuntu from MongoDB Inc.'s apt repository, enable authentication, configure systemd, and prepare the host for production use.

Ubuntu’s universe repository carries an older MongoDB community edition; for production hosts you install from MongoDB Inc.’s own apt repository to get the current major version and steady security patches. This article walks installation of MongoDB 7.0 on Ubuntu 22.04/24.04, the systemd integration, the authentication bootstrap, and the day-zero configuration to harden the host before it accepts a single client connection.

How to verify

Before you install, confirm the OS and that no prior install is present:

lsb_release -a
df -h /var/lib /var/log
dpkg -l | grep -E 'mongo'
ls -la /var/lib/mongodb 2>/dev/null
ss -tlnp | grep 27017

If /var/lib/mongodb exists with data files (WiredTiger.wt, _mdb_catalog.wt), there is an existing dataset — do not install over it.

What’s happening

MongoDB Inc. publishes signed apt repositories for each LTS Ubuntu release. The packages installed are mongodb-org (a meta-package), mongodb-org-server (the mongod daemon), mongodb-org-shell (legacy mongo shell — deprecated in 6+), mongodb-mongosh (the new shell), mongodb-org-mongos (sharded-cluster router), and mongodb-org-tools (mongodump, mongorestore, mongoexport).

The default mongod configuration listens on 127.0.0.1:27017 with no authentication enabled — fine for a fresh install in an isolated environment, but you must enable authentication and bind to a private interface before exposing the port. Data lives in /var/lib/mongodb, configuration in /etc/mongod.conf, logs in /var/log/mongodb/mongod.log, and systemd manages it as mongod.service.

MongoDB 7.0 uses the WiredTiger storage engine exclusively. The default page cache (wiredTigerCacheSizeGB) is (RAM - 1) * 0.5 — for a 16 GB host that is 7.5 GB. Tune up only if MongoDB is the sole workload.

The procedure

  1. Import the MongoDB public key.

    sudo apt install -y curl gnupg
    curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc \
      | sudo gpg --dearmor -o /usr/share/keyrings/mongodb-server-7.0.gpg
  2. Add the apt source for your Ubuntu release.

    echo "deb [arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg] \
      https://repo.mongodb.org/apt/ubuntu $(lsb_release -cs)/mongodb-org/7.0 multiverse" \
      | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
  3. Install and enable.

    sudo apt update
    sudo apt install -y mongodb-org
    sudo systemctl enable --now mongod
    sudo systemctl status mongod --no-pager
  4. Verify it is running and listening.

    ss -tlnp | grep mongod
    mongosh --eval "db.runCommand({connectionStatus: 1})"
  5. Create the admin user. While auth is still off (default), create the initial admin:

    mongosh
    use admin
    db.createUser({
      user: "admin",
      pwd: "strong-password",
      roles: [
        { role: "userAdminAnyDatabase", db: "admin" },
        { role: "clusterAdmin",         db: "admin" },
        { role: "readWriteAnyDatabase", db: "admin" }
      ]
    })
    exit
  6. Enable authentication. Edit /etc/mongod.conf:

    security:
      authorization: enabled
    
    net:
      port: 27017
      bindIp: 127.0.0.1,10.0.0.5    # localhost AND the private-subnet address
    
    storage:
      dbPath: /var/lib/mongodb
      wiredTiger:
        engineConfig:
          cacheSizeGB: 8
    
    systemLog:
      destination: file
      logAppend: true
      path: /var/log/mongodb/mongod.log
      logRotate: reopen
    
    processManagement:
      timeZoneInfo: /usr/share/zoneinfo

    Restart: sudo systemctl restart mongod. After restart, every connection must authenticate.

  7. Reconnect with authentication.

    mongosh "mongodb://admin@127.0.0.1:27017/admin?authSource=admin" -p
  8. Create application users with scoped roles.

    use orders
    db.createUser({
      user: "orderapp",
      pwd: "strong-password",
      roles: [{ role: "readWrite", db: "orders" }]
    })
  9. Open the firewall for the application subnet:

    sudo ufw allow from 10.0.0.0/24 to any port 27017 proto tcp
    sudo ufw status verbose
  10. Enable TLS on the wire. Generate or provision a server certificate, then add to /etc/mongod.conf:

    net:
      tls:
        mode: requireTLS
        certificateKeyFile: /etc/ssl/mongodb/server.pem
        CAFile: /etc/ssl/mongodb/ca.crt

    Connect with mongosh "mongodb://...?tls=true&tlsCAFile=/etc/ssl/mongodb/ca.crt".

Common pitfalls

  • The mongo legacy shell is removed in MongoDB 6+ — use mongosh. Scripts referring to mongo --eval need updating.
  • Enabling authentication after creating data — but before creating the admin user — locks you out. Order matters: create admin first, then enable auth.
  • The bindIp field accepts a comma-separated list with no spaces; bindIp: 127.0.0.1, 10.0.0.5 (with space) silently parses as 127.0.0.1.
  • Default wiredTigerCacheSizeGB is half of RAM minus 1 GB — on a small VM this is too small; on a dedicated large host this leaves RAM unused. Tune explicitly.
  • AppArmor on Ubuntu confines mongod to specific paths; moving the data directory requires apparmor.d updates and a profile reload (sudo systemctl reload apparmor).

In the engagements we run, MongoDB installs are driven from an Ansible role that brings up the server, creates the admin and per-app users, enables TLS, and seeds the backup pipeline before the client subnet is ever opened. We carry that under Managed Operations — and for clients running replica sets, the role extends to bootstrap and quorum verification.