How to Install MongoDB on Ubuntu

Install MongoDB 8.0 Community on Ubuntu from the official mongodb-org repo with gpg keyring (jammy/noble/focal), start mongod with systemctl, connect with mongosh, install shell-only for Atlas, and fix libssl1.1, AVX, and ECONNREFUSED errors.

Published

Updated

Read time 8 min read

Reviewed byDeepak Prasad

Install MongoDB on Ubuntu banner with database icon, mongosh terminal, and Ubuntu orange accent

MongoDB is a document-oriented NoSQL database used for modern apps, analytics, and microservices. On Ubuntu you install the official mongodb-org packages from MongoDB Inc.—not the outdated mongodb package from Ubuntu universe, which conflicts with the official repo (MongoDB docs).

This guide shows how to install MongoDB Community Edition on Ubuntu (8.0 tested), import the GPG key the modern way (no deprecated apt-key), pick the correct jammy / noble / focal repo line, start mongod with systemd, connect with mongosh, install shell-only for Atlas, pin versions, and fix libssl1.1, AVX, and ECONNREFUSED errors described in Ask Ubuntu.

Tested on: Ubuntu 25.04 (Plucky Puffin); kernel 6.14.0-37-generic; amd64; MongoDB 8.0.26 from noble/mongodb-org/8.0 repo.

NOTE
MongoDB 8.0 officially supports Ubuntu 20.04 (focal), 22.04 (jammy), and 24.04 (noble) on x86_64. On Ubuntu 25.04, use the noble repo until MongoDB publishes plucky packages—this was verified on a plucky host. Always use your release codename in the repo URL, not an older LTS unless you enjoy libssl1.1 dependency errors.

Quick command summary

