How to Install MongoDB on Ubuntu: A Step-by-Step Guide for Beginners
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. 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 - A Complete Guide with Examples.
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 --dearmorDepending 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.listTo 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 updateTo install MongoDB, execute the following command:
sudo apt-get install -y mongodb-orgThe above command will install the latest version of MongoDB, but if you need a particular version, you can include the version information to 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 mongodYou 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.confIf 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 mongodWe'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 adminThe 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 titled, How to Create a Database in MongoDB - Quick & Easy Tutorial.
This content first appeared on DataCamp.

Nic Raboy
Nic Raboy is an advocate of modern web and mobile development technologies. He has experience in C#, JavaScript, Golang and a variety of frameworks such as Angular, NativeScript, and Unity. Nic writes about his development experiences related to making web and mobile development easier to understand.