How to Install MongoDB on Ubuntu 22.04 | Ver. 6

Introduction

MongoDB is a NoSQL Database used in many modern web applications. Unlike other databases, it does not use a traditional table-based structure and instead stores and organizes data using dynamic schemas in the form of JSON-like documents.

This feature makes MongoDB highly scalable and flexible, as its schema can be easily modified and updated without complex migrations. MongoDB is a preferred choice for applications requiring fast and efficient data processing and storage.

In this tutorial, you will install MongoDB on an Ubuntu 22.04 LTS server, test it, and learn how to manage it as a systemd service.

Prerequisites

To follow this tutorial, you will Ubuntu 22.04 server.

Required Dependency

It’s possible that you’ll need to install gnupg in order to import the key, but often it’s already installed by default. If it’s not already installed, you can install it using the following command.

sudo apt-get install gnupg

Install libssl1.1

To successfully install MongoDB on Ubuntu 22.04, you must install libssl1. Otherwise, you’ll encounter an error similar to the one shown below.

The following packages have unmet dependencies:
 mongodb-org-mongos : Depends: libssl1.1 (>= 1.1.1) but it is not installable

To install libssl1 follow the below steps by Adding the ubuntu 20.04 source:

echo "deb http://security.ubuntu.com/ubuntu focal-security main" | sudo tee /etc/apt/sources.list.d/focal-security.list

Update the package index on your server

sudo apt-get update

You can use the following command to install libssl1.1:

sudo apt-get install libssl1.1

With all of the necessary prerequisite packages installed, you’re now ready to proceed with the installation of MongoDB.

Install MongoDB

At the time of writing, the default Ubuntu repositories have a stable version of MongoDB available, which is version 3.6. However, the latest stable release of MongoDB is version 7.0.

To obtain the latest version of MongoDB, you must add MongoDB’s package repository to your APT sources using active voice. After adding the repository, you can then install a meta-package called mongodb-org, which always directs to the most current version of MongoDB.

To install it, update the package index on your server if you’ve not done so recently:

sudo apt-get update

To start, you must use active voice to import the public GPG key for the latest stable version of MongoDB. You can accomplish this by running the following command. If you intend to utilize a different version of MongoDB besides 6.0, ensure that you modify “6.0” in the URL portion of the command to correspond with the version you wish to install.

wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add -
OR
curl -fsSL https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add -

cURL is a tool that is used on the command line to transfer data. cURL is available on various operating systems. When an individual uses cURL, it reads the data saved at the URL provided and displays it on the screen. In the example below, cURL displays the contents of the GPG key file. Then, the following command adds the GPG key to the system’s list of trusted keys by utilizing sudo apt-key add, and passes on the output from cURL.

It’s important to note that the curl command uses the options -fsSL. These options tell cURL to fail silently, which means that if cURL is unable to contact the GPG server or if the server is down, it won’t accidentally add the error code to your list of trusted keys.

This command will return OK if the key was added successfully:

Output
OK

If you want to verify that the key was added correctly, you can use the following command:

apt-key list

This will return the MongoDB key somewhere in the output:

Output
/etc/apt/trusted.gpg
--------------------
pub   rsa4096 2019-05-28 [SC] [expires: 2024-05-26]
      2069 1EEC 3521 6C63 CAF6  6CE1 6564 08E3 90CF B1F5
uid           [ unknown] MongoDB 6.0 Release Signing Key <packaging@mongodb.com>
. . .

Right now, your APT installation doesn’t know where to locate the mongodb-org package required for installing the latest version of MongoDB.

APT searches for online sources of packages in two places on your server: the sources.list file and the sources.list.d directory. The sources.list file contains a list of active sources of APT data, with each source on a separate line and the most preferred sources listed first. On the other hand, the sources.list.d directory lets you add sources.list entries as separate files.

Execute the command below to create a file named mongodb-org-6.0.list in the sources.list.d directory. The file will contain only one line which reads as follows: “deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/6.0 multiverse”.

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

This one line provides APT with all the information it needs about the source and its location.

  • deb: This means that the source entry references a regular Debian architecture. In other cases, this part of the line might read deb-src, which means the source entry represents a Debian distribution’s source code.
  • [ arch=amd64,arm64 ]: This specifies which architectures the APT data should be downloaded to. In this case, it specifies the amd64 and arm64 architectures.
  • https://repo.mongodb.org/apt/ubuntu: This is a URI representing the location where the APT data can be found. In this case, the URI points to the HTTPS address where the official MongoDB repository is located.
  • focal/mongodb-org/6.0: Ubuntu repositories can contain several different releases. This specifies that you only want version 6.0 of the mongodb-org package available for the focal release of Ubuntu (“Focal Fossa” being the code name of Ubuntu 20.04).
  • multiverse: This part points APT to one of the four main Ubuntu repositories. In this case, it’s pointing to the multiverse repository.

After running this command, update your server’s local package index so APT knows where to find the mongodb-org package:

sudo apt-get update

Following that, you can install MongoDB:

sudo apt-get install -y mongodb-org

When prompted, press Y and then ENTER to confirm that you want to install the package.

Once the command has finished executing, MongoDB will be installed on your system. However, it still needs to be configured before it can be used. In the next step, you’ll start MongoDB and ensure that it’s functioning properly.

Starting the MongoDB Service and Testing

In the previous step, MongoDB was installed and configured to run as a daemon controlled by systemd. As a result, you can manage MongoDB using different systemctl commands. However, the installation process does not initiate the service automatically.

Run the following systemctl command to start the MongoDB service:

sudo systemctl start mongod.service

Next, verify the status of the service. It’s important to note that the “.service” suffix is not required in the service file definition when executing this command. If the suffix is not included, systemctl will automatically append it to the argument.

sudo systemctl status mongod

When you execute this command, it will return output similar to the following, indicating that the service is currently operational:

Output
● mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)
     Active: active (running) since Tue 2023-01-01 12:57:06 UTC; 2s ago
       Docs: https://docs.mongodb.org/manual
   Main PID: 37128 (mongod)
     Memory: 64.8M
     CGroup: /system.slice/mongod.service
             └─37128 /usr/bin/mongod --config /etc/mongod.conf

Once you have confirmed that the MongoDB service is running properly, you can enable it to start automatically upon booting your system with the following command:

sudo systemctl enable mongod

Next, we’ll look at how to manage the MongoDB server instance with systemd.

Managing the MongoDB Service

As mentioned earlier, MongoDB is configured to run as a systemd service during the installation process described in Step 1. Therefore, you can use standard systemctl commands to manage MongoDB just like other Ubuntu system services.

As mentioned previously, the systemctl status command checks the status of the MongoDB service:

sudo systemctl status mongod

You can stop the service by typing:

sudo systemctl stop mongod

To start the service when it’s stopped, run:

sudo systemctl start mongod

You can also restart the server by running:

sudo systemctl restart mongod

In Step 2, you enabled MongoDB to start automatically with the server. If you ever wish to disable this automatic startup, type:

sudo systemctl disable mongod

Conclusion

This tutorial covered adding the official MongoDB repository to your APT instance and installing the latest version of MongoDB. You also confirmed that MongoDB is running correctly and practiced using systemctl commands.

Leave a Reply

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