Task Command
Check Ubuntu codename lsb_release -cs
Check AVX support grep flags -m1 /proc/cpuinfo | grep avx
Import GPG key (8.0) curl -fsSL https://pgp.mongodb.com/server-8.0.asc | sudo gpg --dearmor -o /usr/share/keyrings/mongodb-server-8.0.gpg
Add repo (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
Install full server sudo apt update && sudo apt install -y mongodb-org
Shell only (Atlas / remote) sudo apt install -y mongodb-mongosh
Enable and start sudo systemctl enable --now mongod
Verify service sudo systemctl status mongod
Connect locally mongosh
Remove everything sudo apt purge -y 'mongodb-org*'

Prerequisites

  • Ubuntu 20.04, 22.04, or 24.04 LTS (recommended). 25.04 tested with the noble repo workaround below.
  • sudo and outbound HTTPS to repo.mongodb.org.
  • gnupg and cURL for the signed repository setup.
  • OpenSSL 3 on Ubuntu 22.04+ (see install OpenSSL on Ubuntu if openssl version fails).
  • Remove conflicting Ubuntu packages first: sudo apt purge mongodb if installed.

What you are installing

Package Role
mongodb-org Metapackage—server, shell, tools
mongodb-org-server mongod daemon, /etc/mongod.conf, systemd unit
mongodb-mongosh Modern mongosh shell (replaces legacy mongo)
mongodb-org-shell Org shell metapackage slice (mongosh is separate package)
mongodb-org-tools mongodump, mongorestore, mongoimport, etc.
mongodb-database-tools Database tools binaries

Data directory: /var/lib/mongodb (owner mongodb). Logs: /var/log/mongodb/mongod.log. Config: /etc/mongod.conf.


Choose Ubuntu codename and MongoDB version

Ubuntu release Codename Repo path segment (8.0)
24.04 LTS noble noble/mongodb-org/8.0
22.04 LTS jammy jammy/mongodb-org/8.0
20.04 LTS focal focal/mongodb-org/8.0
25.04 (interim) plucky Use noble until plucky repo exists (tested)

For MongoDB 7.0 or 6.0, change 8.0 to the desired series in both the GPG URL (server-7.0.asc) and list file. This article tests 8.0.26 per the v8.0 install guide.

Do not point Ubuntu 22.04 at focal/mongodb-org/6.0—that was the root cause of libssl1.1 errors in 2022 forum posts. Use jammy on 22.04.


Step 1: Import the MongoDB GPG key

Install prerequisites:

bash
sudo apt-get install -y gnupg curl

Import the key into /usr/share/keyrings/ (replaces deprecated apt-key add):

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

No output means success. This matches current MongoDB documentation and avoids the Warning: apt-key is deprecated message from older tutorials.


Step 2: Add the official MongoDB repository

Pick one line for your release.

Ubuntu 24.04 (noble)—also used on tested 25.04:

bash
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

Ubuntu 22.04 (jammy):

bash
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 20.04 (focal):

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

Reload indexes:

bash
sudo apt-get update
apt-cache policy mongodb-org

Example on Ubuntu 25.04 using the noble repo:

text
mongodb-org:
 Installed: (none)
 Candidate: 8.0.26
 Version table:
 8.0.26 500
 500 https://repo.mongodb.org/apt/ubuntu noble/mongodb-org/8.0/multiverse amd64 Packages

Step 3: Install MongoDB Community Edition (full server)

Install the metapackage with apt:

bash
sudo apt-get install -y mongodb-org

Verify packages:

bash
dpkg -l | grep -i mongo
which mongod mongosh
mongod --version
mongosh --version
text
ii mongodb-database-tools 100.17.0 amd64
ii mongodb-mongosh 2.8.3 amd64
ii mongodb-org 8.0.26 amd64
ii mongodb-org-server 8.0.26 amd64
ii mongodb-org-shell 8.0.26 amd64
ii mongodb-org-tools 8.0.26 amd64
/usr/bin/mongod
/usr/bin/mongosh
db version v8.0.26
...
 "openSSLVersion": "OpenSSL 3.4.1 11 Feb 2025",
 "environment": {
 "distmod": "ubuntu2404",
 "distarch": "x86_64"
 }
2.8.3

Pin a specific version (optional)

To prevent unintended upgrades (MongoDB docs):

bash
sudo apt-get install -y \
 mongodb-org=8.0.26 \
 mongodb-org-database=8.0.26 \
 mongodb-org-server=8.0.26 \
 mongodb-org-shell=8.0.26 \
 mongodb-org-mongos=8.0.26 \
 mongodb-org-tools=8.0.26 \
 mongodb-org-database-tools-extra=8.0.26

Hold packages:

bash
echo "mongodb-org hold" | sudo dpkg --set-selections
echo "mongodb-org-server hold" | sudo dpkg --set-selections
echo "mongodb-mongosh hold" | sudo dpkg --set-selections

Step 4: Start mongod and enable at boot

bash
sudo systemctl daemon-reload
sudo systemctl enable mongod
sudo systemctl start mongod
sudo systemctl status mongod
text
● mongod.service - MongoDB Database Server
 Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; preset: enabled)
 Active: active (running) since Thu 2026-06-25 12:24:21 IST; 7s ago
 Docs: https://docs.mongodb.org/manual
 Main PID: 1905188 (mongod)
 └─1905188 /usr/bin/mongod --config /etc/mongod.conf

Confirm localhost binding:

bash
ss -tlnp | grep 27017
text
LISTEN 0 4096 127.0.0.1:27017 0.0.0.0:*

By default bindIp: 127.0.0.1 in /etc/mongod.conf—remote hosts cannot connect until you change bind settings and enable authentication (production notes).


Step 5: Connect with mongosh and run a quick test

Wait a few seconds after the first start, then:

bash
mongosh

If you see ECONNREFUSED, wait 2–5 seconds and retry—mongod was still initializing WiredTiger storage.

bash
mongosh --quiet --eval "db.runCommand({ connectionStatus: 1 })"
text
{
 authInfo: { authenticatedUsers: [], authenticatedUserRoles: [] },
 ok: 1
}

List databases and insert a document:

