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.
- 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.
On this page
Toggle1. Check Your System
Open a terminal and check your release, architecture and init system:
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.
On AMD64, MongoDB requires AVX support, including in virtual machines. Check before installing:
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:
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
Refresh package information and install the tools needed to fetch the signing key:
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.
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.
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.
Use the command for your release only. It creates or replaces the named repository file; inspect it first if you have already configured MongoDB.
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:
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.
Refresh APT and inspect the candidate before installing:
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.
sudo apt install mongodb-org
Review the package list. This installs the server, MongoDB Shell and database tools through the official packages.
Start MongoDB now and enable it at boot:
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:
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.
Open the local administrative database:
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.
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.
Open the configuration file. Restarting the service will briefly interrupt connected applications.
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:
security:
authorization: enabled
Do not duplicate a security key. Save with Ctrl+O, Enter, and exit with Ctrl+X. Then restart and check:
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.
Connect as the administrator. The password is requested interactively:
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:
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
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:
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.
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.
Connect as the limited user. Its authentication database is ubuntuFreeDemo, where the account was created:
mongosh ubuntuFreeDemo --username demoApp --password --authenticationDatabase ubuntuFreeDemo
Enter the application user password. At the MongoDB shell prompt, insert and read a sample document:
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.
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:
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:
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:
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:
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
APT may not be able to read the signing key, or the repository file may point to the wrong keyring. Inspect both:
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.
A missing source, wrong Ubuntu codename or failed repository refresh can leave APT without a candidate. Check:
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.
Another MongoDB instance or service may already be listening on the configured port. Identify it first:
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.
Read the service log and inspect the configured data path and its permissions:
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:
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.
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
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.
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.
Remove the source and keyring only if no other configured source uses them:
sudo rm -f /etc/apt/sources.list.d/mongodb-org-9.0.list /usr/share/keyrings/mongodb-server-9.0.gpg
sudo apt update
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.
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
