Install MongoDB on Ubuntu

Install MongoDB on Ubuntu using the official MongoDB Community Edition packages. This updated guide covers the current APT-based installation flow for MongoDB 8.0 on supported Ubuntu LTS releases, then keeps the older MongoDB 3.4 steps for Ubuntu 16.04 as a legacy reference.

For a new installation, use the official MongoDB Ubuntu installation documentation as the reference point. MongoDB 8.0 Community Edition supports 64-bit Ubuntu 24.04 LTS (Noble), Ubuntu 22.04 LTS (Jammy), and Ubuntu 20.04 LTS (Focal). If you are using an older Ubuntu release, upgrade the operating system or choose the MongoDB documentation version that matches your server.

Before installing MongoDB on Ubuntu 24.04, 22.04, or 20.04

Check the Ubuntu release and architecture first. The official MongoDB packages are named mongodb-org; they are different from the older mongodb package that may appear in Ubuntu repositories. If the unofficial mongodb package is already installed, remove it before adding the official MongoDB repository.

</>
Copy
cat /etc/os-release
uname -m
apt-cache policy mongodb mongodb-org

If mongodb is installed from Ubuntu repositories and you want the official MongoDB Community Server packages, remove the conflicting package first. Back up any existing data before removing database software.

</>
Copy
sudo systemctl stop mongodb 2>/dev/null || true
sudo apt-get remove -y mongodb mongodb-server mongodb-clients

Install MongoDB 8.0 on Ubuntu using the official APT repository

The current recommended installation method uses curl, gpg, a keyring file, and a MongoDB repository list file. This avoids the older apt-key approach used in many legacy tutorials.

</>
Copy
sudo apt-get update
sudo apt-get install -y gnupg curl

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

Create the MongoDB 8.0 APT source list for your Ubuntu release

Use the Ubuntu codename in the repository line. On Ubuntu 24.04 it should be noble, on Ubuntu 22.04 it should be jammy, and on Ubuntu 20.04 it should be focal. The command below reads the codename from the system and writes the MongoDB 8.0 list file.

</>
Copy
. /etc/os-release
CODENAME="${UBUNTU_CODENAME:-$VERSION_CODENAME}"

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

For clarity, the repository codename must match one of the supported Ubuntu LTS releases. If the command prints a non-LTS codename, do not continue with that repository line.

Ubuntu versionRepository codenameMongoDB 8.0 repository path
Ubuntu 24.04 LTSnoblenoble/mongodb-org/8.0
Ubuntu 22.04 LTSjammyjammy/mongodb-org/8.0
Ubuntu 20.04 LTSfocalfocal/mongodb-org/8.0

Install the MongoDB Community Server packages on Ubuntu

After the repository file is in place, reload the local package database and install the mongodb-org metapackage. This installs the MongoDB server, shell, mongos, database tools, and related package components from the official MongoDB repository.

</>
Copy
sudo apt-get update
sudo apt-get install -y mongodb-org

To check the installed package versions, run:

</>
Copy
mongod --version
mongosh --version

Start, enable, and verify the mongod service on Ubuntu

Modern Ubuntu releases use systemd, so manage MongoDB with systemctl. The service name is mongod, not mongodb.

</>
Copy
sudo systemctl start mongod
sudo systemctl status mongod

If Ubuntu returns Unit mongod.service not found immediately after installation, reload the systemd unit files and start the service again.

</>
Copy
sudo systemctl daemon-reload
sudo systemctl start mongod

Enable MongoDB to start automatically after a reboot:

</>
Copy
sudo systemctl enable mongod

MongoDB logs are stored under /var/log/mongodb/. On current packages, the main log file is usually /var/log/mongodb/mongod.log.

</>
Copy
sudo tail -n 40 /var/log/mongodb/mongod.log

Connect to MongoDB from the Ubuntu terminal with mongosh

After the mongod service is running, open the MongoDB Shell from the same Ubuntu machine. With the default local configuration, mongosh connects to localhost on port 27017.

</>
Copy
mongosh

Run a simple ping command inside the shell to confirm that the server responds.

