How to install MongoDB on Linux?

Overview

Many modern web apps manage their databases using MongoDB. Open-source NoSQL database management system MongoDB is independent of table-based RBDMS and is a NoSQL database system.

Easily install MongoDB on Linux with this step-by-step guide. Discover how to set up and start using this powerful NoSQL database on your Linux machine.

Prerequisites

There are certain prerequisites that need to be met before you begin.

  • System admin rights and access

  • A system or device running Ubuntu Linux 20.04 or later

  • Familiarity with Linux CLI (Command Line Interface)

Get Started

The official Ubuntu package repository has a stable version of MongoDB. Nonetheless, as of 2022, MongoDB 6.0 is the most recent stable release; however, the version that is presently available from the default Ubuntu repository is 3.6.

To receive the most recent update of this software, you must add the specific package repository for MongoDB to your APT sources. The next step is to install the meta-package mongodb-org, which is continually pointing to the most recent MongoDB release.

Step-1: Importing MongoDB package

Run the command shown below to import the most current stable MongoDB release's public GPG key. Remember to update this script's URL component if you want to install MongoDB in a version other than 4.4.

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

A command-line tool for data transmission called cURL is available on a variety of operating systems. It retrieves whatever data is present at the provided URL and publishes it to the system's output. In the example below, the sudo apt-key add - command is used to add the GPG key to your list of trusted keys after having cURL show the contents of the GPG key file.

Remember that the -fsSL arguments are also used in this curl command, which together instructs cURL to fail quietly. This makes sure that if for any reason cURL is unable to connect to the GPG server or the GPG server is unavailable, it won't mistakenly add the resulting error code to your list of trusted keys.

Output: OK

You may use the following command to double-check your key list:

$ apt-key list

Step 2: Finding the latest MongoDB version

Your APT installation is currently unable to locate the mongodb-org package required to setup the most recent MongoDB version.

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

Step 3: Updating the latest version

After executing the script, you must change your local server package so that APT will understand where to look for the MongoDB package to install:

$ sudo apt update

Step-4: Installing MongoDB

After this we can start installing MongoDB:

$ sudo apt install mongodb-org 

When you are prompted press y and Enter to confirm.

Your Linux computer will have MongoDB installed after the task has finished.

Initiating MongoDB service and evaluating the database

During the installation process described in the step above, MongoDB is automatically configured to run as a systemd-managed daemon, allowing you to manage MongoDB using the various systemctl commands. During this installation procedure, the service is not started automatically.

Use the systemctl command to launch the MongoDB service as shown below:

$ sudo systemctl start mongod.service

then verify the service's status. Be aware that the extension. service is not included in the service file definition for this command. Systemctl will automatically add this suffix to any parameter you supply if it doesn't already exist, so you don't need to specify it:

$ sudo systemctl status mongod

After confirming that the MongoDB service is operating as intended, allow it to start up automatically:

$ sudo systemctl enable mongod

Conclusion

The most recent version of MongoDB was installed throughout this tutorial, and you also added the official MongoDB repository to your APT instance. You then ran a few systemctl commands and verified MongoDB's operation.

Last updated