How to Install MongoDB on Ubuntu 22.04

Published on

5 min read

Install MongoDB on Ubuntu 22.04

MongoDB is a free and open-source document database. It belongs to the family of NoSQL databases, which differs from the traditional table-based SQL databases like MySQL and PostgreSQL. The data in MongoDB is stored in flexible, JSON-like documents where fields can vary from document to document. It does not require a predefined schema, and the data structure can be changed over time.

Some key features of MongoDB are replication, indexing, user-defined searches, load balancing, and JavaScript execution.

This guide covers the step-by-step process of installing MongoDB on Ubuntu.


To follow the installation steps, ensure that you execute the commands as a root or user with sudo privileges on a Ubuntu 22.04 or 20.04 instance.

Installing MongoDB on Ubuntu

The standard Ubuntu repositories usually include an outdated MongoDB version. We will install the official MongoDB packages, which are always up to date. The installation of MongoDB on Ubuntu is fairly straightforward. We’ll enable the MongoDB repository, import the repository GPG key, and install the MongoDB packages.

At the time of writing this article, the latest version of MongoDB available from the official MongoDB repositories is version 7.0.

The first step is to install the dependencies necessary to add a new repository . Most likely, you will already have those packages installed on your system, but some packages may be missing:

sudo apt updatesudo apt install gnupg wget apt-transport-https ca-certificates software-properties-common

Next, import the MongoDB repository’s GPG key to your system:

wget -qO- \ | \  gpg --dearmor | \  sudo tee /usr/share/keyrings/mongodb-server-7.0.gpg >/dev/null

Add the MongoDB repository by creating the folowing file:

echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] \ $(lsb_release -cs)/mongodb-org/7.0 multiverse" | \  sudo tee -a /etc/apt/sources.list.d/mongodb-org-7.0.list

The $(lsb_release -cs) part in the command above, prints the Ubuntu codename. For example, if you have Ubuntu version 22.04, the command will print jammy.

Once the repository is enabled, update the local package index:

sudo apt update

Now you can install MongoDB by typing:

sudo apt install mongodb-org

The mongodb-org is a meta-package that includes the following packages:

  • mongodb-org-server - The mongod daemon and corresponding init scripts and configurations.
  • mongodb-org-mongos - The mongos daemon.
  • mongodb-org-shell - The mongo shell, an interactive JavaScript interface to MongoDB. It is used to perform administrative tasks thought the command line.
  • mongodb-org-tools - Contains several MongoDB tools for importing and exporting data, statistics, as well as other utilities.

By default, the MongoDB daemon doesn’t start after the installation. To start the service and enable it on boot execute:

sudo systemctl enable --now mongod

We can verify whether the installation has been successful by connecting to the MongoDB database server using the mongosh shell tool and printing the connection status:

mongosh --eval 'db.runCommand({ connectionStatus: 1 })'

The output will look something like below:

Current Mongosh Log ID:	655a70dfea9d876644ee6607
Connecting to:		mongodb://
Using MongoDB:		7.0.3
Using Mongosh:		2.0.2

  authInfo: { authenticatedUsers: [], authenticatedUserRoles: [] },
  ok: 1

The command returns information about the connection status and authenticated users and their permissions. A value of 1 for the ok field indicates success.

Configuring MongoDB

The MongoDB configuration file is located in the /etc directory and is named mongod.conf. The file uses the YAML format.

The default configuration settings are sufficient in most cases. However, for production environments, we recommend uncommenting the security section and enabling access control, as shown below:

sudo nano /etc/mongod.conf
  authorization: enabled

The authorization option enables Role-Based Access Control (RBAC) that regulates users’ access to database resources and operations. If this option is not enabled each user will have access to all databases and perform any action.

Whenever you make changes to the configuration, you need to restart the mongod service for changes to take effect:

sudo systemctl restart mongod

To find more information about the configuration options available in latest MongoDB version, visit the Configuration File Options documentation page.

Creating Administrative MongoDB User

If you enabled the MongoDB authentication, you’ll need to create an administrative user that can access and manage the MongoDB instance.

Access the mongo shell:


From inside the MongoDB shell, type the following command to connect to the admin database:

use admin
switched to db admin

Run the following command to create an administrative user:

    user: "myMongoAdmin",
    pwd: passwordPrompt(),
    roles: [
      { role: "userAdminAnyDatabase", db: "admin" },
      { role: "readWriteAnyDatabase", db: "admin" }

We named the admin use “myMongoAdmin”. Feel free to call it as you like. You can also enter a cleartext password instead of the passwordPrompt() method, but it is not recommended to display your password on the screen, and it will also be saved to your shell history.

You will be prompted to type the password, type it and press “Enter”. Do not forget to set a secure password:

Enter password
********{ ok: 1 }

“ok: 1” means the command was sucessfully executed.

Once done, exit the mongo shell with:


To test the changes, access the MongoDB shell using the administrative user you have previously created:

mongo -u myMongoAdmin -p --authenticationDatabase admin

You will be connected to MogoDB shell if you enter the correct password.

Switch to the “admin” database and run show users to get information about all the users and their roles:

use admin
switched to db admin
show users

In our example, we have only one user:

    _id: 'admin.myMongoAdmin',
    userId: new UUID("a00ab5ca-5c1f-4a3b-8f67-be8a79c7649c"),
    user: 'myMongoAdmin',
    db: 'admin',
    roles: [
      { role: 'userAdminAnyDatabase', db: 'admin' },
      { role: 'readWriteAnyDatabase', db: 'admin' }
    mechanisms: [ 'SCRAM-SHA-1', 'SCRAM-SHA-256' ]


We’ve shown you how to install and configure MongoDB on Ubuntu 20.04 and 22.04. For more information on this topic, visit the MongoDB Manual .

If you hit a problem or have feedback, leave a comment below.