How to Install MongoDB on Ubuntu 26.04 and 24.04

Share

To install MongoDB on Ubuntu 26.04 or 24.04, use the official MongoDB APT repository. MongoDB is a document database that stores records in flexible, JSON-like documents.

This guide installs Community Edition, checks the service, enables authentication, and explains updates and removal. If you need help, leave a comment or contact us.

Before you begin
  • Ubuntu 26.04 or 24.04, 64-bit AMD64 or ARM64, internet access and sudo.
  • A new standalone installation with systemd. Back up existing databases before changes; do not treat this as an upgrade guide.
  • MongoDB 9.0.4 or newer is required for Ubuntu 26.04. Check CPU support below.

1. Check Your System

1   Check Ubuntu and architecture

Open a terminal and check your release, architecture and init system:

Terminal
cat /etc/os-release
dpkg --print-architecture
ps -p 1 -o comm=

Look for Ubuntu 26.04 (resolute) or 24.04 (noble), amd64 or arm64, and systemd. If these do not match, stop and check the official platform documentation. WSL or containers may need a different service setup.

2   Check the processor and existing packages

On AMD64, MongoDB requires AVX support, including in virtual machines. Check before installing:

Terminal
grep -qw avx /proc/cpuinfo && echo "AVX available" || echo "AVX not detected"

Run this check on AMD64 only. If AVX is missing, stop and check the CPU or virtual machine settings. ARM64 has separate architecture requirements; check the official production notes for your processor.

Check for an existing MongoDB installation:

Terminal
dpkg-query -W -f='${binary:Package} ${Version} ${db:Status-Abbrev}\n' '*mongo*' 2>/dev/null

If packages or an existing database are present, do not remove them or install over them without a backup and a version-specific upgrade plan. Package names alone do not establish database compatibility.

2. Install MongoDB with APT

1   Install repository prerequisites

Refresh package information and install the tools needed to fetch the signing key:

Terminal
sudo apt update
sudo apt install ca-certificates curl gnupg

If the update reports an error, fix it before continuing. Review proposed package changes when prompted.

2   Add the MongoDB signing key

Download the official MongoDB 9.0 signing key and convert it into a dedicated APT keyring. The second command replaces a file of the same name if one exists.

Terminal
curl -fsSLo /tmp/mongodb-server-9.asc https://pgp.mongodb.com/server-9.asc &&
sudo gpg --dearmor --yes -o /usr/share/keyrings/mongodb-server-9.0.gpg /tmp/mongodb-server-9.asc

The second command runs only if the download succeeds. The keyring lets APT verify packages from this repository.

3   Add the repository for your Ubuntu release

Use the command for your release only. It creates or replaces the named repository file; inspect it first if you have already configured MongoDB.

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

That command is for Ubuntu 26.04. For Ubuntu 24.04, use this instead:

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

Do not use a different Ubuntu release’s repository as a workaround.

4   Check the package source and install

Refresh APT and inspect the candidate before installing:

Terminal
sudo apt update
apt-cache policy mongodb-org

The candidate should be from MongoDB’s 9.0 repository for your Ubuntu codename. On 26.04 it must be 9.0.4 or newer. If no candidate appears or the update fails, stop and use troubleshooting below.

Terminal
sudo apt install mongodb-org

Review the package list. This installs the server, MongoDB Shell and database tools through the official packages.

5   Start and verify the service

Start MongoDB now and enable it at boot:

Terminal
sudo systemctl enable --now mongod
systemctl is-active mongod
systemctl is-enabled mongod

Expect active and enabled. If systemd says the unit is not found, run sudo systemctl daemon-reload, then retry. Test the server and shell:

Terminal
mongod --version
mongosh --version
mongosh --quiet --eval 'db.runCommand({ ping: 1 })'

A successful ping contains ok: 1. A version number alone does not prove the server is running.

3. Secure Your MongoDB Installation

A new standalone installation normally listens locally and has authorization disabled. The following steps are for a fresh, local instance without existing users. They briefly create the first administrator before access control is enabled; keep the server on localhost throughout setup.

1   Create an administrator

Open the local administrative database:

Terminal
mongosh admin

At the mongosh prompt, create an administrator. The shell prompts for the password so it is not written in the command itself. Choose a strong password and store it safely.

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

Wait for a successful response. If creation fails, stop and investigate before enabling authorization. This account can manage users and read and write data; create narrower application users later. Exit the shell with exit.

2   Enable authorization

Open the configuration file. Restarting the service will briefly interrupt connected applications.

Terminal
sudo nano /etc/mongod.conf

