Section 2: Installation

🐧 MongoDB Linux Installation

Complete step-by-step guide to install MongoDB Server on Ubuntu, Debian, RHEL, CentOS, and Fedora

5+ Linux Distributions
10min Installation Time
100% Free & Open Source
7.0+ Latest Version
πŸ“–

Installation Overview

What you need to know

πŸ’‘ About This Guide

This guide covers MongoDB Community Edition installation on popular Linux distributions using official MongoDB repositories. We'll use package managers (apt/yum/dnf) for easy installation and automatic updates.

⚠️ Important Notes
  • You need sudo/root privileges for installation
  • MongoDB requires 64-bit Linux systems
  • Minimum 2 GB RAM recommended (4 GB+ for production)
  • Default data directory: /var/lib/mongodb
  • Default config file: /etc/mongod.conf
  • Default port: 27017
βœ…

System Requirements

Supported distributions

  • Ubuntu: 20.04 LTS, 22.04 LTS, 24.04 LTS
  • Debian: 10 (Buster), 11 (Bullseye), 12 (Bookworm)
  • RHEL/CentOS: 7, 8, 9
  • Fedora: 37, 38, 39
  • Architecture: x86_64 (64-bit)
  • RAM: Minimum 2 GB (4 GB+ recommended)
  • Disk: 5 GB free space minimum
🟠

Ubuntu / Debian Installation

Using apt package manager

1
Import MongoDB GPG Key

Import the public key used by the package management system to verify package authenticity.

Terminal
$ curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | sudo gpg --dearmor -o /usr/share/keyrings/mongodb-server-7.0.gpg
πŸ’‘ What this does Downloads MongoDB's GPG key and stores it in the system keyring to verify package signatures.
2
Add MongoDB Repository

Create a list file for MongoDB in your sources list directory.

For Ubuntu 22.04 (Jammy):

Terminal
$ 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

For Ubuntu 20.04 (Focal):

Terminal
$ echo "deb [ arch=amd64,arm64 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

For Debian 12 (Bookworm):

Terminal
$ echo "deb [ signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] http://repo.mongodb.org/apt/debian bookworm/mongodb-org/7.0 main" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
⚠️ Choose the Correct Version! Replace "jammy", "focal", or "bookworm" with your distribution's codename. Check with: lsb_release -a
3
Update Package Database

Reload the local package database to include MongoDB packages.

Terminal
$ sudo apt-get update
4
Install MongoDB Packages

Install the latest stable version of MongoDB Community Edition.

Terminal
$ sudo apt-get install -y mongodb-org
Reading package lists... Done Building dependency tree... Done The following NEW packages will be installed: mongodb-database-tools mongodb-mongosh mongodb-org mongodb-org-database mongodb-org-mongos mongodb-org-server mongodb-org-shell mongodb-org-tools ... Setting up mongodb-org (7.0.x) ...
βœ… Installation Complete! MongoDB is now installed. The packages include: mongod (server), mongos (sharding router), mongo/mongosh (shell), and various tools.
5
Start MongoDB Service

Start the MongoDB service and enable it to start on boot.

Terminal
# Start MongoDB $ sudo systemctl start mongod # Enable MongoDB to start on boot $ sudo systemctl enable mongod
Created symlink /etc/systemd/system/multi-user.target.wants/mongod.service
# Check status $ sudo systemctl status mongod
● mongod.service - MongoDB Database Server Loaded: loaded (/lib/systemd/system/mongod.service; enabled) Active: active (running) since Mon 2024-12-14 10:30:45 UTC Docs: https://docs.mongodb.org/manual Main PID: 12345 (mongod) Memory: 65.2M CPU: 2.456s
πŸ”΄

RHEL / CentOS / Fedora Installation

Using yum or dnf package manager

1
Create MongoDB Repository File

Create a .repo file to install MongoDB with yum or dnf.

For RHEL 9 / Rocky Linux 9 / AlmaLinux 9:

Terminal
$ sudo tee /etc/yum.repos.d/mongodb-org-7.0.repo << EOF [mongodb-org-7.0] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/9/mongodb-org/7.0/x86_64/ gpgcheck=1 enabled=1 gpgkey=https://www.mongodb.org/static/pgp/server-7.0.asc EOF

For RHEL 8 / CentOS 8:

Terminal
$ sudo tee /etc/yum.repos.d/mongodb-org-7.0.repo << EOF [mongodb-org-7.0] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/8/mongodb-org/7.0/x86_64/ gpgcheck=1 enabled=1 gpgkey=https://www.mongodb.org/static/pgp/server-7.0.asc EOF
2
Install MongoDB Packages

