Ubuntek Banner

Install MongoDB on Ubuntu

Databases Intermediate 20 minutes

Summary

MongoDB is a popular NoSQL database that stores data in flexible, JSON-like documents. This tutorial will help you install the latest MongoDB Community Edition on Ubuntu, verify the installation, and start using the MongoDB shell.

Key Vocabulary

MongoDB: A document-oriented NoSQL database.
NoSQL: A database that stores data in non-tabular formats, often as documents or key-value pairs.
mongod: The MongoDB server process.
mongosh: The MongoDB shell for interacting with databases.
Collection: A group of MongoDB documents, similar to a table in SQL.

Prerequisites

  • Ubuntu 20.04 or later
  • Sudo/root access
  • Internet connection

Step-by-Step Instructions

Step 1

Import the MongoDB GPG Key

Add MongoDB's official GPG key to verify package authenticity.

curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | sudo gpg --dearmor -o /usr/share/keyrings/mongodb-server-7.0.gpg
Why this works: This ensures that packages are from the official MongoDB source.
Step 2

Add the MongoDB Repository

Add the official MongoDB repository to your system.

echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu $(lsb_release -cs)/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
Why this works: This allows apt to find and install MongoDB packages.
Step 3

Update Package Lists

Refresh your package lists to include the new MongoDB repository.

sudo apt update
Why this works: Ensures apt knows about the latest MongoDB packages.
Step 4

Install MongoDB

Install the MongoDB server and tools.

sudo apt install mongodb-org -y
Why this works: Installs the MongoDB server, shell, and related tools.
Step 5

Start MongoDB Service

Start the MongoDB service.

sudo systemctl start mongod
Why this works: Starts the MongoDB server process.
Step 6

Enable MongoDB at Boot

Enable MongoDB to launch automatically at system boot.

sudo systemctl enable mongod
Why this works: Ensures MongoDB will start automatically after reboot.
Step 7

Check MongoDB Status

Verify that MongoDB is running.

sudo systemctl status mongod
Why this works: You should see 'active (running)' in the output.
Step 8

Access the MongoDB Shell

Open the MongoDB shell to interact with your database.

mongosh
Why this works: You should see a prompt like 'test>'. Type 'exit' to leave the shell.
Step 9

Check MongoDB Version

Check the installed MongoDB version inside the shell.

db.version()
Why this works: This command shows the MongoDB server version.

Common Issues & Solutions

Problem: mongod: command not found
Solution: MongoDB may not be installed or not in your PATH. Try reinstalling or check your PATH.
Problem: Failed to start mongod.service
Solution: Check logs with 'sudo journalctl -u mongod'. Common issues: port 27017 in use, permission problems, or config file errors.
Problem: Connection refused to 127.0.0.1:27017
Solution: MongoDB isn't running. Start it with 'sudo systemctl start mongod'.

Conclusion

Congratulations! You have successfully installed MongoDB on Ubuntu. You can now create databases, collections, and documents for your applications. For advanced usage, explore MongoDB's security, backup, and performance features.