How to Install and Run MongoDB on Ubuntu 22.04
If you want to use a fast, stable, and scalable database on your server or VPS, MongoDB is one of the best choices. In this tutorial, we are going to install, run, and secure MongoDB on Ubuntu 22.04 step by step and without complications; exactly what I have done many times on real servers.

Step 1: System Update
Before installing any important service on Linux, the first and most important thing is to update the system. This will prevent many strange errors and future security problems.
apt update && apt upgrade -y

This command will update the package list and upgrade all installed packages to the latest version.
Next, we need to install the libssl library, because MongoDB requires it.
wget http://archive.ubuntu.com/ubuntu/pool/main/o/openssl/libssl1.1_1.1.0g-2ubuntu4_amd64.deb dpkg -i libssl1.1_1.1.0g-2ubuntu4_amd64.deb
Step 2: Add the MongoDB GPG Key
MongoDB uses a GPG key to verify the integrity of its packages. We add this key to the system with the following command:
curl -fsSL https://www.mongodb.org/static/pgp/server-4.4.asc | gpg -o /usr/share/keyrings/mongodb-server-4.4.gpg --dearmor
If everything is done correctly, the key has been added successfully, and we can move on to the next step.
Step 3: Add MongoDB Repository
Now we need to add the official MongoDB repository to the system sources to enable its installation.
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-4.4.gpg ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | tee /etc/apt/sources.list.d/mongodb-org-4.0.list
This will introduce the MongoDB packages to the system.
Step 4: Installing MongoDB
After adding the repository, it’s time to install MongoDB.
apt update apt install mongodb-org
After installation, to make sure MongoDB is installed correctly, we check its version:
mongo --version
Step 5: Start MongoDB and enable it at boot time
After installation, the MongoDB service does not start automatically. We need to start it manually and configure it to start after a reboot.
systemctl start mongod systemctl enable mongod
To check the status of the service:
systemctl status mongod
If the status is active (running), then MongoDB is running without any problems.
Step 6: Securing MongoDB
By default, MongoDB does not have authentication and anyone can connect to the database. For a real-world environment and server, this is not acceptable at all.
First, open the MongoDB configuration file:
nano /etc/mongod.conf
Find the security section and add these lines:
authorization: "enabled"
After saving the file, restart MongoDB:
systemctl restart mongod
Now, enter the MongoDB shell:
mongo
Switch to the admin database:
use admin
And create an administrative user:
db.createUser({
user: "admin",
pwd: "strongpassword",
roles: [{ role: "userAdminAnyDatabase", db: "admin" }]
})
Make sure to use a strong password instead of strongpassword.
Finally, we exit the shell:
exit
Step 7: Checking MongoDB Connection and Authentication
For the final test, we will use the mongosh tool. If it is not installed, install it first:
apt install mongodb-mongosh
Now, using the username and password we created, we will connect to MongoDB:
mongosh --host localhost --port 27017 -u admin -p --authenticationDatabase admin
After entering the password, if you are logged into the mongosh environment, it means:
- Authentication is properly enabled
- MongoDB is running healthy and secure
- The server is ready to be used in real projects
My personal experience
In almost all the servers where I installed MongoDB, the biggest problem was that authentication was not enabled or the system was not updated. If you follow these few steps carefully, MongoDB will run on Ubuntu 22.04 completely stable and without any problems.
Conclusion
In this tutorial, we’ve walked you through installing MongoDB on Ubuntu 22.04, running the service, and most importantly, getting it ready for real-world use from a security perspective, by updating your system, adding the official repository, enabling the service, and also configuring authentication, you now have a stable and reliable database that can serve as the foundation for serious projects, my experience has shown that most MongoDB problems start not with the database itself, but with hasty installation and carelessness about security. If you’ve followed these steps carefully, you can now safely connect MongoDB to your applications, create databases, define users, and even think about scalability and backups. From here on out, MongoDB is no longer just installed, it’s ready for professional use, for more professional tips and tricks, check out our website.
Yes, MongoDB is completely stable on Ubuntu 22.04 and is used for both live and production environments.
If you have enabled the service, MongoDB will start automatically after every reboot.