Install MongoDB using yum (RHEL 7/8) or dnf (RHEL 9/Fedora).

Using yum (RHEL 7/8):

Terminal
$ sudo yum install -y mongodb-org

Using dnf (RHEL 9/Fedora):

Terminal
$ sudo dnf install -y mongodb-org
3
Start MongoDB Service

Start and enable MongoDB to run on system boot.

Terminal
# Start MongoDB $ sudo systemctl start mongod # Enable on boot $ sudo systemctl enable mongod # Verify status $ sudo systemctl status mongod
βœ“

Verify Installation

Confirm MongoDB is working

Connect to MongoDB

Use the MongoDB Shell (mongosh) to connect and verify.

MongoDB Shell
$ mongosh
Current Mongosh Log ID: 657b3a4c9e8a7b2c3d4e5f6a Connecting to: mongodb://127.0.0.1:27017 Using MongoDB: 7.0.5 Using Mongosh: 2.1.1 For mongosh info see: https://docs.mongodb.com/mongodb-shell/ test>
# Check MongoDB version test> db.version()
7.0.5
# Show databases test> show dbs
admin 40.00 KiB config 12.00 KiB local 40.00 KiB
# Test insert test> db.test.insertOne({ name: "MongoDB", status: "running" })
{ acknowledged: true, insertedId: ObjectId('657b3a4c9e8a7b2c3d4e5f6b') }
# Exit test> exit
βœ… MongoDB is Working! If you can connect and run commands, MongoDB is installed correctly and running.
Check Service Status

Verify MongoDB is running as a system service.

Terminal
# Check if MongoDB is running $ sudo systemctl is-active mongod
active
# Check if enabled on boot $ sudo systemctl is-enabled mongod
enabled
# View MongoDB logs $ sudo tail -f /var/log/mongodb/mongod.log
βš™οΈ

Basic Configuration

Essential settings

Configuration File

MongoDB's configuration file is located at /etc/mongod.conf

/etc/mongod.conf
# Storage settings storage: dbPath: /var/lib/mongodb journal: enabled: true # Network settings net: port: 27017 bindIp: 127.0.0.1 # Change to 0.0.0.0 for remote access # Process management processManagement: timeZoneInfo: /usr/share/zoneinfo # Security (uncomment to enable) #security: # authorization: enabled
⚠️ After Editing Config Always restart MongoDB after configuration changes:
$ sudo systemctl restart mongod
Useful Management Commands
Service Management
# Start MongoDB $ sudo systemctl start mongod # Stop MongoDB $ sudo systemctl stop mongod # Restart MongoDB $ sudo systemctl restart mongod # Check status $ sudo systemctl status mongod # Enable on boot $ sudo systemctl enable mongod # Disable on boot $ sudo systemctl disable mongod # View logs $ sudo journalctl -u mongod $ sudo tail -f /var/log/mongodb/mongod.log
πŸ”§

Troubleshooting

Common issues and solutions

MongoDB Won't Start

Symptoms: systemctl start mongod fails

Solutions:

  1. Check logs: sudo journalctl -xe -u mongod
  2. Verify data directory permissions:
    $ sudo chown -R mongodb:mongodb /var/lib/mongodb $ sudo chown mongodb:mongodb /tmp/mongodb-27017.sock
  3. Check if port 27017 is already in use: sudo lsof -i :27017
  4. Verify config file syntax: mongod --config /etc/mongod.conf --test
Cannot Connect to MongoDB

Symptoms: mongosh connection fails

Solutions:

  1. Verify MongoDB is running: sudo systemctl status mongod
  2. Check firewall:
    # Ubuntu/Debian (ufw) $ sudo ufw allow 27017 # RHEL/CentOS (firewalld) $ sudo firewall-cmd --permanent --add-port=27017/tcp $ sudo firewall-cmd --reload
  3. Check bindIp in /etc/mongod.conf
Package Not Found

Symptoms: "Package mongodb-org not found"

Solutions:

  1. Verify repository was added correctly
  2. Update package database:
    # Ubuntu/Debian $ sudo apt-get update # RHEL/CentOS $ sudo yum clean all && sudo yum update
  3. Check internet connectivity
  4. Verify correct distribution codename in repository URL
πŸŽ‰ Installation Complete!

Congratulations! MongoDB is now running on your Linux system. Here's what to do next:

  • Secure MongoDB: Enable authentication and create admin user
  • Install MongoDB Compass: Get the GUI tool for easier management
  • Learn mongosh: Practice MongoDB shell commands
  • Create Databases: Start building your applications
  • Set Up Backups: Configure regular backups with mongodump
  • Monitor Performance: Use built-in monitoring tools