MongoDB Installation Guide

Beginner
⏱️ 12 min read
📚 Updated: Jul 2026
🎯 5 Examples
Setup

What You’ll Learn

This guide walks you through installing MongoDB Community Server on your machine, starting the mongod service, connecting with mongosh, and running your first database commands.

01

Community Server

Free local database.

02

Windows / Mac / Linux

Platform install steps.

03

mongosh shell

Connect and run commands.

04

Verify setup

ping test & first query.

05

Local vs Atlas

Pick the right option.

06

Troubleshooting

Fix common connection errors.

Definition and Usage

MongoDB is a document database. To use it locally you install MongoDB Community Server—the open-source edition that runs the mongod process on your computer. You then connect with mongosh, MongoDB’s modern shell, to create databases, insert documents, and run aggregations.

💡
Beginner tip

Think of mongod as the engine and mongosh as the steering wheel. The server must be running before the shell can connect to mongodb://localhost:27017.

After installation you can follow MongoDB tutorials on CodeToFun—starting with aggregation operators and accumulators—using the same local instance.

🚀 Choose your setup path

OptionBest forInstall effort
Community Server (local)Learning, offline dev, full controlMedium — this guide
MongoDB Atlas (cloud)Quick start, teams, no local serviceLow — free tier at mongodb.com/atlas
DockerCI, isolated versions, containersMedium — docker run mongo

This tutorial focuses on local Community Server installation—the foundation most beginners want before exploring Atlas or Docker.

🧰 System requirements

Before downloading, confirm your machine meets MongoDB’s basics:

64-bit OS Required

Windows 10+, macOS 11+, or a supported Linux distribution (Ubuntu LTS, Debian, RHEL).

RAM 4 GB+

4 GB minimum for learning; 8 GB or more recommended for larger datasets and aggregation labs.

disk space 10 GB+

Leave room for data files, logs, and future indexes under the default dbPath.

port 27017 Default

Ensure nothing else binds to TCP 27017 unless you configure a custom port in mongod.conf.

⚡ Quick Reference

TaskCommand / action
Connect (default)mongosh
Health checkdb.runCommand({ ping: 1 })
Start (Linux)sudo systemctl start mongod
Start (macOS Homebrew)brew services start mongodb-community
Default port27017
Stop shellexit or Ctrl+C twice
Windows
MSI installer
+ MongoDB Service

Official .msi from mongodb.com

macOS
brew tap mongodb/brew
brew install mongodb-community

Homebrew tap method

Ubuntu
apt install
mongodb-org

After adding MongoDB repo

Verify
db.runCommand({ ping: 1 })

Expect ok: 1

Examples Gallery

Install steps for each platform, then verify and run your first commands in mongosh.

💻 Platform installs

Download MongoDB Community Server from the official download page, then follow your OS steps below.

Example 1 — Install on Windows

  1. Download the Windows MSI for MongoDB Community Server.
  2. Run the installer. Choose Complete setup.
  3. Check Install MongoDB as a Service so mongod starts automatically.
  4. Install mongosh when prompted (or install separately from the same download page).
  5. Optional: install MongoDB Compass for a graphical UI.
PowerShell
# Confirm mongosh is on PATH
mongosh --version

# Connect to local server
mongosh

How It Works

The MSI registers a Windows Service named MongoDB. It listens on port 27017 and stores data under C:\Program Files\MongoDB\Server\{version}\data by default.

Example 2 — Install on macOS (Homebrew)

Terminal
brew tap mongodb/brew
brew install mongodb-community

# Start the server as a background service
brew services start mongodb-community

# Connect
mongosh

How It Works

Homebrew installs binaries under its prefix and manages the mongod service with brew services. Data defaults to /opt/homebrew/var/mongodb on Apple Silicon.

Example 3 — Install on Ubuntu / Debian

Import MongoDB’s GPG key and add the official repository (see current docs for your version), then install:

Terminal
sudo apt-get update
sudo apt-get install -y mongodb-org

sudo systemctl start mongod
sudo systemctl enable mongod

sudo systemctl status mongod
mongosh

How It Works

systemctl enable mongod starts MongoDB on boot. Config lives at /etc/mongod.conf; data at /var/lib/mongodb by default.

✅ Verify and use

Confirm the server responds, then create a sample document.

Example 4 — Verify with ping

mongosh
mongosh