</>
Copy
db.runCommand({ ping: 1 })
{ ok: 1 }

Stop or restart MongoDB on Ubuntu after installation

Use these service commands when you need to stop MongoDB, restart it after changing /etc/mongod.conf, or check whether it is active.

</>
Copy
sudo systemctl stop mongod
sudo systemctl restart mongod
sudo systemctl is-active mongod

Basic MongoDB authentication steps after Ubuntu installation

For a local development machine, the default localhost-only setup may be enough while learning. For a server or shared environment, create users and enable authorization before exposing the database to applications. Do not open port 27017 to the public internet without authentication, firewall rules, and a clear network plan.

Start by creating an administrative user from mongosh while authentication is still disabled:

</>
Copy
use admin

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

Then edit /etc/mongod.conf and add or update the security section:

</>
Copy
security:
  authorization: enabled

Restart MongoDB and connect with the user you created:

</>
Copy
sudo systemctl restart mongod
mongosh -u adminUser -p --authenticationDatabase admin

Troubleshooting MongoDB installation errors on Ubuntu

The following checks fix most installation issues caused by a missing key, an unsupported Ubuntu codename, or a service that has not been reloaded after package installation.

MongoDB Ubuntu problemLikely causeWhat to check
NO_PUBKEY during apt-get updateMongoDB signing key was not imported correctlyRepeat the curl ... | gpg --dearmor keyring command and confirm the list file uses signed-by=/usr/share/keyrings/mongodb-server-8.0.gpg.
Unable to locate package mongodb-orgRepository list file is missing or has the wrong Ubuntu codenameRun cat /etc/apt/sources.list.d/mongodb-org-8.0.list and verify noble, jammy, or focal.
Unit mongod.service not foundsystemd has not reloaded the service unit yetRun sudo systemctl daemon-reload, then start mongod again.
mongosh cannot connect locallymongod is stopped or listening on a different address or portCheck sudo systemctl status mongod and review /etc/mongod.conf.

MongoDB on Ubuntu QA checklist for this installation

  • Confirm the Ubuntu release is 24.04 LTS, 22.04 LTS, or 20.04 LTS before using the MongoDB 8.0 repository line.
  • Use the official mongodb-org package name, not the older Ubuntu repository package named mongodb.
  • Use a keyring file under /usr/share/keyrings/ instead of adding a new apt-key entry.
  • Start and enable the mongod service with systemctl on modern Ubuntu releases.
  • Verify the installation with both systemctl status mongod and a mongosh ping command.
  • Before production use, configure authentication, review /etc/mongod.conf, keep backups, and restrict network access to trusted clients.

Legacy MongoDB 3.4 installation steps for Ubuntu 16.04

The following section is the earlier tutorial flow for MongoDB 3.4 on Ubuntu 16.04. Keep it only for maintaining older systems. For a new server, use the MongoDB 8.0 steps above instead of the legacy apt-key commands.

Following is a quick overview of the steps we go through while installing MongoDB on Ubuntu.

  1. Import MongoDB GPG public key
  2. Create list file for MongoDB
  3. Reload Local Package Database
  4. Install MongoDB Packages
  5. Start MongoDB
  6. Stop MongoDB

Following is a detailed step by step guide to install MongoDB 3.4 on Ubuntu 16.04.

1. Import MongoDB 3.4 GPG public key on Ubuntu

Open a terminal and run the following command to import MongoDB GPG public key.

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 0C49F3730359A14518585931BC711F9BA15703C6
root@arjun-VPCEH26EN:/home/arjun# sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 0C49F3730359A14518585931BC711F9BA15703C6
Executing: /tmp/tmp.2327Zvbpks/gpg.1.sh --keyserver
hkp://keyserver.ubuntu.com:80
--recv
0C49F3730359A14518585931BC711F9BA15703C6
gpg: requesting key A15703C6 from hkp server keyserver.ubuntu.com
gpg: key A15703C6: public key "MongoDB 3.4 Release Signing Key <packaging@mongodb.com>" imported
gpg: Total number processed: 1
gpg:               imported: 1  (RSA: 1)

