How to Install MongoDB on Ubuntu 24.04 and 22.04

Install MongoDB on Ubuntu 24.04 and 22.04 with the official APT repo. Covers setup, auth, updates, firewall guidance, and removal.

Last updatedAuthorJoshua JamesRead time8 minGuide typeUbuntu

MongoDB fits Ubuntu applications that need a document database with flexible JSON-style records, the mongod service, and the mongosh administration shell on one managed update path. To install MongoDB on Ubuntu with the official Community Edition packages, use MongoDB’s own APT repository instead of any legacy distro package names that may appear on upgraded systems.

MongoDB 8.3 is the latest stable minor release, and the upstream Ubuntu repository currently publishes Community Server packages for Ubuntu 24.04 and 22.04 on amd64 and arm64. MongoDB 8.0 remains available on the same two releases, 7.0 is limited to Ubuntu 22.04, and Ubuntu 26.04 still lacks native upstream repository metadata.

Install MongoDB on Ubuntu

MongoDB’s own repository is the maintained path here. MongoDB’s official Ubuntu installation documentation covers the Community Edition package workflow, while older Ubuntu mongodb package names are not maintained by MongoDB Inc. and can conflict with the upstream packages when they exist on a migrated system.

Compare MongoDB Community Server Branches on Ubuntu

The current branch picture is easier to follow when you separate MongoDB’s release naming from Ubuntu’s LTS matrix.

MongoDB BranchMongoDB StatusUbuntu SupportBest Fit
MongoDB 8.3Latest stable minor releaseUbuntu 24.04 and 22.04New deployments that want the latest upstream Community Server
MongoDB 8.0Current major releaseUbuntu 24.04 and 22.04Stacks pinned to the 8.0 release line
MongoDB 7.0Previous stable releaseUbuntu 22.04 onlyOlder deployments that are not ready to move off 7.0 yet

MongoDB still publishes packages for Ubuntu 20.04 in some branches, but Ubuntu 20.04 has left standard Ubuntu support. This repository workflow stays with Ubuntu 24.04 and 22.04.

Ubuntu 26.04 is not ready for this repository method yet. MongoDB’s native resolute repository paths for 8.3, 8.2, 8.0, and 7.0 currently return 404, so do not point Ubuntu 26.04 at the older noble or jammy suites.

Prepare Ubuntu for the MongoDB Repository

Update Ubuntu Before MongoDB Installation

Refresh package metadata before adding the MongoDB source. If APT reports pending upgrades, run sudo apt upgrade separately and review the package list before you continue.

sudo apt update

These commands use sudo for tasks that need root privileges. If your account does not have sudo access yet, run the commands as root or add a new user to sudoers on Ubuntu first.

Install Required Packages for the MongoDB Repository on Ubuntu

Install the packages that fetch and verify the repository metadata. Desktop systems often already have some of them, but cloud and minimal Ubuntu images usually do not. The flags in the curl command keep the download quiet and fail fast, and the curl command in Linux guide has broader examples if you want to go deeper.

sudo apt install -y ca-certificates curl gpg

Check MongoDB CPU and Architecture Support on Ubuntu

MongoDB 5.0 and newer require AVX on amd64 systems, and MongoDB’s production notes list ARMv8.2-A or newer for arm64. Check the local architecture before you install the server package, especially on older hardware or virtual machines where the hypervisor may hide CPU features.

ARCH=$(dpkg --print-architecture)
printf 'Architecture: %s\n' "$ARCH"

if [ "$ARCH" = "amd64" ]; then
  grep -qw avx /proc/cpuinfo && echo "AVX supported" || echo "AVX missing"
fi

A supported amd64 system returns:

Architecture: amd64
AVX supported

If the check returns AVX missing, stop before installing mongodb-org. The package can install successfully and still fail at runtime with Illegal instruction when the CPU or virtual CPU does not expose the required instruction set.

Import the MongoDB GPG Key on Ubuntu

MongoDB 8.3 currently uses MongoDB’s 8.0 release signing key for the Ubuntu repository. Import that key into a dedicated APT keyring before you add the source.

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

The gpg --dearmor --yes step converts the ASCII-armored key into APT’s binary format and lets the command overwrite the keyring cleanly if you rerun it later.

Add the MongoDB 8.3 Repository on Ubuntu

Create a DEB822 repository file with Ubuntu’s detected codename and architecture. The guard writes the source only for the Ubuntu releases and architectures that MongoDB currently publishes for this branch.

