Home MongoDB How to Install MongoDB in Ubuntu 22.04/20.04

How to Install MongoDB in Ubuntu 22.04/20.04

Numerous modern web applications fancy the use of a document database to store data; a case exhibited by MongoDB. The no traditional table-based relational database structure classifies MongoDB as a NoSQL database. It instead embraces dynamic schemas through the use of JSON-like documents.

With MongoDB, there is no need for a predefined schema for data to be stored on a database. The MongoDB schema is open to altercations and updates making it unnecessary to set up a new database whenever new project specifications need implementation.

This article will walk us through the installation and configuration of MongoDB on Ubuntu 22.04 and Ubuntu 20.04. Before proceeding, make sure you have root user access or you are a Sudoer user on the system you are using.

Installing MongoDB in Ubuntu

Always ensure that your Ubuntu operating system is up-to-date.

$ sudo apt update

Since we are after the latest stable release of MongoDB, our APT sources need the inclusion of its dedicated package repository. The current stable release for MongoDB is 5.0.

We will import MongoDB’s public GPG key by pointing to its latest stable version release number (5.0) using the following curl command.

$ curl -fsSL https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -

Next, you need to configure MongoDB’s APT repository, which will query inside the sources.list.d directory for download and installation sources of targeted packages. We should be able to create a file called mongodb-org-5.0.list with the following single line entry.

$ echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list
Configure MongoDB APT Repository
Configure MongoDB APT Repository

Update your Ubuntu system again so that the APT package manager knows where to fetch the MongoDB package for download and installation.

$ sudo apt update

Finally, you can now proceed to install MongoDB on Ubuntu with the following command.

$ sudo apt install mongodb-org -y
Install MongoDB on Ubuntu
Install MongoDB on Ubuntu

Once installed, you need to start the service responsible for MongoDB with the following command:

$ sudo systemctl start mongod

Afterward, check on its status with the following command to be sure that it is running:

$ sudo systemctl status mongod
Check MongoDB on Ubuntu
Check MongoDB on Ubuntu

If you need the MongoDB database to keep running even after a successful system reboot, execute the following command:

$ sudo systemctl enable mongod

To test the connection to the MongoDB database, we will run the following command:

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

The output of the above command should reveal its port, address, version, and whether the database is active.

Check MongoDB Connection on Ubuntu
Check MongoDB Connection on Ubuntu

To access MongoDB shell, run:

$ mongo

You will be greeted with the following interface:

Access MongoDB on Ubuntu
Access MongoDB on Ubuntu

From here, you are free to accomplish any database-related objective that brought you to this article.

Query MongoDB on Ubuntu
Query MongoDB on Ubuntu

MongoDB is an exciting addition to any database project. With this article, you should be able to put a lid on the first of many projects MongoDB will help you accomplish.

Ravi Saive
I am an Experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies. Founder of TecMint.com, LinuxShellTips.com, and Fossmint.com. Over 150+ million people visited my websites.

Each tutorial at UbuntuMint is created by a team of experienced writers so that it meets our high-quality standards.

Was this article helpful? Please add a comment to show your appreciation and support.

11 thoughts on “How to Install MongoDB in Ubuntu 22.04/20.04”

  1. A little clarification:

    After "sudo systemctl enable mongod" you have to run "sudo systemctl start mongod" or reboot the system.

    Reply
  2. I am unsure of the consequences of installing libssl1.1 alongside libssl3. As a workaround, I am using mongodb from a docker container instead. I hope that the official support for 22.04 is not so far away though.

    Reply
  3. Dude, I don’t think it actually works on Ubuntu 22.04

    $ sudo apt install mongodb-org -y
    

    Sample Output:

    Reading package lists... Done
    Building dependency tree... Done
    Reading state information... Done
    Some packages could not be installed. This may mean that you have
    requested an impossible situation or if you are using the unstable
    distribution that some required packages have not yet been created
    or been moved out of Incoming.
    The following information may help to resolve the situation:
    
    The following packages have unmet dependencies:
     mongodb-org-mongos : Depends: libssl1.1 (>= 1.1.1) but it is not installable
     mongodb-org-server : Depends: libssl1.1 (>= 1.1.1) but it is not installable
     mongodb-org-shell : Depends: libssl1.1 (>= 1.1.1) but it is not installable
    E: Unable to correct problems, you have held broken packages.
    
    Reply

Leave a Reply to halman Cancel reply

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published or shared. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.