2. Create the MongoDB 3.4 list file for Ubuntu

The list file changes from Ubuntu release to release.

Run the corresponding command in terminal for the Ubuntu version of yours.

Ubuntu ReleaseCommand
Ubuntu 12.04 (Precise)echo “deb [ arch=amd64 ] http://repo.mongodb.org/apt/ubuntu precise/mongodb-org/3.4 multiverse” | sudo tee /etc/apt/sources.list.d/mongodb-org-3.4.list
Ubuntu 14.04 (Trusty)echo “deb [ arch=amd64 ] http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.4 multiverse” | sudo tee /etc/apt/sources.list.d/mongodb-org-3.4.list
Ubuntu 16.04 (Xenial)echo “deb [ arch=amd64,arm64 ] http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 multiverse” | sudo tee /etc/apt/sources.list.d/mongodb-org-3.4.list

When run for Ubuntu 16.04 (Xenial)

root@arjun-VPCEH26EN:/home/arjun# echo "deb [ arch=amd64,arm64 ] http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.4.list
deb [ arch=amd64,arm64 ] http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 multiverse

3. Reload the Ubuntu package database for MongoDB 3.4

Run the following command in terminal to reload local package database.

sudo apt-get update

4. Install MongoDB 3.4 packages on Ubuntu

Running following command installs MongoDB Packages.

sudo apt-get install -y mongodb-org
...
The following additional packages will be installed:
  mongodb-org-mongos mongodb-org-server mongodb-org-shell mongodb-org-tools
The following NEW packages will be installed:
  mongodb-org mongodb-org-mongos mongodb-org-server mongodb-org-shell mongodb-org-tools
0 upgraded, 5 newly installed, 0 to remove and 5 not upgraded.
Need to get 66.9 MB of archives.
After this operation, 270 MB of additional disk space will be used.
Get:1 http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4/multiverse amd64 mongodb-org-shell amd64 3.4.9 [7,983 kB]
Get:2 http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4/multiverse amd64 mongodb-org-server amd64 3.4.9 [14.2 MB]
Get:3 http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4/multiverse amd64 mongodb-org-mongos amd64 3.4.9 [8,113 kB]                     
Get:4 http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4/multiverse amd64 mongodb-org-tools amd64 3.4.9 [36.5 MB]                       
Get:5 http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4/multiverse amd64 mongodb-org amd64 3.4.9 [3,526 B]                             
Fetched 66.9 MB in 1min 48s (614 kB/s)                                                                                                         
Selecting previously unselected package mongodb-org-shell.
(Reading database ... 1279975 files and directories currently installed.)
Preparing to unpack .../mongodb-org-shell_3.4.9_amd64.deb ...
Unpacking mongodb-org-shell (3.4.9) ...
Selecting previously unselected package mongodb-org-server.
Preparing to unpack .../mongodb-org-server_3.4.9_amd64.deb ...
Unpacking mongodb-org-server (3.4.9) ...
Selecting previously unselected package mongodb-org-mongos.
Preparing to unpack .../mongodb-org-mongos_3.4.9_amd64.deb ...
Unpacking mongodb-org-mongos (3.4.9) ...
Selecting previously unselected package mongodb-org-tools.
Preparing to unpack .../mongodb-org-tools_3.4.9_amd64.deb ...
Unpacking mongodb-org-tools (3.4.9) ...
Selecting previously unselected package mongodb-org.
Preparing to unpack .../mongodb-org_3.4.9_amd64.deb ...
Unpacking mongodb-org (3.4.9) ...
Processing triggers for man-db (2.7.5-1) ...
Setting up mongodb-org-shell (3.4.9) ...
Setting up mongodb-org-server (3.4.9) ...
Adding system user `mongodb' (UID 130) ...
Adding new user `mongodb' (UID 130) with group `nogroup' ...
Not creating home directory `/home/mongodb'.
Adding group `mongodb' (GID 139) ...
Done.
Adding user `mongodb' to group `mongodb' ...
Adding user mongodb to group mongodb
Done.
Setting up mongodb-org-mongos (3.4.9) ...
Setting up mongodb-org-tools (3.4.9) ...
Setting up mongodb-org (3.4.9) ...

5. Start MongoDB 3.4 on Ubuntu

To start MongoDB, run the following command in terminal

sudo service mongod start

To verify if MongoDB has started, check the legacy log file at /var/log/mongodb/mongodb.log or the current package log file at /var/log/mongodb/mongod.log.

2017-10-14T17:08:27.786+0530 I INDEX    [initandlisten] build index done.  scanned 0 total records. 0 secs
2017-10-14T17:08:27.787+0530 I COMMAND  [initandlisten] setting featureCompatibilityVersion to 3.4
2017-10-14T17:08:27.787+0530 I NETWORK  [thread1] waiting for connections on port 27017

MongoDB would be started and waiting for connections on 27017.

6. Stop MongoDB 3.4 on Ubuntu

To stop MongoDB, run the following command in terminal

sudo service mongod stop
2017-10-14T17:11:28.121+0530 I CONTROL  [signalProcessingThread] got signal 15 (Terminated), will terminate after current cmd ends
2017-10-14T17:11:28.121+0530 I NETWORK  [signalProcessingThread] shutdown: going to close listening sockets...
2017-10-14T17:11:28.121+0530 I NETWORK  [signalProcessingThread] closing listening socket: 7
2017-10-14T17:11:28.122+0530 I NETWORK  [signalProcessingThread] closing listening socket: 8
2017-10-14T17:11:28.122+0530 I NETWORK  [signalProcessingThread] removing socket file: /tmp/mongodb-27017.sock
2017-10-14T17:11:28.122+0530 I NETWORK  [signalProcessingThread] shutdown: going to flush diaglog...
2017-10-14T17:11:28.122+0530 I FTDC     [signalProcessingThread] Shutting down full-time diagnostic data capture
2017-10-14T17:11:28.127+0530 I STORAGE  [signalProcessingThread] WiredTigerKVEngine shutting down
2017-10-14T17:11:28.475+0530 I STORAGE  [signalProcessingThread] shutdown: removing fs lock...
2017-10-14T17:11:28.475+0530 I CONTROL  [signalProcessingThread] now exiting
2017-10-14T17:11:28.475+0530 I CONTROL  [signalProcessingThread] shutting down with code:0
2017-10-14T17:11:28.475+0530 I CONTROL  [initandlisten] shutting down with code:0

MongoDB Ubuntu installation FAQs

How do I install MongoDB on Ubuntu 24.04 using the terminal?

Install gnupg and curl, import the MongoDB 8.0 signing key into /usr/share/keyrings/, create the MongoDB APT list file with the noble repository path, run sudo apt-get update, and install mongodb-org. Then start the service with sudo systemctl start mongod.

What is the correct service name for MongoDB on Ubuntu?

The service name for official MongoDB packages is mongod. Use commands such as sudo systemctl start mongod, sudo systemctl status mongod, and sudo systemctl restart mongod.

How do I run MongoDB in the Ubuntu terminal after installation?

Start the mongod service, then run mongosh from the terminal. Inside the MongoDB Shell, run db.runCommand({ ping: 1 }) to verify the connection.

Why does Ubuntu show “Unable to locate package mongodb-org”?

This usually means the MongoDB repository list file is missing, the package database was not updated, or the repository codename does not match your Ubuntu release. Check /etc/apt/sources.list.d/mongodb-org-8.0.list, confirm the codename, and run sudo apt-get update again.

Should I use apt-key to install MongoDB on modern Ubuntu releases?

No. For current Ubuntu releases, use a GPG keyring file with the signed-by= option in the repository line. The older apt-key command is kept in this page only for the legacy MongoDB 3.4 Ubuntu 16.04 section.

MongoDB on Ubuntu installation recap

In this MongoDB tutorial, we installed MongoDB Community Edition on Ubuntu using the official repository, started the mongod service, verified the server with mongosh, reviewed basic authentication steps, and kept the old MongoDB 3.4 Ubuntu commands as a legacy reference. For new Ubuntu installations, prefer the MongoDB 8.0 steps at the top of the tutorial.