. /etc/os-release
ARCH=$(dpkg --print-architecture)

case "$VERSION_CODENAME:$ARCH" in
  noble:amd64|noble:arm64|jammy:amd64|jammy:arm64)
    printf '%s\n' \
      'Types: deb' \
      'URIs: https://repo.mongodb.org/apt/ubuntu' \
      "Suites: $VERSION_CODENAME/mongodb-org/8.3" \
      'Components: multiverse' \
      "Architectures: $ARCH" \
      'Signed-By: /usr/share/keyrings/mongodb-server-8.0.gpg' \
    | sudo tee /etc/apt/sources.list.d/mongodb-org-8.3.sources > /dev/null
    ;;
  *)
    printf 'MongoDB 8.3 packages are not published for Ubuntu %s (%s) on %s.\n' "$VERSION_ID" "$VERSION_CODENAME" "$ARCH"
    ;;
esac

The VERSION_CODENAME variable resolves to noble on Ubuntu 24.04 and jammy on Ubuntu 22.04. The dpkg --print-architecture part keeps the source aligned with your system architecture, and tee writes the file with root privileges because a plain > redirection would still run in your current shell. If the command prints the unsupported-release message, stop there and wait for MongoDB to publish packages for that Ubuntu release.

cat /etc/apt/sources.list.d/mongodb-org-8.3.sources
Types: deb
URIs: https://repo.mongodb.org/apt/ubuntu
Suites: noble/mongodb-org/8.3
Components: multiverse
Architectures: amd64
Signed-By: /usr/share/keyrings/mongodb-server-8.0.gpg

On Ubuntu 22.04, the Suites: line uses jammy/mongodb-org/8.3 instead of noble/mongodb-org/8.3. On arm64, the Architectures: line uses arm64.

Refresh APT and Verify the MongoDB Package Source on Ubuntu

Refresh APT first, then confirm that mongodb-org is coming from MongoDB’s repository before you install anything.

sudo apt update

Relevant output includes:

Get:5 https://repo.mongodb.org/apt/ubuntu noble/mongodb-org/8.3 InRelease [3,005 B]
Get:6 https://repo.mongodb.org/apt/ubuntu noble/mongodb-org/8.3/multiverse amd64 Packages [6,762 B]
apt-cache policy mongodb-org
mongodb-org:
  Installed: (none)
  Candidate: 8.3.1
  Version table:
     8.3.1 500
        500 https://repo.mongodb.org/apt/ubuntu noble/mongodb-org/8.3/multiverse amd64 Packages
     8.3.0 500
        500 https://repo.mongodb.org/apt/ubuntu noble/mongodb-org/8.3/multiverse amd64 Packages

On Ubuntu 22.04, the candidate version stays on the same 8.3.x track but the source path changes to jammy/mongodb-org/8.3.

Install MongoDB Community Edition on Ubuntu

Install the MongoDB metapackage once APT sees the right candidate. This pulls in the server, mongosh, and the supported database tools together.

sudo apt install -y mongodb-org

Start and Enable MongoDB on Ubuntu

Reload systemd once after installation, then enable MongoDB to start now and on future boots. The reload is harmless when it is not needed, and it avoids the occasional Unit mongod.service not found problem right after package installation.

sudo systemctl daemon-reload
sudo systemctl enable --now mongod

Verify MongoDB Package Versions on Ubuntu

Check the installed server, shell, and database tools packages first. This package-level check confirms that APT installed the expected MongoDB 8.3 packages before you test the running service.

for pkg in mongodb-org-server mongodb-mongosh mongodb-database-tools; do
  dpkg-query -W -f='${binary:Package} ${Version}\n' "$pkg"
done

Relevant output includes:

mongodb-org-server 8.3.1
mongodb-mongosh 2.8.3
mongodb-database-tools 100.17.0
mongod --version
mongosh --version

On a system that passes the CPU requirement check, the first server line should report db version v8.3.x, and the shell version should match the installed mongodb-mongosh package.

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

This confirms that the local shell can reach the running server instead of only proving that the binary exists. A working local server returns { ok: 1 }.

If mongod --version fails with Illegal instruction or the service exits immediately, recheck the CPU requirement section before troubleshooting the repository. That failure usually means the package installed but the processor or virtual CPU does not expose the instruction set MongoDB requires.

