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.
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:
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:
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.
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:
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:
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:
To install MongoDB, execute the following command:
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:
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:
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
systemctlcommands 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:
If you'd rather use
nanoor another tool to edit this file, that's fine too.Navigate to the
#securityline and change it to be the following:After saving the file, restart the MongoDB instance by executing the following:
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:
You'll probably want to change the
userandpwdto 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:
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.