Install MongoDB on Ubuntu: 5 Easy Steps

Photo of Jim Kutz
Jim Kutz
December 5, 2025

Summarize this article with:

✨ AI Generated Summary

You're likely here because MongoDB's flexible document model has become the backbone of your applications, yet getting it running cleanly on Ubuntu keeps hitting snags. Outdated package lists, missing GPG keys, and cryptic configuration defaults turn what should be a straightforward install into a troubleshooting session.

This guide walks you through a five-step process: adding the official repository, installing the packages, starting the mongod service, confirming everything works, and applying basic security settings. 

TL;DR: MongoDB Installation at a Glance

  • Add MongoDB’s official APT repository using the correct GPG key for your version (7.0 or 8.0).
  • Install mongodb-org to get the server, shell, and admin tools in one step.
  • Start the mongod service and enable it at boot so the database survives restarts.
  • Verify the instance with mongosh, a simple ping command, and log checks.
  • Enable authorization, create an admin user, and restart—this closes the default open-access configuration.

What Do You Need Before Installing MongoDB on Ubuntu?

Before starting the installation, confirm your server meets these prerequisites. Missing any of these requirements leads to GPG errors or missing-package issues during installation:

  • Ubuntu LTS 20.04, 22.04, or 24.04: The database publishes current packages only for supported long-term releases
  • Sudo privileges: You need root-level rights to add repositories, install packages, and manage systemctl services
  • At least 2 GB of RAM for production: Smaller dev environments will run, but heavy queries and index builds exhaust memory quickly
  • gnupg and curl installed: These utilities fetch and verify the MongoDB GPG key that proves package authenticity
  • Reliable outbound HTTPS access to repo.mongodb.org: Ensure firewalls allow apt to reach port 443, or configure an internal mirror for private networks

Step 1: How to Add the MongoDB Repository

Adding the official repository lets you pull the latest, signed packages through APT instead of relying on Ubuntu's older defaults. You'll first import a public GPG key, then create a version-specific repo file, and finally refresh your package index so APT can see the new source.

1. Import the MongoDB Public GPG Key

The key validates every package you download. Without it, APT refuses to install the software:

# MongoDB 8.0
[curl -fsSL](https://airbyte.com/data-engineering-resources/api-to-database) https://www.mongodb.org/static/pgp/server-8.0.asc \
 | sudo gpg -o /usr/share/keyrings/mongodb-server-8.0.gpg --dearmor
# MongoDB 7.0 (use if you plan to stay on the 7.x branch)
curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc \
 | sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg --dearmor

The --dearmor flag converts the ASCII key into the binary format APT expects. Storing it under /usr/share/keyrings/ isolates it from other system keys.

2. Add the Official MongoDB APT Source

Next, point APT to the correct repository for your Ubuntu release. Replace the codename and key file to match your setup:

# Ubuntu 24.04 (noble) with MongoDB 8.0
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
# Ubuntu 22.04 (jammy) with MongoDB 7.0
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" \
 | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
# Ubuntu 20.04 (focal) with MongoDB 7.0
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/7.0 multiverse" \
 | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list

The line format breaks down as: repository type (deb), supported architectures, path to the key that signs packages, repository URL, Ubuntu codename, version folder, and the multiverse component.

3. Update Package Lists

Finish by reloading APT's cache:

sudo apt update

If you see "NO_PUBKEY" errors, the key path in your repo file likely doesn't match the one you imported. Fix the path or re-import the key, then rerun apt update until no signature warnings appear.

Step 2: How to Install MongoDB on Ubuntu

With the repository configured, you can now grab the actual binaries. 

1. Install the Server and Tooling

Run a single apt command to grab the meta-package, which bundles the daemon (mongod), the modern shell (mongosh), and the administrative utilities:

sudo apt install -y mongodb-org

Wait for the command to finish. If it returns to the prompt without errors, the installation succeeded.

2. Confirm What Was Installed

First, check the version string:

mongod --version

A valid output lists the exact build of Community Edition (for example, "db version v7.0.2"), proving the binaries are executable.

Next, list the installed packages to verify every component is present:

dpkg -l | grep mongo

You should spot entries for mongodb-org-server, mongodb-org-shell, and mongodb-org-tools. With those files in place, you're ready to start the service.

Step 3: How to Start and Enable the MongoDB Service

You've installed the packages, now it's time to bring the database online so it's always available when you need it.

1. Start the Service

To launch the database, run:

sudo systemctl start mongod

This command asks systemd to start the mongod daemon, which begins listening on port 27017 for incoming connections. It only affects the current session, so if you reboot right now the database would stay offline.

2. Enable at Boot

Keep it running after every reboot with:

sudo systemctl enable mongod

enable creates a symbolic link inside systemd's boot targets, guaranteeing that Ubuntu starts the database automatically. Skipping this step is a common oversight that leaves production applications without a datastore after a patch cycle.

3. Check Status

Confirm everything worked:

sudo systemctl status mongod

A healthy service shows active (running) and enabled:

● mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; enabled)
     Active: active (running) since …

If the status is anything else, check /var/log/mongodb/mongod.log for binding or permission errors before moving on to verification steps.

