Install MongoDB Community Edition
Install MongoDB Community Edition on your machine in this hands-on MongoDB tutorial. Step-by-step guidance, troubleshooting tips, and what to learn next.
Focus: install mongodb community edition on your machine
You found the right tutorial: MongoDB is brilliant, but the installation process can feel like a minefield if you don't know where to start. How many times have you followed a blog post, only to hit a dead end because of a minor version mismatch or a missing repository? This lesson flips that script by guiding you through a clean, predictable install of MongoDB Community Edition on your own machine — no more guesswork, just a solid foundation for the rest of your MongoDB journey.
The problem this lesson solves
Manually downloading a MongoDB tarball, running binaries from random directories, and praying that mongod starts is a recipe for wasted hours and confusing configuration errors. Without a systematic install process, you'll struggle with:
- Inconsistent versions: You might end up with a dev version that behaves differently from production.
- Missing service management: On Linux, if MongoDB isn't registered as a service, it won't auto-start after a reboot.
- Path issues: Running
mongodfrom a different directory can cause data and log paths to be misinterpreted.
This lesson installs MongoDB Community Edition (the free, open-source version) using your operating system's native package manager. We'll cover Ubuntu (or Debian-based), macOS (Homebrew), and Windows (MSI installer), so you have a working mongod and mongosh command in your terminal before moving on.
Core concept / mental model
Think of installing MongoDB like setting up a local database server — it's a daemon that listens on a port (default 27017) and responds to queries. The core pieces are:
mongod: The database service (daemon). It handles data storage and query processing.mongosh: The modern shell client that lets you interact with the server.mongod.conf: The configuration file that defines data directories, log paths, and network bindings.
Mental model: MongoDB is like a post office that stores parcels (documents).
mongodis the post office building,mongoshis the postal clerk's window, and the data directory is the sorting room. Until the building is installed and opened, you can't send or receive anything.
How it works step by step
Before running commands, you need to understand the logical flow:
- Add the official MongoDB repository to your package manager. This ensures you get the
mongodb-orgpackage directly from MongoDB, not an outdated or mismatched version from your OS's default repos. - Install the MongoDB packages (
mongodb-orgincludes the server, shell, and tools). - Start and enable the MongoDB service so it runs now and on boot.
- Verify the install by connecting with
mongosh.
For macOS with Homebrew, the process is simpler: tap the MongoDB formulas, install, and run the service via brew services.
For Windows, the MSI installer handles everything graphically, but you still need to configure the service and optionally the path.
Hands-on walkthrough
Install on Ubuntu 22.04 (or Debian 11+)
First, update your package index and install required dependencies:
sudo apt update
sudo apt install -y gnupg curl
Next, import the MongoDB public GPG key and add the official repository. Replace jammy with your Ubuntu codename (e.g., focal for 20.04):
curl -fsSL https://pgp.mongodb.com/server-7.0.asc | \
sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg \
--dearmor
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
Now update again and install MongoDB:
sudo apt update
sudo apt install -y mongodb-org
Start the service and enable it to launch on boot:
sudo systemctl start mongod
sudo systemctl enable mongod
Verify the service status:
sudo systemctl status mongod
You should see active (running). Now connect with the shell:
mongosh
Expected output: Current Mongosh Log ID: ... and a prompt like test> — you're in!
Install on macOS with Homebrew
If you don't have Homebrew, install it first: /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Then tap the MongoDB formulas and install:
brew tap mongodb/brew
brew install mongodb-community@7.0
Run MongoDB as a service (auto-starts at login):
brew services start mongodb-community@7.0
Wait a few seconds, then verify:
mongosh
If the service fails, check the log at /usr/local/var/log/mongodb/mongo.log (Intel) or /opt/homebrew/var/log/mongodb/mongo.log (Apple Silicon).
Install on Windows via MSI
- Download the MSI installer from the MongoDB Community Download Center.
- Run the installer — leave the default settings, but make sure you check Install MongoDB Compass if you want a GUI.
- Choose Install MongoDB as a Service and Run service as Network Service.
- After installation, MongoDB runs automatically. To connect, open a command prompt and type
mongosh.
Note: The MSI adds the
bindirectory to PATH, so you can runmongoshfrom any folder. If it doesn't work, restart your terminal or addC:\Program Files\MongoDB\Server\7.0\binto PATH manually.
Compare options / when to choose what
| Method | Pros | Cons | Best for |
|---|---|---|---|
| OS package manager (Linux) | Native startup, auto-updates via apt, automatic service management |
Requires sudo, the repo setup can be tricky | Production-like local dev on Linux, server admin |
| Homebrew (macOS) | Simple, one-line install, easy service control | Slightly behind official release, may require tap | Mac users who want quick setup |
| MSI installer (Windows) | GUI, includes service setup, no CLI knowledge needed | Silent installs are trickier, less control | Windows beginners / GUI lovers |
| Docker container | Isolated, reproducible, no OS-specific steps | Extra overhead, networking nuances | Teams, microservices, clean slate testing |
If you're on Linux and will use MongoDB daily, use apt. If you're on macOS and want the simplest path, Homebrew wins. Windows users have no better option than the MSI. Docker is excellent for CI or when you want zero footprint on your host—but for this track, native installs are the focus.
Troubleshooting & edge cases
mongod: command not found
- Cause: The
bindirectory isn't in your PATH, or the install didn't complete. - Fix: On Linux, reinstall with
sudo apt install -y mongodb-organd check/usr/bin/mongod. On macOS, ensure/opt/homebrew/binis in your shell PATH. On Windows, add thebinpath manually.
Failed to start mongod.service: Unit mongod.service not found
- Cause: The service file wasn't created because the
mongodb-org-serverpackage didn't install properly. - Fix: Reinstall with
sudo apt install -y mongodb-org-serverand then start the service.
mongosh: error: connect ECONNREFUSED 127.0.0.1:27017
- Cause: The MongoDB server isn't running.
- Fix: Start it manually:
sudo systemctl start mongod(Linux) orbrew services start mongodb-community@7.0(macOS). For Windows, check Services and startMongoDB Server.
Data directory permission issues on Linux
- Cause: The default data dir
/data/dbis owned by root, butmongodruns asmongodbuser. - Fix: Use the default directories from the package (they're already set up). If you're using a manual tar install, run
sudo mkdir -p /data/db && sudo chown -Rid -u/data/db.
Version mismatch errors
- Cause: You installed a different version than the repo provides.
- Fix: Always use the same major version for
mongodandmongosh. If you need a specific minor, pin the version on install (e.g.,mongodb-org=7.0.14).
What you learned & what's next
You've successfully installed MongoDB Community Edition on your machine, configured it as a background service, and verified it by connecting with mongosh. You now understand the core components (mongod, mongosh) and can start the server whenever you need it.
What’s next: With a running MongoDB, you're ready to dive into creating databases and collections. In the next lesson, you'll learn how to perform basic CRUD operations — inserting, querying, updating, and deleting documents — using mongosh. Those commands will become your daily toolkit for interacting with MongoDB.
Remember: The installation is only the start. The real magic happens when you begin storing and retrieving data!
Practice recap
Now that MongoDB is running, test your setup by creating a quick database: in mongosh, run use practice and then db.items.insertOne({name: 'install test'}). Then query it with db.items.find(). If you see the document, you're ready for the next lesson on CRUD operations!
Common mistakes
- Skipping the official repository update on Linux, leading to an outdated MongoDB version from the distro's default repos — always use MongoDB's own repo.
- Starting
mongodmanually and then rebooting, only to find the service isn't running — remember to enable the service withsystemctl enableorbrew services start. - Mixing major versions between
mongodandmongosh— this clearly causes connection or command errors; stick to the same version family (e.g., 7.0). - Forgetting to install the
mongoshshell separately on some systems — on Linux, themongodb-orgpackage includes it, but on minimal installations you may needmongodb-mongoshexplicitly.
Variations
- Docker: Run MongoDB in a container with
docker run -d -p 27017:27017 mongo:7.0— useful for isolation and quick teardown. - Manual tarball: Download the official
.tgzfrom MongoDB and runmongoddirectly — gives you complete control over paths and versions, but requires more manual setup. - Chocolatey (Windows): Use
choco install mongodbfor a command-line install — a scripting-friendly alternative to the MSI GUI.
Real-world use cases
- Local development of a Node.js or Python app that stores user profiles in MongoDB — a native install lets you replicate production behavior without external services.
- Running MongoDB on a Linux staging server to test backup and restore scripts before a production rollout.
- Teaching a MongoDB course or workshop — a standard install on every student's laptop ensures consistent setup and fewer surprises.
Key takeaways
- MongoDB Community Edition is free and can be installed via your OS package manager or Homebrew, not just the GUI installer.
- Use
mongodfor the database service andmongoshfor the client shell — they're separate tools that work together. - Always add MongoDB's official repository to get the latest stable version, not an outdated distro version.
- Start and enable the MongoDB service so it runs automatically —
systemctl enable mongodorbrew services start. - Connectivity issues (ECONNREFUSED) usually mean the server isn't running — check the service status first.
- The install process prepares you for the next step: performing CRUD operations with
mongosh.
Keep learning
Related tutorials, quizzes, and articles for this topic.
Discussion
Questions, corrections, and tips help everyone reading this page.
0 comments
Add a comment
No comments yet — start the thread.