How to Install MongoDB On Ubuntu / Linux?

The purpose of this guide is to explain how to install MongoDB on Ubuntu and Linux. The step-by-step instructions for installing mongodb on Windows and Mac OS can be found here.

MongoDB is a document-oriented NoSQL database that is widely used to build fast, scalable applications that handle large amounts of data.

Documents in MongoDB are stored in JSON format instead of traditional tables in relational databases.

The JSON format encapsulates data in key-value pairs with colons separating field names from values.

MongoDB flexible schema makes it an ideal choice for developers building fast, highly scalable applications capable of managing large volumes of data.

Install MongoDB On Ubuntu / Linux

Step 1: Install MongoDB

  • During the installation, the prerequisite packages must first be installed. Run the following command to accomplish this.
sudo apt install -y software-properties-common gnupg apt-transport-https ca-certificates
  • MongoDB can be installed via the APT package manager using the Ubuntu official repositories as follows when you about to install MongoDB on Ubuntu:
sudo apt install -y mongodb

The repositories do not provide the latest version of MongoDB. On Ubuntu, you must add the MongoDB package repository to your sources list file to install the most recent MongoDB package.

  • Using the wget command, you need to import the MongoDB public key on your system:
wget -qO - | sudo apt-key add -
  • The following output indicates that the public key has been added by above command:
  • Add MongoDB’s APT repository to /etc/apt/sources.list.d.
echo "deb [ arch=amd64,arm64 ] focal/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list

In the /etc/apt/sources.list.d/ directory, the mongodb-org-6.0.list file is added. The following line appears in this file:

deb [ arch=amd64,arm64 ] focal/mongodb-org/6.0 multiverse
  • Reload the local package index after adding the repository.
sudo apt update

With above command, Ubuntu learns about the newly added MongoDB repository and refreshes the local repositories.

  • Afterwards, install the mongodb-org meta-package that provides MongoDB.
sudo apt install -y mongodb-org
  • You can verify the version of MongoDB installed after the installation is complete by checking the following:
mongod --version

MongoDB’s version, Git version, and other details are displayed in the output of the command.

Step 2: Enable / Start the MongoDB

  • When MongoDB is installed, it is disabled by default. Run the following command to verify when you about to install MongoDB on Ubuntu :
sudo systemctl status mongod
  • Start the MongoDB service by running the following command:
sudo systemctl start mongod
  • Check the status of the service once again:
sudo systemctl status mongod

You can see from the above output that MongoDB is up and running. Connect to the database server and run a diagnostic command to confirm that the database is up and running as it comes to when you about to install MongoDB on Ubuntu.


  • Connecting to the database and displaying the server URL and port for MongoDB is shown in the command below.
mongo --eval 'db.runCommand({ connectionStatus: 1 })'

The command also returns MongoDB’s internal connectionStatus value:

ok” indicates that the database server is running as expected. MongoDB‘s default port is 27017 and the server’s URL is, which is the localhost address.

  • The default port can also be checked as follows:
sudo ss -pnltu | grep 27017
  • MongoDB can now be enabled to start on boot as soon as the service has been verified as working as expected.
sudo systemctl enable mongod

The installation and configuration of MongoDB to start automatically on boot has gone well so far.

Step 3: Create A Database & User

Your MongoDB instance should be running and configured for remote access at this point. Now let’s create a database and user in MongoDB.

  • Run the following command to access MongoDB  when you about to install MongoDB on Ubuntu:

You will see some information about MongoDB before you enter the MongoDB shell, such as the version of MongoDB and the MongoDB shell.

An access control warning appears just above the Mongo shell prompt informing you that data and configuration can’t be accessed because access control isn’t enabled. As of yet, authentication has not been enabled.

As soon as authentication is enabled, this warning will disappear. The installation process creates three databases by default. They are admin, config, and local.

  • Run the following command to list existing databases  when you about to install MongoDB on Ubuntu:
> show dbs

Create a database by using the use command. Run the following command to create a database called firms:

> use employees
  • The db command can be used to verify which database you’re in.
> db

When we talk about install MongoDB on Ubuntu, MongoDB provides shell methods for managing databases. You can create a new user in a database with db.createUser.

Usernames and passwords, as well as roles you wish to grant the user, are required for this method. JSON format is used to present this information.

  • The following syntax can be used to create a user with read and write privileges on the firm’s database called elonmusk.
    user: "elonmusk",
    pwd: "some_password",
    roles: [ { role: "readWrite", db: "firms" } ]
  • Db.getUsers() can be used to list the users created.
> show users


    _id: 'firms.elonmusk',
    userId: UUID("lcde5d61-fbba-6c84-806e-6a3c25709f02"),
    user: 'elonmusk',
    db: 'firms',
    roles: [ { role: 'readWrite', db: 'firms' } ],
    mechanisms: [ 'SCRAM-SHA-1', 'SCRAM-SHA-256' ]

Use the db.dropUser method to delete the user.

db.dropUser("firms", {w: "majority", wtimeout: 4000})


{ ok: 1 }

Step 4: Secure MongoDB

A MongoDB database does not enable authentication by default, implying that any user with access to the server can view, add, or delete data without being granted permissions.

The vulnerability can lead to a serious breach of your data. We will demonstrate how MongoDB can be secured in light of this.

  • Access the Mongo Shell to create an administrative user  when you about to install MongoDB on Ubuntu.
  • Connect or switch to the admin database next.
> use admin
  • Paste below lines and hit ENTER to create the database user.
    user: "Adminelonmusk",
    pwd: passwordPrompt(),
    roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]

Adminelonmusk is a user created by the line user: “Adminelonmusk“.

You are prompted for the administrative user’s password when you call the pwd: passwordPrompt() method. It is a safer option than typing the password in cleartext in the pwd: field as it comes to when you about to install MongoDB on Ubuntu.


The roles: [ { role: “userAdminAnyDatabase”, db: “admin” }, “readWriteAnyDatabase” ] line specifies the administrative roles. A read-write permission is granted to the Administrative user for the admin database as ity comes to.

  • Use the exit command or CTRL + C to exit Mongo Shell.
  • The next step is to enable authentication once the Admin user has been created. Open mongod.conf to do this.
sudo nano /etc/mongod.conf
  • Locate the security section. Make sure it is enabled by uncommenting it and adding the authorization directive.
  authorization: enabled
  • Save your changes and exit the configuration file. It is necessary to restart the Mongo service in order for the changes to take effect.
sudo systemctl restart mongod
  • Login again to Mongo Shell  when you about to install MongoDB on Ubuntu.

You will no longer see the warning.

  • You will, however, receive an error message if you attempt to perform any database-related tasks, such as viewing databases.
> show dbs
  • Running exit will log you out of the Mongo Shell before you can log in with authentication. Following is the syntax for logging in as the administrative user.
mongosh "mongodb://adminuser@mongo-ip-address:27017"

Data can be accessed, created and modified in the database only by the administrative user from now on.

There is a steady rise in popularity for MongoDB, a powerful and flexible NoSQL database. Unstructured data is among the most common types of data that will be stored in this database. You now know how to install mongodb on Ubuntu and other Linux distributions.

We value your feedback.

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!

Leave a Reply

Your email address will not be published. Required fields are marked *