Efficiently Installing MongoDB on Ubuntu 22.04: A Step-by-Step Guide for Reliable Database Setup
MongoDB is a leading NoSQL database favored for its scalability and flexibility, making it crucial for modern app development. Mastering its installation on Ubuntu 22.04 ensures stability and security in your backend infrastructure, setting a strong foundation for any data-driven project.
Forget generic installation guides cluttered with unnecessary steps. This post distills the MongoDB setup process on Ubuntu 22.04 to the essential commands and configurations, empowering developers to get up and running swiftly with a secure, optimized environment.
Why MongoDB on Ubuntu 22.04?
Ubuntu 22.04 (Jammy Jellyfish) is the latest long-term support (LTS) release, renowned for its stability and robust support — traits every backend service needs. Combining MongoDB’s dynamic NoSQL capabilities with Ubuntu’s reliability creates an environment perfect for contemporary applications requiring flexible and performant data storage.
Step-by-Step MongoDB Installation Guide
1. Update Your System
Before starting, ensure your system packages are current:
sudo apt update && sudo apt upgrade -y
This step helps you avoid compatibility issues during installation.
2. Import the MongoDB Public GPG Key
MongoDB packages are signed; importing their official public key verifies package integrity during installation:
wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo tee /usr/share/keyrings/mongodb-server-6.0.gpg > /dev/null
Note: This example uses MongoDB 6.0, the current stable release at the time of writing.
3. Create the MongoDB Source List File
Add the official MongoDB repository to apt sources for Ubuntu 22.04:
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-6.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list
This tells your system where to fetch MongoDB packages.
4. Reload Local Package Database
Refresh your package lists to include MongoDB repository packages:
sudo apt update
5. Install MongoDB Packages
Install the latest stable version of MongoDB along with helpful tools:
sudo apt install -y mongodb-org
This installs:
mongod
(the database daemon),mongo
(the interactive shell),- and other required components.
6. Start and Enable MongoDB Service
Start the MongoDB service immediately and ensure it runs on system boot:
sudo systemctl start mongod
sudo systemctl enable mongod
Check running status:
sudo systemctl status mongod
If you see active (running), congratulations—you have a working database server!
7. Verify Installation by Connecting to Mongo Shell
Run the interactive shell to ensure you can connect:
mongosh
If successful, you should see a prompt like this:
test>
Try listing existing databases as a quick check:
show dbs
Optional But Recommended: Secure Your MongoDB Instance
By default, MongoDB doesn't enforce authentication out-of-the-box on localhost binding only—meaning anyone with server access can read/write data unless secured.
Here’s a quick way to enable authentication:
- Create an admin user
Inside mongosh
, execute:
use admin
db.createUser({
user: "admin",
pwd: "YourStrong!Passw0rd",
roles: [{ role: "userAdminAnyDatabase", db: "admin" }]
});
Replace "YourStrong!Passw0rd"
with a complex password.
- Enable Authentication in Config
Edit /etc/mongod.conf
using your preferred editor, e.g., nano:
sudo nano /etc/mongod.conf
Find the security
section and add:
security:
authorization: enabled
- Restart MongoDB
Apply changes by restarting service:
sudo systemctl restart mongod
Now, any attempt to access databases requires proper authentication credentials.
Example login from the shell with authentication enabled:
mongosh -u admin -p YourStrong!Passw0rd --authenticationDatabase admin
Conclusion
With these essentials steps, you’re set up with a fast, secure MongoDB instance on Ubuntu 22.04 without excess fluff or confusing detours.
Recap:
- Update your system.
- Add official MongoDB repo and key.
- Install
mongodb-org
package. - Start and enable
mongod
service. - Verify connectivity via
mongosh
. - Optionally secure installation by enabling user authentication.
Whether you’re building prototypes or scaling production apps, this streamlined setup gives you stable groundwork without compromises.
Happy coding!
If this helped you or if you encounter any hiccups during installation, feel free to leave comments below — I’m here to help troubleshoot or share tips!