db.runCommand({ ping: 1 })

/* Expected:
{ ok: 1 }
*/

How It Works

ping is the simplest health check—it confirms mongod accepted your connection without writing data.

Example 5 — Insert and find your first document

mongosh
use codetofun_demo

db.users.insertOne({
  name: "Alex",
  role: "developer",
  joined: new Date()
})

db.users.findOne({ name: "Alex" })

/* Returns the document you inserted */
*/

How It Works

use switches databases (MongoDB creates it on first write). insertOne adds a BSON document; findOne reads it back—your install is fully working.

🧠 How a local MongoDB install works

1

mongod starts

The server process binds to a port (default 27017) and opens the data directory on disk.

Server
2

mongosh connects

The shell sends wire-protocol commands over TCP to localhost:27017.

Client
3

Commands execute

Inserts, finds, and aggregations run against databases and collections stored as BSON files.

Query
=

Ready for tutorials

Your local instance is ready for operators, accumulators, and aggregation labs on CodeToFun.

📝 Notes

  • Default local installs have no authentication—fine for learning on your machine; enable auth before exposing to a network.
  • Add the MongoDB bin folder to your PATH if mongosh is not recognized after install.
  • On Windows, use Services (services.msc) to restart MongoDB if the shell cannot connect.
  • Check logs when startup fails: Windows Event Viewer, macOS Homebrew logs, Linux /var/log/mongodb/mongod.log.
  • MongoDB Compass is optional—it connects to the same URI as mongosh for visual browsing.
  • Next topic: $add operator (aggregation expressions).

Conclusion

Installing MongoDB locally means running mongod, connecting with mongosh, and confirming { ok: 1 } from a ping command. Pick the installer for your OS, start the service, and run a sample insert.

From here, explore aggregation tutorials—operators and accumulators—on your new instance, or spin up MongoDB Atlas if you prefer a managed cloud database.

💡 Best Practices

✅ Do

  • Install mongosh alongside Community Server for the modern shell experience
  • Run db.runCommand({ ping: 1 }) after every install or upgrade
  • Keep MongoDB updated to a supported LTS release from mongodb.com
  • Use a dedicated data disk or folder with enough free space for indexes
  • Document your connection URI if you switch between local and Atlas

❌ Don’t

  • Expose an unauthenticated mongod to the public internet
  • Delete the data directory while mongod is running—stop the service first
  • Mix very old shell tools (legacy mongo) with new server versions without checking compatibility
  • Skip reading official platform notes for your exact OS version
  • Assume Compass alone installs the database—it is only a client

Key Takeaways

Knowledge Unlocked

Five things to remember about installation

Use this checklist whenever you set up MongoDB on a new machine.

5
Core concepts
💻 02

OS installers

MSI, Homebrew, apt.

Platform
🔌 03

Port 27017

Default endpoint.

Network
04

ping test

Verify connection.

Check
05

Atlas option

Cloud alternative.

Choice

❓ Frequently Asked Questions

Install MongoDB Community Server (the mongod database process) and mongosh (the modern shell). On Windows the official MSI can install both. macOS users often use Homebrew; Linux users add MongoDB's apt repository.
mongod is the database server that stores data and listens on port 27017 by default. mongosh is the client you type commands into—it connects to mongod to run queries and admin tasks.
Open mongosh and run db.runCommand({ ping: 1 }). A response like { ok: 1 } means the server accepted the connection. On Linux you can also run sudo systemctl status mongod.
Atlas is MongoDB's free cloud tier—no local service to manage, great for tutorials and team sharing. Local Community Server is best when you need offline access, custom configs, or to learn how mongod works under the hood.
Default paths vary by OS: on Linux typically /var/lib/mongodb, on macOS Homebrew installs under /opt/homebrew/var/mongodb, on Windows under C:\Program Files\MongoDB\Server\{version}\data. You can change dbPath in mongod.conf.
mongod may not be started, may be bound to a different port, or a firewall may block the connection. Start the service first (Windows Service, brew services, or systemctl) then retry.
Did you know?

The legacy mongo shell was replaced by mongosh, which supports modern JavaScript syntax, improved autocomplete, and the same API for Atlas and local servers. If tutorials mention “the MongoDB shell,” they mean mongosh today. See the official installation docs.

Continue the MongoDB Series

MongoDB is installed—start learning aggregation with the $add operator.

Next: $add →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful