How to Install MongoDB on Linux: Step-by-Step Guide
To install
MongoDB on Linux, add the official MongoDB repository, update your package list, and then install the mongodb-org package using your package manager. Finally, start the mongod service to run the database.Syntax
These are the main commands to install MongoDB on Ubuntu or Debian Linux:
wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add -: Adds MongoDB's public key for package verification.echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list: Adds the MongoDB repository to your system.sudo apt-get update: Updates the package list to include MongoDB packages.sudo apt-get install -y mongodb-org: Installs MongoDB packages.sudo systemctl start mongod: Starts the MongoDB service.sudo systemctl enable mongod: Enables MongoDB to start on boot.
bash
wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add - echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list sudo apt-get update sudo apt-get install -y mongodb-org sudo systemctl start mongod sudo systemctl enable mongod
Example
This example shows how to install MongoDB 6.0 on Ubuntu 20.04. It adds the MongoDB repository, installs the database, and starts the service.
bash
wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add - echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list sudo apt-get update sudo apt-get install -y mongodb-org sudo systemctl start mongod sudo systemctl status mongod
Output
● mongod.service - MongoDB Database Server
Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2024-06-20 10:00:00 UTC; 10s ago
Docs: https://docs.mongodb.org/manual
Main PID: 12345 (mongod)
Tasks: 23 (limit: 4915)
Memory: 50.0M
CGroup: /system.slice/mongod.service
└─12345 /usr/bin/mongod --config /etc/mongod.conf
Common Pitfalls
Common mistakes when installing MongoDB on Linux include:
- Not adding the MongoDB repository before installing, which causes package not found errors.
- Skipping
sudo apt-get updateafter adding the repo, so the system doesn't see the new packages. - Not starting the
mongodservice after installation, so MongoDB won't run. - Using incorrect repository URLs for your Linux version.
Always check your Linux distribution version and use the matching MongoDB repo URL.
bash
sudo apt-get install -y mongodb-org # This will fail if repo is not added or updated # Correct way: wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add - echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list sudo apt-get update sudo apt-get install -y mongodb-org
Quick Reference
Summary tips for installing MongoDB on Linux:
- Always add the official MongoDB repository before installing.
- Run
sudo apt-get updateafter adding the repo. - Use
sudo systemctl start mongodto start MongoDB. - Enable MongoDB to start on boot with
sudo systemctl enable mongod. - Check MongoDB service status with
sudo systemctl status mongod.
Key Takeaways
Add the official MongoDB repository before installing to get the latest version.
Always run 'sudo apt-get update' after adding new repositories.
Start and enable the mongod service to run MongoDB properly.
Use the correct repository URL matching your Linux distribution version.
Check the MongoDB service status to confirm it is running.