Step 4: How to Verify That MongoDB Is Running Correctly

Once packages are installed and the service is running, confirm the server accepts connections and writes logs properly. Connect via shell, run a test query, and check logs if issues arise.

1. Connect to the Shell

Launch the modern client (mongosh replaced the deprecated mongo binary):

mongosh

You should see:

Current Mongosh Log ID: 65d9…
Connecting to:          mongodb://127.0.0.1:27017

A prompt ending in > confirms the mongod daemon is running and you are connected, but not necessarily that it is listening on port 27017. Connection refused errors indicate the service isn't running or is bound to a different interface.

2. Run a Test Command

Inside the shell, ping the database:

db.runCommand({ ping: 1 })

A healthy instance returns { ok: 1 }, proving the database executes commands and returns results.

3. Check Logs if Something Fails

When the shell won't connect, inspect the primary log:

sudo tail -n 40 /var/log/mongodb/mongod.log

Look for network error while attempting to run command, address already in use, or permission denied. The log identifies startup failures faster than repeated restarts, helping you resolve binding issues or data-directory permissions.

Step 5: How to Configure MongoDB for Basic Security

A fresh installation allows anyone who reaches port 27017 to read and modify data. You'll fix this security gap with three steps: enable authorization, create an admin user, and restart the service.

1. Enable Authorization

Open the server's main config file:

sudo nano /etc/mongod.conf

Uncomment or add the security block so the daemon enforces credentials on every request:

security:  
  authorization: enabled

This setting changes the database from wide-open access to credentials-required, and closes the default exposure.

Save the file and exit the editor.

2. Create an Admin User

Before restarting, connect without auth one last time and add your root-level account:

mongosh
use admin
db.createUser({
  user: "siteRoot",
  pwd: "changeMeNow!",
  roles: [ { role: "root", db: "admin" } ]
})
quit()

Creating administrative users in the admin database gives you full role-based control for managing MongoDB. Still, application and replication users should be created in their respective databases with limited roles.

3. Restart the Service

Reload the database so it reads the new settings:

sudo systemctl restart mongod

Now an unauthenticated mongosh attempt gets rejected, while a credentialed session succeeds:

mongosh -u siteRoot -p --authenticationDatabase admin

If that connects cleanly, authorization is active. 

What Common MongoDB Installation Issues Should You Watch For?

The database can stumble on predictable pitfalls during installation. Most problems happen while adding the repository, importing the GPG key, or starting the mongod service.

Missing GPG Key Errors

Missing GPG Key errors occur when Apt complains about a missing key with NO_PUBKEY. The key import failed, so re-run this command:

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

Confirm the repository line in /etc/apt/sources.list.d/ points to https://repo.mongodb.org/apt/ubuntu. Run sudo apt update again.

Service Start Failures

Service start failures happen when sudo systemctl start mongod returns "Failed to start mongod.service." Check the logs first:

sudo journalctl -u mongod --no-pager | tail

Port 27017 might already be taken. Edit /etc/mongod.conf to bind the database to a free port, restart the service, and verify with sudo systemctl status mongod.

Permission Issues

Permission Issues cause the daemon to die with "Permission denied" because the data directory isn't owned by the mongodb user. Reset ownership:

sudo chown -R mongodb:mongodb /var/lib/mongodb
sudo systemctl restart mongod

How Can You Sync MongoDB Data Into Other Tools Automatically?

Your database holds valuable data, but it's isolated from your analytics dashboards, data warehouses, and business intelligence tools. Manual exports and custom scripts break constantly, leaving your team scrambling to keep reports updated.

Airbyte's connector handles this automatically, including change data capture (CDC) for real-time synchronization. Whether you need data in Snowflake, BigQuery, or any of 600+ other destinations, Airbyte generates open-standard code that won't lock you into proprietary formats.

The connector works with your existing security setup and scales with your data volume using predictable, capacity-based pricing. Try Airbyte.

Frequently Asked Questions

Why do I get NO_PUBKEY errors when running apt update?

The GPG key path in your repo file usually doesn’t match the file you imported. Re-import the correct key into /usr/share/keyrings/ and make sure your .list file references the same key. Run sudo apt update again until all signature warnings disappear.

MongoDB installed, but systemctl start mongod fails. What should I check first?

Always check the logs before retrying. Use sudo journalctl -u mongod --no-pager | tail. Most failures come from a locked port, a corrupted data directory, or permission issues on /var/lib/mongodb.

Why can I connect with mongosh before enabling auth, but not after?

Once you turn on authorization, you must authenticate every session. Use:
mongosh -u <user> -p --authenticationDatabase admin
If that works, your security settings are correct.

Can MongoDB run without opening port 27017 publicly?

Yes. For production, keep MongoDB bound to 127.0.0.1 or a private interface and restrict access through your application layer. Use SSH tunnels, VPC rules, or internal networking instead of exposing the database directly.

Limitless data movement with free Alpha and Beta connectors
Introducing: our Free Connector Program
The data movement infrastructure for the modern data teams.
Try a 30-day free trial
Photo of Jim Kutz