⬅ Previous Topic
Installing MongoDB on WindowsNext Topic ⮕
Installing MongoDB on Mac⬅ Previous Topic
Installing MongoDB on WindowsNext Topic ⮕
Installing MongoDB on MacMongoDB 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.
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.
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
.
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.
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.
sudo systemctl start mongod
Explanation: This command starts the MongoDB background service so it's ready to accept connections.
sudo systemctl enable mongod
Explanation: This makes sure MongoDB automatically starts every time your system boots.
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.
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.
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.
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.
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.
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.
⬅ Previous Topic
Installing MongoDB on WindowsNext Topic ⮕
Installing MongoDB on MacYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.