How to Install MongoDB on Ubuntu: Complete Guide (2026)

Complete guide to installing MongoDB on Ubuntu with configuration and security best practices.

Quick Overview

Want to install MongoDB on Ubuntu? This step-by-step guide shows you how to install, configure, and secure MongoDB on Ubuntu 22.04/24.04. Perfect for developers and system administrators building modern applications in 2026. MongoDB’s document-oriented approach makes it ideal for applications requiring flexible schemas, horizontal scalability, and high performance with large volumes of data.

What is MongoDB

MongoDB is a popular open-source NoSQL database that stores data in flexible JSON-like documents called BSON (Binary JSON). Unlike traditional SQL databases that use rigid tables and schemas, MongoDB scales horizontally and handles unstructured data efficiently, making it ideal for modern web applications, real-time analytics, content management systems, and big data processing.

Organizations choose MongoDB when they need to store diverse data types without predefined schemas, scale their database across multiple servers, or work with rapidly changing data structures common in agile development environments. Its ability to handle semi-structured and unstructured data makes it particularly valuable for applications dealing with user-generated content, IoT data, and mobile applications.

Prerequisites

Before installing MongoDB, ensure your system meets these requirements:

  • Ubuntu 22.04 LTS or 24.04 LTS server
  • sudo privileges for administrative tasks
  • Minimum 2GB RAM recommended for production use
  • At least 10GB free disk space for data storage
  • Stable internet connection for downloading packages
  • Basic familiarity with Linux command line

Method 1: Install from Official Repository

Installing from the official MongoDB repository ensures you get the latest stable version with all security updates and feature improvements. This is the recommended method for production environments.

Step 1: Import MongoDB GPG Key

Import the official MongoDB GPG key to verify package authenticity and ensure secure downloads:

wget -qO – https://www.mongodb.org/static/pgp/server-7.0.asc | sudo apt-key add –

This command downloads the GPG key and adds it to your system’s trusted keys, preventing package tampering during installation.

Step 2: Create List File

Add the MongoDB repository to your system’s package sources:

echo “deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse” | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list

This creates a new source list file that points to the official MongoDB repository, allowing your system to find and install MongoDB packages.

Step 3: Update and Install

Update your package lists to include the new repository and install MongoDB:

sudo apt update

sudo apt install -y mongodb-org

The -y flag automatically confirms the installation without prompting. This command installs the MongoDB server, shell, and supporting tools.

Method 2: Install from Ubuntu Repository

For older MongoDB versions available in Ubuntu’s default repositories, which may be suitable for testing or development:

sudo apt update

sudo apt install -y mongodb

Note that Ubuntu repositories may contain older versions than the official MongoDB repository, potentially lacking the latest features and security patches.

Start and Enable MongoDB

After installation, start the MongoDB service and configure it to start automatically on boot:

sudo systemctl start mongod

sudo systemctl enable mongod

Verify the service is running correctly: sudo systemctl status mongod

You should see output indicating the service is active and running. If there are errors, check the logs for details.

Verify Installation

Test that MongoDB is working correctly:

mongosh –eval ‘db.runCommand({ connectionStatus: 1 })’

This connects to the database and checks the connection status, returning information about the server. Alternatively, enter the MongoDB shell with: mongosh

Basic Configuration

Enable Authentication

For production environments, always enable authentication to secure your database against unauthorized access:

Edit the configuration file: sudo nano /etc/mongod.conf

Add the following to the security section:

security:

authorization: enabled

Save the file and restart MongoDB to apply changes: sudo systemctl restart mongod

Create Admin User

After enabling authentication, create an administrative user with full privileges:

Connect to MongoDB: mongosh

Switch to the admin database: use admin

Create the user with:

db.createUser({

user: “admin”,

pwd: “your_secure_password”,

roles: [“root”]

})

Replace “your_secure_password” with a strong, unique password. Exit and reconnect with authentication: mongosh -u admin -p –authenticationDatabase admin

Firewall Configuration

If you have UFW (Uncomplicated Firewall) enabled, allow MongoDB connections:

sudo ufw allow 27017/tcp

sudo ufw reload

Port 27017 is MongoDB’s default port. In production, consider restricting access to specific IP addresses rather than allowing all connections: sudo ufw allow from 192.168.1.0/24 to any port 27017

Basic MongoDB Commands

Once installed, familiarize yourself with these essential commands:

show dbs – List all databases on the server

use mydb – Switch to a database (creates if it doesn’t exist)

db.createCollection(“users”) – Create a new collection

db.users.insertOne({name: “John”, age: 30}) – Insert a document

db.users.find() – Query all documents in a collection

db.users.find({name: “John”}) – Query with a filter

db.users.updateOne({name: “John”}, {$set: {age: 31}}) – Update a document

db.users.deleteOne({name: “John”}) – Delete a document

Troubleshooting

MongoDB Won’t Start

If the service fails to start, check the logs for error messages:

sudo tail -f /var/log/mongodb/mongod.log

Verify port availability: sudo netstat -plnt | grep 27017

Check for port conflicts with other services. Common issues include insufficient disk space, permission problems on data directories, or configuration syntax errors.

Permission Denied

If you encounter permission errors, ensure proper ownership of MongoDB directories:

sudo chown -R mongodb:mongodb /var/lib/mongodb

sudo chown -R mongodb:mongodb /var/log/mongodb

Also check that the MongoDB user has proper permissions on configuration files.

Best Practices

  • Always enable authentication in production environments
  • Regular backups using mongodump command or automated backup solutions
  • Monitor database performance with MongoDB Compass, mongostat, or command-line tools
  • Keep MongoDB updated with security patches
  • Use replica sets for high availability in production
  • Implement proper firewall rules restricting database access
  • Enable access logging to track database operations
  • Use strong, unique passwords for all database users

Uninstall MongoDB

If you need to remove MongoDB from your system:

Stop the service: sudo systemctl stop mongod

Remove packages: sudo apt remove –purge mongodb-org*

Delete data and logs: sudo rm -r /var/log/mongodb /var/lib/mongodb

Remove repository configuration: sudo rm /etc/apt/sources.list.d/mongodb-org-7.0.list

Conclusion

MongoDB on Ubuntu provides a powerful, scalable database solution for modern applications. Whether you are building web applications, IoT platforms, mobile backends, or analytics systems, MongoDB handles diverse data requirements efficiently. With proper configuration, security measures, and maintenance procedures, it serves as a robust foundation for your data storage needs, capable of growing with your application from prototype to production.