Find the existing security: section, which may be commented out. Enable or add this block once at the top YAML level, using spaces, not tabs:

Configuration
security:
  authorization: enabled

Do not duplicate a security key. Save with Ctrl+O, Enter, and exit with Ctrl+X. Then restart and check:

Terminal
sudo systemctl restart mongod
systemctl is-active mongod

If restart fails, inspect sudo journalctl -u mongod -n 50 --no-pager, correct the YAML, and retry. Do not disable authorization merely to hide the error.

3   Test access

Connect as the administrator. The password is requested interactively:

Terminal
mongosh admin --username siteAdmin --password

Run db.adminCommand({ listDatabases: 1 }).ok at the shell prompt; a permitted request returns 1. Exit, then confirm an unauthenticated request is refused:

Terminal
mongosh --quiet --eval 'db.adminCommand({ listDatabases: 1 })'

Expect an authentication or authorization error. If the command lists databases, access control is not active; check the configuration and service before continuing.

4. Try MongoDB

1   Create a limited application user

Use siteAdmin for administration, not application connections. An application user limited to one database reduces the damage from a leaked password.

Connect as the administrator and enter its password when prompted:

Terminal
mongosh admin --username siteAdmin --password

At the MongoDB shell prompt, create a user in ubuntuFreeDemo. Choose a new strong password when prompted. The readWrite role applies only to that database.

MongoDB shell
db.getSiblingDB("ubuntuFreeDemo").createUser({ user: "demoApp", pwd: passwordPrompt(), roles: [ { role: "readWrite", db: "ubuntuFreeDemo" } ] })

A successful creation returns { ok: 1 }. If it fails, check that you authenticated as siteAdmin on the admin database. Type exit to leave the shell.

2   Connect and insert a document

Connect as the limited user. Its authentication database is ubuntuFreeDemo, where the account was created:

Terminal
mongosh ubuntuFreeDemo --username demoApp --password --authenticationDatabase ubuntuFreeDemo

Enter the application user password. At the MongoDB shell prompt, insert and read a sample document:

MongoDB shell
db.notes.insertOne({ topic: "MongoDB", status: "working" })
db.notes.findOne({ topic: "MongoDB" })

The insert reports acknowledged: true. The query returns a document like this; the generated ID will differ:

{
  _id: ObjectId('...'),
  topic: 'MongoDB',
  status: 'working'
}

This account cannot write to other databases. To remove the example collection, first confirm the selected database is ubuntuFreeDemo, then run db.notes.drop(). This permanently deletes that collection. Do not use it on data you need.

5. Review Network Access

Keep the default localhost binding if applications run on the same machine. If another trusted host needs access, enable authentication first and review MongoDB’s security checklist. Authentication and a firewall do not encrypt traffic; use TLS or a properly configured secure tunnel for remote connections.

1   Allow a trusted client when needed

In /etc/mongod.conf, edit the existing net section to include the server’s actual private interface address alongside localhost. This example address is a placeholder, not the client address:

Configuration
net:
  port: 27017
  bindIp: 127.0.0.1,192.168.1.10

Replace the private address with one assigned to this server. Save, restart mongod, and check the service. Avoid binding to all interfaces. On an active UFW firewall, allow only the trusted client address, for example:

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

Replace the example with the client’s real IP. This adds a firewall rule but does not enable UFW. Check existing rules and cloud firewalls too. Do not enable or reset a remote firewall without first preserving SSH access.

6. Update MongoDB

Before updates, verify your database backup and plan for a possible service interruption. sudo apt upgrade is system-wide and can upgrade unrelated Ubuntu packages.

For the packages installed in this guide, refresh APT and explicitly request updates for the installed MongoDB components:

Terminal
sudo apt update
sudo apt install --only-upgrade mongodb-org mongodb-org-database mongodb-org-server mongodb-org-shell mongodb-org-mongos mongodb-org-tools mongodb-org-database-tools-extra mongodb-mongosh mongodb-database-tools

Review the proposed changes before confirming. APT may also need to change dependencies. If any listed package is not installed, --only-upgrade leaves it uninstalled; check the package list if your installation differs. This follows the configured 9.0 repository. Check the service and version afterwards:

Terminal
systemctl is-active mongod
mongod --version

Major MongoDB upgrades and Ubuntu release upgrades need a compatible release, a verified backup and MongoDB’s version-specific upgrade procedure. Changing the repository alone is not an upgrade plan.

7. Troubleshooting

1   NO_PUBKEY or signature errors

APT may not be able to read the signing key, or the repository file may point to the wrong keyring. Inspect both:

Terminal
cat /etc/apt/sources.list.d/mongodb-org-9.0.list
gpg --show-keys /usr/share/keyrings/mongodb-server-9.0.gpg

Check that signed-by names this keyring. If it is missing or incorrect, repeat the signing-key step above and run sudo apt update. If verification still fails, check the error and MongoDB’s current signing-key instructions. Do not disable signature verification.

2   Unable to locate package mongodb-org

A missing source, wrong Ubuntu codename or failed repository refresh can leave APT without a candidate. Check:

Terminal
cat /etc/os-release
cat /etc/apt/sources.list.d/mongodb-org-9.0.list
sudo apt update
apt-cache policy mongodb-org

Confirm resolute for Ubuntu 26.04 or noble for 24.04, then correct the source using the matching repository step above. Resolve any APT errors before retrying the install.

3   Port 27017 is already in use

Another MongoDB instance or service may already be listening on the configured port. Identify it first:

Terminal
sudo ss -ltnp '( sport = :27017 )'

If another service owns the port, stop it only after confirming it is safe to interrupt. Alternatively, choose an unused port in the existing net section of /etc/mongod.conf and use that port in client connections. Restart mongod and check its status. Do not delete a socket or stop an unknown process blindly.

4   Data directory permission errors

Read the service log and inspect the configured data path and its permissions:

Terminal
sudo journalctl -u mongod -n 50 --no-pager
sudo grep -A 3 '^storage:' /etc/mongod.conf
sudo namei -l /var/lib/mongodb
sudo ls -ld /var/lib/mongodb

The path commands inspect the default directory; substitute your actual storage.dbPath if it differs. If the log identifies an ownership problem and you have confirmed this is the default data directory used by the package service, restore the directory owner:

Terminal
sudo chown mongodb:mongodb /var/lib/mongodb
sudo systemctl restart mongod
systemctl is-active mongod

This changes the directory itself, not its contents. If a specific file or parent directory still blocks access, inspect that path and correct its ownership only after verifying it belongs to this instance. Avoid recursive ownership changes or world-writable permissions.

5   Illegal instruction or authentication failure

On AMD64, an illegal instruction can mean that AVX is unavailable or hidden by a virtual machine. Recheck the processor step. For authentication, use the correct username, password and authentication database: admin for siteAdmin, and ubuntuFreeDemo for demoApp. Check the service log if it still fails.

8. Remove MongoDB

1   Stop the service and review removal

Back up databases first. Stopping the service interrupts connected applications. Review the list of packages before accepting removal; the shell and tools may be shared by other MongoDB installations.

Terminal
sudo systemctl disable --now mongod
sudo apt purge mongodb-org mongodb-org-database mongodb-org-server mongodb-org-mongos mongodb-org-tools mongodb-org-database-tools-extra mongodb-mongosh mongodb-database-tools

Check installed MongoDB packages with dpkg-query as shown above. Package removal does not mean your database files are gone.

2   Remove this guide’s repository

Remove the source and keyring only if no other configured source uses them:

Terminal
sudo rm -f /etc/apt/sources.list.d/mongodb-org-9.0.list /usr/share/keyrings/mongodb-server-9.0.gpg
sudo apt update
3   Optionally delete local data and logs

Permanent deletion: this removes databases and logs in the default directories. Skip this step if you want to keep them. Confirm the actual storage.dbPath and systemLog.path in your saved configuration and verify backups first. Do not run this if those paths contain anything you need.

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

Custom paths are not removed by this command. Removing the packages and files does not remove backups stored elsewhere.

Helpful Resources

Read MongoDB’s platform support matrix, authentication guide and production notes for details beyond a local standalone installation.

Frequently Asked Questions

Is MongoDB supported on Ubuntu 26.04?

MongoDB Community supports Ubuntu 26.04 on supported 64-bit AMD64 and ARM64 hardware starting with MongoDB 9.0.4. Check the current platform matrix before installing.

Is MongoDB Community Edition free?

MongoDB Community Edition is available without a subscription. Review MongoDB’s license terms for your intended use.

What is the difference between mongod and mongosh?

mongod is the database server. mongosh is the shell used to connect to and administer a MongoDB server.

Does MongoDB start automatically after installation?

Do not assume it is running after package installation. Enable and start mongod with systemctl, then check that it is active.

Is a local MongoDB installation password protected by default?

A fresh standalone installation generally has authorization disabled. Create an administrator and enable authorization before allowing other clients to access it.

Will removing MongoDB delete my databases?

Removing packages does not necessarily remove database files. Deleting the data directory is a separate, irreversible step.

Related guides: Install curl on Ubuntu · Back up files on Ubuntu · Install Kubernetes on Ubuntu