Are you looking to install MongoDB on Ubuntu? This tutorial covers a simple, step-by-step process to get MongoDB up and running on an Ubuntu 22.04 or 20.04 server. We will walk through the entire installation process and cover the essential first steps for a basic configuration.
This will give you a properly set-up MongoDB instance, ready for your development projects. We have written this tutorial in a way that it’s followed easily by beginners and experienced alike, and the setup is smooth and error-free.
What is MongoDB?
MongoDB is a powerful and popular NoSQL database. Unlike traditional SQL databases, in which you must store data in tables with rigid rows and columns, like a spreadsheet, MongoDB has taken on a more flexible document-based approach. Think of it as a digital filing cabinet: it has drawers (“collections”) and inside those drawers are files (“documents”) that can hold various types of information in a JSON-like format.
This flexibility makes MongoDB an excellent choice for modern applications where data structures can evolve, such as content management systems, e-commerce platforms, and real-time analytics. It allows developers to store complex data naturally without the constraint of a predefined schema.
Installing MongoDB on Ubuntu
First, you need to add the official MongoDB repository to your system. This ensures you install a trusted and up-to-date version of the database. Your system needs to trust the source of the software. This command imports the key that verifies the authenticity of the MongoDB packages.
wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add -Next, you must tell your package manager where to find the MongoDB packages. The command differs slightly based on your Ubuntu version.
For Ubuntu 22.04:
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.listFor Ubuntu 20.04
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.listOnce package manager are configured for MongoDB repository, you just need to update package index and install MongoDB. Below command will check for update into packages and start downloading MongoDB in your Ubuntu machine:
sudo apt update
sudo apt install -y mongodb-orgCommand will install the complete MongoDB package, including the database server, shell, and other useful tools.
Starting and Managing the MongoDB Service
After the installation, the MongoDB service will not start automatically. You need to start it and ensure it launches every time you reboot the server.
Start the MongoDB Service
Use the following command to start the MongoDB server.
sudo systemctl start mongodVerify the Service is Running
You can check that the service started correctly by checking its status.
sudo systemctl status mongodLook for an active (running) status in the output to confirm everything is working like below image.

Once verified, you can enable MongoDB on boot using below command. It will make sure MongoDB services starts automatically when you restart your system or boot up.
sudo systemctl enable mongodYour MongoDB Ubuntu setup is now complete and running with its default configuration. You can connect to it locally using the MongoDB Shell by simply typing mongosh.
Next Step: Secure Your Database
Now that MongoDB is up and running, the next important thing you’ll need to do is secure it. By default, MongoDB does not enable authentication, which means that anyone who can reach your server can change your data. This is a significant security risk for any application.
Setting up an administrative user with full privileges will protect your data. The next tutorial in this series, How to Create an Admin User in MongoDB, will walk you through this important process that allows you to enable authentication and thus secure your database.
Conclusion
You’ve gone through the process to install MongoDB on Ubuntu. Your NoSQL database is installed, running, and enabled to start on boot. You have a good foundation for future projects. The next step would be to secure your instance and begin exploring the powerful features MongoDB has to offer.