Verify the MongoDB Service on Ubuntu

Confirm that systemd sees mongod as active, then verify that the server is listening on its default local port. Run these checks on a system that passed the CPU requirement check earlier.

systemctl is-active mongod
systemctl is-enabled mongod

A healthy service returns:

active
enabled
ss -lnt | grep ':27017'
LISTEN 0      4096       127.0.0.1:27017      0.0.0.0:*

Secure Your First MongoDB Deployment on Ubuntu

A fresh MongoDB install is fine for local testing, but the first real deployment should add an admin user and then turn on authorization before any remote clients come into the picture.

Create the First MongoDB Admin User on Ubuntu

Create the first administrative account while the server still accepts local unauthenticated setup work. Replace StrongPasswordHere with a real password before creating the user.

mongosh admin <<'MONGOSH'
db.createUser({
   user: "admin",
   pwd: "StrongPasswordHere",
   roles: [
      { role: "userAdminAnyDatabase", db: "admin" },
      "readWriteAnyDatabase"
   ]
})
MONGOSH

This account is enough to create other users and manage the server during the first setup pass. For long-term production use, you can later replace the broad readWriteAnyDatabase role with narrower per-database accounts for each application.

Enable MongoDB Authorization on Ubuntu

Open /etc/mongod.conf and make sure it contains one active security block with authorization enabled.

security:
   authorization: enabled

If the file already has a security section, edit that existing block instead of adding a duplicate. MongoDB only needs one active security key in the YAML file.

sudo systemctl restart mongod

Verify Authenticated MongoDB Admin Access on Ubuntu

Reconnect with the admin account you just created and verify that MongoDB accepts the authenticated admin command before you test the unauthenticated case.

mongosh -u admin -p --authenticationDatabase admin --eval "db.adminCommand({ listDatabases: 1 }).ok"

Relevant output includes:

1

Now confirm that a local connection without credentials is blocked.

mongosh --eval "db.adminCommand({ listDatabases: 1 })"

Relevant output includes:

MongoServerError: Command listDatabases requires authentication

Review MongoDB Network Access on Ubuntu

MongoDB installs with bindIp limited to 127.0.0.1 by default. That is the safest setting for single-host applications, local development, and any system where clients do not need to connect over the network.

Change MongoDB bindIp on Ubuntu for Trusted Networks Only

If another host really needs to reach MongoDB, edit /etc/mongod.conf so the net section includes a specific trusted address instead of exposing the server everywhere.

net:
  port: 27017
  bindIp: 127.0.0.1,192.168.1.10

Replace 192.168.1.10 with the server’s real private address. Avoid 0.0.0.0 unless you are deliberately exposing MongoDB behind tighter network controls and authentication.

sudo systemctl restart mongod

If you use UFW on the host, restrict port 27017 to trusted clients only. If UFW is not set up yet, install and configure UFW on Ubuntu first. Before you bind MongoDB beyond localhost, also review MongoDB’s security checklist and enable authentication for anything outside local testing.

sudo ufw allow from 192.168.1.20 to any port 27017 proto tcp
sudo ufw status numbered

Replace 192.168.1.20 with the trusted client address. The numbered UFW status should show a rule for 27017/tcp limited to that source address instead of a broad public allow.

Update MongoDB on Ubuntu

When you want newer MongoDB 8.3 patch releases from the same upstream repository, upgrade the metapackage instead of doing a full generic system upgrade first.

sudo apt update
sudo apt install --only-upgrade mongodb-org -y

Relevant output includes:

mongodb-org is already the newest version (8.3.1).

If MongoDB publishes a newer 8.3.x build, the same command upgrades the installed packages from the repository you already configured.

Troubleshoot MongoDB on Ubuntu

Most Ubuntu install failures come down to one of three issues: the repository file points at the wrong release, the signing key is missing, or older Ubuntu MongoDB packages are already installed.

Fix Unable to Locate Package mongodb-org on Ubuntu

When APT cannot find mongodb-org, start by checking the source file you wrote earlier. It must match your Ubuntu codename and architecture exactly.

cat /etc/apt/sources.list.d/mongodb-org-8.3.sources
Types: deb
URIs: https://repo.mongodb.org/apt/ubuntu
Suites: noble/mongodb-org/8.3
Components: multiverse
Architectures: amd64
Signed-By: /usr/share/keyrings/mongodb-server-8.0.gpg

