Skip to content
DevOps devops databases 5 min read

Installing MongoDB on Ubuntu

MongoDB is a NoSQL document database (a database that stores data as flexible JSON-like documents instead of rows in tables). It is a popular choice for apps that need a schema that can change easily, like content systems, catalogs, and real-time analytics. Installing it on Ubuntu takes a few extra steps because MongoDB is not shipped in Ubuntu’s default package repositories. In this guide you will add MongoDB’s own official software repository, install the database, start it as a service, and connect to it with the modern shell, mongosh.

Why the official MongoDB repo is needed

A package repository (often just called a “repo”) is an online catalog of software that apt, Ubuntu’s package manager, can download and install from. Ubuntu ships with a default set of repos, but MongoDB Inc. does not publish the current MongoDB Community Edition into them.

Ubuntu’s universe repo sometimes carries a package called mongodb, but it is old, unmaintained, and not the same as the official build. Always use MongoDB’s own repo so you get current, security-patched releases (MongoDB 8.0 is the current stable line in 2026) and mongosh, the modern shell.

SourcePackage nameVersionWhen to use
Ubuntu universemongodbOld / unmaintainedNever for real workloads
MongoDB official repomongodb-orgCurrent (8.0)Always

Gotcha: The package you install is mongodb-org (a meta-package that pulls in the server, shell, and tools). Do not install the mongodb package from Ubuntu’s repo at the same time — the two conflict.

Step 1: Install prerequisites

You need a couple of helper tools to fetch and verify the repo’s signing key. A signing key is a cryptographic file (GPG, GNU Privacy Guard) that proves the packages really came from MongoDB and were not tampered with.

sudo apt update
sudo apt install -y gnupg curl

Step 2: Add the MongoDB signing key

Download MongoDB’s GPG key and store it in /usr/share/keyrings, the standard location Ubuntu uses for trusted repo keys.

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

This command downloads the key (curl), then converts it into the binary format apt expects (gpg --dearmor) and saves it. There is no visible output when it succeeds.

Step 3: Add the MongoDB repo

Now tell apt where to find MongoDB packages. The exact line depends on your Ubuntu version. Check yours first.

lsb_release -a

Output:

Distributor ID: Ubuntu
Description:    Ubuntu 24.04.2 LTS
Release:        24.04
Codename:       noble

The Codename is what matters. For Ubuntu 24.04 it is noble; for Ubuntu 22.04 it is jammy. Use the matching line below.

For Ubuntu 24.04 (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

For Ubuntu 22.04 (jammy), replace noble with jammy:

echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-8.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/8.0 multiverse" | \
  sudo tee /etc/apt/sources.list.d/mongodb-org-8.0.list

The signed-by= part links this repo to the key you added in Step 2, so apt only trusts packages signed by it.

Step 4: Install MongoDB

Refresh the package list so apt sees the new repo, then install.

sudo apt update
sudo apt install -y mongodb-org

Output:

The following NEW packages will be installed:
  mongodb-org mongodb-org-database mongodb-org-server
  mongodb-mongosh mongodb-org-tools ...
Setting up mongodb-org-server (8.0.x) ...
Setting up mongodb-mongosh (2.x.x) ...

This installs five things: the database itself, the mongod server daemon, the mongosh shell, the command-line tools (backup/restore), and the meta-package that ties them together.

Step 5: Start and enable mongod

mongod is the MongoDB server process (the daemon, a background program that runs all the time). Ubuntu uses systemd to manage services, so use systemctl.

sudo systemctl start mongod
sudo systemctl enable mongod

start runs it now; enable makes it start automatically on every reboot. Check that it is running.

sudo systemctl status mongod

Output:

● mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; enabled)
     Active: active (running) since Mon 2026-06-15 10:21:04 UTC
   Main PID: 1843 (mongod)

active (running) and enabled mean it is up and will survive reboots. If it fails, check the log at /var/log/mongodb/mongod.log.

Step 6: Connect with mongosh and verify

mongosh is the official MongoDB Shell — an interactive prompt where you run database commands in JavaScript syntax. Connect to the local server.

mongosh

Output:

Current Mongosh Log ID: 66...
Connecting to:          mongodb://127.0.0.1:27017/...
Using MongoDB:          8.0.x
Using Mongosh:          2.x.x

test>

You are now at the test> prompt, connected to the default local database. Confirm the version directly.

test> db.version()
'8.0.x'

Type exit to leave the shell. A working db.version() proves the server is installed, running, and reachable.

Default configuration and security

MongoDB’s config file lives at /etc/mongod.conf. By default it binds only to 127.0.0.1 (localhost), so the database is reachable only from the same machine.

net:
  port: 27017
  bindIp: 127.0.0.1

Security warning: A fresh MongoDB install has no authentication — anyone who can reach port 27017 has full access. Never change bindIp to 0.0.0.0 (all network interfaces) without first enabling authentication and a firewall (ufw). Set up users before exposing it.

Best Practices

  • Always install mongodb-org from the official repo, never the outdated mongodb package from Ubuntu universe.
  • Run sudo systemctl enable mongod so the database restarts after a reboot or power loss.
  • Keep bindIp at 127.0.0.1 until you have configured authentication and a firewall rule with ufw allow from <trusted-ip>.
  • Pin a major version line (here 8.0) in your repo file so you control when you move to the next major release.
  • Watch /var/log/mongodb/mongod.log when troubleshooting startup or connection problems.
  • Set up a backup routine early so you are not caught out by data loss.
Last updated June 15, 2026
Was this helpful?