Course
So you're ready to dabble with MongoDB on Linux and you've chosen Ubuntu as the variant you want to work with. Fantastic choice because you're in for a fairly easy experience that will get you up and running with MongoDB in no time!
In this tutorial, we'll explore installing MongoDB through the Ubuntu Package Manager and do some minor quality of life improvements that will better secure your installation from the rest of the world. You can also check out the video guide.
While we'll be exploring the native installation experience, if you'd prefer to use Docker on Ubuntu for working with MongoDB, check out my previous tutorial, running MongoDB in Docker.
Prerequisites
To be successful with this tutorial, you'll need to be using a modern version of Ubuntu. I'm using Ubuntu 25.04 Desktop Edition with an ARM CPU, but you should have no issue with other CPU architectures or Ubuntu Server Edition.
While the tooling is fairly flexible for working with MongoDB, for the purpose of this example, the MongoDB Shell will be the main tool used for interacting with MongoDB.
Installing MongoDB Community Edition on Ubuntu With the Package Manager
Since Ubuntu is a wildly popular variant of Linux, installation of MongoDB is fairly seamless, like how you'd expect other packages to be installed. The process will include adding MongoDB as a software source to our package manager, installing the package, and then running the installed package.
Let's start by importing the GPG key for MongoDB. From the command line, execute the following:
curl -fsSL https://www.mongodb.org/static/pgp/server-8.0.asc | sudo gpg -o /usr/share/keyrings/mongodb-server-8.0.gpg --dearmor
Depending on the major version of MongoDB that you wish to use, the version information in this command and future commands may need to change. We'll be working with MongoDB 8.X for this example.
With the GPG key imported, we can proceed to creating the package list file. Execute the following command:
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-8.0.gpg ] https://repo.mongodb.org/apt/ubuntu noble/mongodb-org/8.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-8.0.list
To be clear, yes, I am using Ubuntu 25.04 (Plucky) and yes, the above package list references MongoDB 24.04 (Noble). It worked fine for me, but pay attention as versions become significantly different. Otherwise, you might end up with unintended results.
At this point, we can refresh the package manager on our computer:
sudo apt-get update
To install MongoDB, execute the following command:
sudo apt-get install -y mongodb-org
The above command will install the latest version of MongoDB, but if you need a particular version, you can include the version information in the command like mongodb-org=8.0.11
.
MongoDB should be installed on the computer as of now. However, it might not be running at the time installation has completed. To run MongoDB on Ubuntu, execute the following command:
sudo systemctl start mongod
You can validate that MongoDB is functioning correctly by connecting to it from the MongoDB Shell, MongoDB Compass, or any similar tooling.
In the example of the MongoDB Shell, you can execute the following command:
mongosh "mongodb://localhost:27017"
Of course, the above command assumes you are attempting to connect to the MongoDB instance from the same computer.
Should MongoDB need to be stopped, restarted, or similar, you can make use of the various systemctl
commands that are common on Ubuntu.
Adding Basic Security to the MongoDB Instance on Ubuntu
By default, authentication is disabled in fresh installations of MongoDB. This might be fine depending on your use case, but it's usually a good idea to at least require authentication and authorization.
Of course, you'll probably want to explore other hardening best practices such as firewall rules and similar, but for the scope of this tutorial, securing your Ubuntu installation of MongoDB with a username and password is a good starting point.
On Ubuntu, execute the following command:
sudo vi /etc/mongod.conf
If you'd rather use nano
or another tool to edit this file, that's fine too.
Navigate to the #security
line and change it to be the following:
security:
authorization: "enabled"
After saving the file, restart the MongoDB instance by executing the following:
sudo systemctl restart mongod
We're not done yet. We've only activated authentication, but we need to create our administrative user.
If you're using the MongoDB Shell, execute the following:
use admin;
db.createUser({
"user": "nraboy",
"pwd": "mongodb",
"roles": [
{
"role": "userAdminAnyDatabase",
"db": "admin"
}
]
});
You'll probably want to change the user
and pwd
to something that makes sense and is more secure, but this is just an example.
Now, when you want to connect to MongoDB using the MongoDB Shell, you'd use a command like this:
mongosh --host localhost --port 27017 -u nraboy -p --authenticationDatabase admin
The experience will be slightly different depending on the tool you're using, but you can get a general idea based on the information we're providing.
Conclusion
You just saw how to deploy MongoDB on a Linux host—in particular, Ubuntu. The great thing about MongoDB is that it is fairly easy to install and run anywhere and on all types of hardware.
If MongoDB Atlas isn't an option and you need to explore something other than Ubuntu, it might be worth checking out my other tutorial that demonstrates a container deployment using Docker. However, if you're ready to jump to the next step, consider checking out my previous tutorial on creating a Database in MongoDB.
If you’re working with Python and enjoy structured approaches to learning, check out the Introduction to MongoDB in Python course.
Become a Data Engineer
FAQs
Can I install MongoDB on Ubuntu using Snap or Flatpak?
Although Snap and Flatpak support many applications on Ubuntu, MongoDB is best installed via the official APT repository to ensure compatibility and updates.
Is MongoDB supported on Ubuntu Server as well as Desktop?
Yes, MongoDB works seamlessly on both Ubuntu Server and Desktop. The installation process is virtually identical, though server environments may need extra security considerations.
How do I uninstall MongoDB completely from Ubuntu?
Run sudo apt-get purge mongodb-org*
followed by sudo rm -r /var/log/mongodb /var/lib/mongodb
to fully remove MongoDB and its associated data.
How do I check the MongoDB version installed on Ubuntu?
Use the command mongod --version
or mongosh --version
depending on what component you want to check. These give you the currently installed version.
How can I enable MongoDB to start on boot in Ubuntu?
Enable the MongoDB service using sudo systemctl enable mongod
. This ensures MongoDB automatically starts each time your system boots up.
How do I update MongoDB on Ubuntu without breaking it?
To update MongoDB safely, first check compatibility with your current config and data. Then run sudo apt-get update && sudo apt-get upgrade mongodb-org
if using the official repo.
How can I back up my MongoDB data on Ubuntu?
Use the mongodump
tool provided with MongoDB to create a backup. You can also script it with cron
to perform regular automated backups.
Is it safe to expose MongoDB running on Ubuntu to the internet?
By default, no. You should always use firewalls, VPNs, or SSH tunnels, and enforce authentication. Exposing an unsecured MongoDB to the internet is a security risk.

Nic Raboy is a Developer Relations Lead at MongoDB where he leads a team of Python, Java, C#, and PHP developers who create awesome content to help developers be successful at including MongoDB in their projects. He has experience with Golang and JavaScript and often writes about many of his development adventures.