On Ubuntu 22.04, the Suites: value should use jammy/mongodb-org/8.3 instead of noble/mongodb-org/8.3. If the file is wrong or missing, recreate it, run sudo apt update again, and recheck apt-cache policy mongodb-org.

Fix MongoDB Public Key Errors on Ubuntu

If apt update reports NO_PUBKEY 4B7C549A058F8B6B, reimport the MongoDB signing key and refresh APT. The 8.3 Ubuntu repo currently expects the 8.0 release key.

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

Fix MongoDB Package Conflicts on Ubuntu

If an older Ubuntu MongoDB package is already installed, it can block mongodb-org by trying to own the same binaries. Check what is already on the system first.

dpkg -l | grep -E 'mongodb|mongo-tools'

If that output includes Ubuntu packages such as mongodb, mongodb-server, or mongodb-server-core, remove those older Ubuntu packages before you retry the official install.

Remove MongoDB from Ubuntu

Removing MongoDB cleanly means stopping the service, purging the packages, and then deciding whether you want to keep or delete the data directory. The data removal step is destructive, so skip it if you still need the databases.

Stop MongoDB and Purge the Packages on Ubuntu

Stop the service first so package removal does not leave a running process behind.

sudo systemctl stop mongod
sudo systemctl disable mongod
sudo apt purge -y mongodb-org mongodb-org-database mongodb-org-server mongodb-org-shell mongodb-mongosh mongodb-org-mongos mongodb-org-tools mongodb-database-tools mongodb-org-database-tools-extra

Verify the MongoDB Packages Are Gone on Ubuntu

Check the package state before you remove the repository file. At this point, the repo can still exist, but the installed package should be gone.

apt-cache policy mongodb-org
mongodb-org:
  Installed: (none)
  Candidate: 8.3.1
  Version table:
     8.3.1 500
        500 https://repo.mongodb.org/apt/ubuntu noble/mongodb-org/8.3/multiverse amd64 Packages

The Installed: (none) line confirms the package is gone. The candidate remains until you remove the MongoDB repository itself.

Remove MongoDB Data and Repository Files on Ubuntu

Delete the repository file and keyring first, then refresh APT so the removed source no longer contributes a package candidate.

sudo rm -f /etc/apt/sources.list.d/mongodb-org-8.3.sources /usr/share/keyrings/mongodb-server-8.0.gpg
sudo apt update

Once the source file is gone and APT has refreshed, check whether the repository files are still present.

test ! -e /etc/apt/sources.list.d/mongodb-org-8.3.sources && test ! -e /usr/share/keyrings/mongodb-server-8.0.gpg && echo "MongoDB repository files removed"
MongoDB repository files removed
apt-cache policy mongodb-org

With the MongoDB source removed and no package installed, this command returns no package data. That empty result confirms Ubuntu no longer has an active MongoDB repository configured.

The next command permanently deletes local MongoDB databases and logs under /var/lib/mongodb and /var/log/mongodb. Back up any databases you still need before removing these directories.

sudo rm -rf /var/lib/mongodb /var/log/mongodb

Conclusion

MongoDB Community Edition is running on Ubuntu from the upstream APT repository, with mongod managed by systemd and mongosh ready for admin work. If the server needs remote clients, configure UFW on Ubuntu before you open port 27017 beyond trusted networks. If a relational database fits the workload better, you can also install PostgreSQL 17 on Ubuntu.

Share this guide

Help another Linux user troubleshoot faster

Share this guide with someone troubleshooting Linux systems or saving it for later.

Follow LinuxCapable

Want more LinuxCapable guides in Google?

Add LinuxCapable as a preferred source so Google can show more of our fresh Linux tutorials in Top Stories and From your sources when relevant.

Add LinuxCapable as a preferred source on Google
Search LinuxCapable

Need another guide?

Search LinuxCapable for package installs, commands, troubleshooting, and follow-up guides related to what you just read.

Found this guide useful?

Support LinuxCapable to keep tutorials free and up to date.

Buy me a coffeeBuy me a coffee
Before commenting, please review our Comments Policy.
Formatting tips for your comment

You can use basic HTML to format your comment. Useful tags currently allowed in published comments:

You type Result
<code>command</code> command
<strong>bold</strong> bold
<em>italic</em> italic
<blockquote>quote</blockquote> quote block

Got a Question or Feedback?

We read and reply to every comment - let us know how we can help or improve this guide.

Verify before posting: