Installing MongoDB on Linux
MongoDB can be installed on various Linux distributions like Ubuntu, Debian, CentOS, or RHEL. In this guide, we will walk through installing MongoDB Community Edition on Ubuntu (20.04 or 22.04), the most popular Linux distribution for developers.
Step 1: Import the MongoDB public GPG key
This key ensures that the software you're installing is trusted and verified.
curl -fsSL https://pgp.mongodb.com/server-7.0.asc | \
sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg \
--dearmor
Explanation: This command downloads the MongoDB GPG key and saves it to your system keyring securely.
Step 2: Add the MongoDB repository
echo "deb [ signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] \
https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/7.0 multiverse" | \
sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
Explanation: This adds the official MongoDB 7.0 repository to your system so you can install it using apt
.
Step 3: Update your system package list
sudo apt update
Output:
Hit:1 http://archive.ubuntu.com/ubuntu focal InRelease Get:2 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/7.0 ... Reading package lists... Done
Explanation: This refreshes your package index to include the newly added MongoDB repository.
Step 4: Install MongoDB
sudo apt install -y mongodb-org
Output:
Reading package lists... Done ... Setting up mongodb-org-server ... Setting up mongodb-org-mongos ...
Explanation: This installs the MongoDB server, client tools, and the shell on your Linux machine.
Step 5: Start the MongoDB service
sudo systemctl start mongod
Explanation: This command starts the MongoDB background service so it's ready to accept connections.
Step 6: Enable MongoDB to start on system boot
sudo systemctl enable mongod
Explanation: This makes sure MongoDB automatically starts every time your system boots.
Step 7: Check MongoDB status
sudo systemctl status mongod
Output:
● mongod.service - MongoDB Database Server Loaded: loaded (/lib/systemd/system/mongod.service; enabled) Active: active (running) since ...
Explanation: You should see that MongoDB is active and running. If you ever need to troubleshoot issues, this is the first check.
Step 8: Access the MongoDB shell
mongosh
Output:
Current Mongosh Log ID: ... Using MongoDB: 7.0.0 test>
Explanation: The mongosh
is MongoDB's interactive shell where you can run commands and interact with your database in real-time.
Intuition Check
Q: What if I close my terminal — will MongoDB stop working?
A: No! Because it's running as a background service managed by systemd
. It's detached from your session.
Uninstall MongoDB (Optional)
If you ever want to uninstall MongoDB completely:
sudo systemctl stop mongod
sudo apt purge mongodb-org*
sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongodb
Explanation: This stops the service, removes all packages, logs, and database files from your system.
Summary
You’ve now successfully installed MongoDB on Linux! From adding the GPG key to starting the service, you’ve prepared your system to use MongoDB both locally and in future development projects.
Next Step
In the next topic, we will explore MongoDB tools like Compass (GUI) and the shell so you can interact with your database visually and through code.