bash
mongosh --quiet --eval "show dbs"
mongosh --quiet --eval "db.test.insertOne({hello:'world'}); db.test.find"
text
admin 40.00 KiB
config 12.00 KiB
local 40.00 KiB
[ { _id: ObjectId('6a3cd0dd9ab4614d059df8a3'), hello: 'world' } ]

The legacy mongo shell no longer ships with MongoDB 6.0+ packages—use mongosh only (Ask Ubuntu).


Step 6: Install mongosh only (no local server)

When you use MongoDB Atlas or a remote cluster and only need the CLI client (Ask Ubuntu #1127055):

  1. Complete Step 1 and Step 2 (GPG key + repo).
  2. Install the shell package only:
bash
sudo apt-get update
sudo apt-get install -y mongodb-mongosh

Optional tools for backups without mongod:

bash
sudo apt-get install -y mongodb-database-tools

Connect to Atlas:

bash
mongosh "mongodb+srv://cluster.example.mongodb.net/mydb" --username myuser

You do not need mongodb-org-server or systemctl start mongod for cloud-only workflows.


Step 7: Install database tools

mongodb-org-tools (pulled in by mongodb-org) includes:

  • mongodump / mongorestore
  • mongoimport / mongoexport
  • mongostat / mongotop

Verify:

bash
mongodump --version

Install tools alone (after repo is configured):

bash
sudo apt-get install -y mongodb-org-tools

Uninstall MongoDB

Stop the service and purge packages (MongoDB docs):

bash
sudo systemctl stop mongod
sudo apt-get purge -y 'mongodb-org*'
sudo rm -rf /var/lib/mongodb /var/log/mongodb
sudo rm -f /etc/apt/sources.list.d/mongodb-org-8.0.list
sudo rm -f /usr/share/keyrings/mongodb-server-8.0.gpg
sudo apt-get update
sudo apt-get autoremove -y   # optional

This permanently deletes all databases on the host. Back up with mongodump first. For reviewing orphaned dependencies after purge, see remove unused packages on Ubuntu.


Troubleshooting

Symptom Likely cause Fix
Depends: libssl1.1 on Ubuntu 22.04 Wrong repo codename (focal on jammy) Use jammy/mongodb-org/X.Y in sources list—not focal; drop manual libssl1.1 .deb hacks
apt-key is deprecated warning Old tutorial Use gpg --dearmor/usr/share/keyrings/mongodb-server-8.0.gpg with signed-by=
Illegal instruction / core-dump on start CPU lacks AVX (MongoDB 5.0+) grep avx /proc/cpuinfo; use MongoDB 4.4 or newer VM host
ECONNREFUSED 127.0.0.1:27017 mongod still starting or not running sudo systemctl status mongod; wait and retry; read /var/log/mongodb/mongod.log
Conflicts with mongodb package Ubuntu universe package installed sudo apt purge mongodb; install mongodb-org from official repo
Package mongodb-org has no installation candidate Wrong codename or disabled multiverse Match noble/jammy/focal; on 25.04 use noble
Remote clients cannot connect Default bindIp: 127.0.0.1 Edit /etc/mongod.conf bind IP and enable auth before exposing to network
soft rlimits too low warning Open file limit under 64000 Raise ulimit -n per MongoDB production notes
mongo: command not found Legacy shell removed Install and use mongosh
Unintended version upgrade apt tracks latest in repo Pin version and dpkg --set-selections hold

Optional: tarball install (advanced)

MongoDB publishes binary tarballs at mongodb.com/try/download/community. Use tarballs when you cannot use apt (air-gapped custom layout) or need a specific binary path. Typical flow:

bash
sudo apt-get install -y libcurl4 openssl liblzma5
cd /tmp

The openssl package is usually preinstalled on Ubuntu—see install OpenSSL on Ubuntu if the tarball step fails with openssl: command not found.

bash
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu2404-8.0.26.tgz
tar -xzf mongodb-linux-x86_64-ubuntu2404-8.0.26.tgz
sudo cp mongodb-linux-*/bin/{mongod,mongos} /usr/local/bin/
sudo mkdir -p /var/lib/mongo /var/log/mongodb
sudo chown -R $USER /var/lib/mongo /var/log/mongodb
mongod --dbpath /var/lib/mongo --logpath /var/log/mongodb/mongod.log --fork

Install mongosh separately from the repo (mongodb-mongosh deb) or MongoDB Shell download. Prefer apt mongodb-org for systemd integration and supported upgrades.


References


Summary

The supported way to install MongoDB on Ubuntu is the official mongodb-org repository: import the GPG key to /usr/share/keyrings/, add the list file with the correct noble, jammy, or focal codename, run sudo apt install -y mongodb-org, then sudo systemctl enable --now mongod. On Ubuntu 25.04, use the noble repo until plucky packages exist—8.0.26 installed and ran successfully in tests.

Connect with mongosh, not the removed mongo shell. For Atlas-only users, install mongodb-mongosh without the server. Avoid focal repos on jammy hosts, skip libssl1.1 workarounds on modern releases, and verify AVX before installing MongoDB 5.0+.

Frequently Asked Questions

1. How do I install MongoDB on Ubuntu?

Import the MongoDB GPG key to /usr/share/keyrings/, add the official mongodb-org repo for your Ubuntu codename (noble for 24.04, jammy for 22.04, focal for 20.04), run sudo apt update && sudo apt install -y mongodb-org, then sudo systemctl enable --now mongod. Connect with mongosh.

2. Why does MongoDB fail with libssl1.1 dependency on Ubuntu 22.04?

Early MongoDB 6.0 packages targeted Ubuntu 20.04 (focal) and required libssl1.1. Use the jammy repo (not focal) on 22.04—MongoDB 6.0.3+ builds natively for jammy with OpenSSL 3. Do not manually install libssl1.1 .deb files on current releases unless you are deliberately pinning an obsolete focal repo.

3. How do I install only the MongoDB shell without the server?

After adding the MongoDB repo, run sudo apt install -y mongodb-mongosh (modern shell) or mongodb-org-shell for the full org metapackage slice. Use mongosh "mongodb+srv://..." to connect to MongoDB Atlas without installing mongod locally.

4. What is the difference between mongo and mongosh?

The legacy mongo shell was removed from MongoDB 6.0 packages. mongosh is the supported JavaScript/Node.js REPL for MongoDB 4.2+ and is the only shell you should install today.

5. Can I use apt install mongodb on Ubuntu?

No for production—Ubuntu universe mongodb packages conflict with official mongodb-org packages and are often outdated. Purge sudo apt purge mongodb if present, then install from repo.mongodb.org per MongoDB documentation.

6. Which Ubuntu codename do I use in the MongoDB repo line?

Match your LTS release: noble for 24.04, jammy for 22.04, focal for 20.04. On Ubuntu 25.04 (plucky) use noble until MongoDB publishes plucky packages—tested successfully with noble/mongodb-org/8.0.

7. Why does mongod fail with Illegal instruction or core-dump?

MongoDB 5.0+ requires AVX CPU instructions on x86_64. Check grep flags -m1 /proc/cpuinfo | grep avx. Older VMs and CPUs need MongoDB 4.4 or a host with AVX support.

8. Why does mongosh show ECONNREFUSED right after systemctl start mongod?

mongod needs a few seconds to bind port 27017 on first start. Wait 2–5 seconds and retry, or check sudo systemctl status mongod and tail /var/log/mongodb/mongod.log.

9. How do I uninstall MongoDB from Ubuntu?

sudo systemctl stop mongod; sudo apt purge -y mongodb-org*; sudo rm -rf /var/lib/mongodb /var/log/mongodb; remove /etc/apt/sources.list.d/mongodb-org-*.list and the keyring under /usr/share/keyrings/.
Omer Cakmak

Linux Administrator

Highly skilled at managing Debian, Ubuntu, CentOS, Oracle Linux, and Red Hat servers. Proficient in bash scripting, Ansible, and AWX central server management, he handles server operations on …