How to Install MongoDB on Ubuntu in 2025
- HolyHosting

- Oct 30
- 5 min read
MongoDB is one of the most popular open-source NoSQL database management systems. It stores data in a document format rather than in a tabular structure. Since it doesn’t have a rigid schema, MongoDB offers greater scalability and flexibility compared to SQL.
This database management system (DBMS) is popular among developers due to its performance and compatibility with multiple programming languages. However, installing MongoDB on Ubuntu requires running several commands, making the process challenging.
With this in mind, we will explain how to install MongoDB on a virtual private server (VPS) running Ubuntu. We will also cover the steps to create a new database, set up a user, and enable remote authentication.

What is MongoDB?
MongoDB is one of the most widely used open-source NoSQL database management systems. It is commonly used for large-scale applications or websites.
Unlike SQL, MongoDB stores data in BSON documents with a flexible schema, resulting in higher scalability. It also supports multiple operating systems, programming languages, and frameworks.
Prerequisites for Installing MongoDB on Ubuntu
Before installing the official MongoDB package, check your hardware and software compatibility. Your VPS hosting plan should support Ubuntu, provide SSH access, and allow full root privileges.
HolyHosting VPS offers several Linux distributions, including Ubuntu. You can install it easily by going to VPS Panel → Operating System and Panel.
We recommend using the latest operating system to ensure compatibility and security. However, the steps to install MongoDB on Ubuntu 20.04 or other versions are similar.
Our VPS hosting plans also allow remote SSH connections. In addition to using applications like PuTTY or Terminal, you can run Linux commands directly from your web browser using our Browser Terminal.
HolyHosting VPS also provides full root access, allowing users to execute MongoDB installation commands without permission issues.
How to Install MongoDB on Ubuntu
In this section, we will explain how to install MongoDB on Ubuntu 20.04 or other versions.
Before proceeding, make sure you are connected to your VPS via SSH using a root or superuser account.
1. Install MongoDB
Before installing the MongoDB package, download GnuPG and the cURL utility by running the following command in your command-line interface:
sudo apt-get install -y gnupg curl
Use cURL and GnuPG to import MongoDB’s GPG public key and obtain the installation package:
curl -fsSL https://pgp.mongodb.com/server-7.0.asc | \
sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg \
--dearmor
Note that the URL may vary depending on the MongoDB package. In this tutorial, we will install MongoDB Community Edition 7.0, the latest stable version at the time of writing.
After importing the official MongoDB packages, create a list file for installation. The command differs depending on the Ubuntu version. For Ubuntu 22.04 or later, run:
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, run:
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
Update the APT repository to synchronize the local package database:
sudo apt-get update
Run the following command to install the latest stable MongoDB version using APT:
sudo apt-get install -y mongodb-org
Optionally, you can choose a specific MongoDB package version. For example, to install MongoDB 7.0.2, run:
sudo apt-get install -y mongodb-org=7.0.2 mongodb-org-database=7.0.2 mongodb-org-server=7.0.2 mongodb-mongosh=7.0.2 mongodb-org-mongos=7.0.2 mongodb-org-tools=7.0.2
If you specify only the MongoDB version and not the other components, APT will install the newest package.
Since APT automatically updates component packages, lock the installation to keep the current version:
echo "mongodb-org hold" | sudo dpkg --set-selections
echo "mongodb-org-server hold" | sudo dpkg --set-selections
echo "mongodb-mongosh hold" | sudo dpkg --set-selections
Pro Tip: If you don’t import the GPG public key into your local package database, APT will install an unofficial MongoDB package. This may cause compatibility, security, and legal issues, as well as lack of support.
2. Start the MongoDB Service
After installation, start MongoDB Community Edition with:
sudo systemctl start mongod
The systemctl command is essential for managing the MongoDB service. If you encounter an error, reload services and try starting the DBMS again:
sudo systemctl daemon-reload
To check if the MongoDB server started successfully, verify its status:
sudo systemctl status mongod
You should see "active (running)" if the service is running correctly.
By default, the service doesn’t start at boot. To enable automatic startup:
sudo systemctl enable mongod
The main MongoDB daemon, called mongod, handles data requests, manages access, and runs background management operations.
3. Configure MongoDB
Edit MongoDB’s main configuration file located at /etc/mongod.conf, created automatically during installation:
sudo nano /etc/mongod.conf
The configuration file includes several options that define server behavior. For example, systemLog defines logging settings, while net handles network configurations.
Warning: Be careful editing mongod.conf, as incorrect changes can break the service or expose your server to security risks.
Use # to comment out parameters instead of deleting them. After editing, restart MongoDB to apply changes:
sudo systemctl restart mongod
4. Create a New Database
The installation automatically creates the admin database. Since it is used for administrative purposes, it’s recommended to create a new database:
mongosh
Use the use command to create or access a database:
use customers
List all databases:
show dbs
5. Create a New User
MongoDB doesn’t have a default account. Create a user for each database and set privileges using db.createUser().
For example, create a root user with full permissions:
use admin
db.createUser(
{
user: "root",
pwd: "$tr0ngPa$$w0rD",
roles: [ { role: "root", db: "admin" } ]
}
)
Show all users in the current database:
show users
Test database connection:
mongosh --port 27017 -u root -p '$tr0ngPa$$w0rD' 'admin'
6. Enable Remote Authentication
By default, MongoDB allows logins only from the host machine. To access MongoDB from another system, modify the bindIP parameter in mongod.conf:
bindIP: 127.0.0.1,123.123.12.1
Enable authentication under the security section:
authorization: enabled
Restart MongoDB:
sudo systemctl restart mongod
Connect remotely using:
mongo "mongodb://root:$tr0ngPa$$w0rD@123.123.1.1:27017/?authSource=admin"
For added security, restrict MongoDB’s listening port to your local IP using UFW:
sudo ufw allow from your_local_ip to any port 27017
This ensures the firewall only accepts connections from the specified IP.
Conclusion
MongoDB is a popular free, open-source NoSQL database system for large-scale websites and applications. Its flexible schema makes it more scalable than SQL.
This guide summarized the MongoDB setup on Ubuntu:
Install MongoDB: Import the official repository key with GnuPG and cURL, create the list file, and install via APT.
Start MongoDB: Launch the service with systemctl and enable it for automatic startup.
Configure MongoDB: Edit mongod.conf and restart the service.
Create a New Database: Use mongosh and the use command.
Create a New User: Add users with db.createUser(), setting passwords and roles.
Enable Remote Authentication: Adjust bindIP in mongod.conf, enable authentication, and restrict access via UFW.
Use a VPS with full root access and wide software compatibility, like HolyHosting, to ensure a smooth MongoDB setup. It also provides one-click OS installation and a browser-based terminal for convenience.
Conclusion
We hope this guide has been helpful! 🚀 Remember, don’t hesitate to ask questions on the HolyHosting Discord or contact our support team.
Follow us on Twitter @HolyHosting to stay up to date.

Stuffy @ HolyHosting


















