How to Install MongoDB on Ubuntu 22.04: A Step-by-Step Guide

How to Install MongoDB on Ubuntu 22.04: A Step-by-Step Guide

Installing MongoDB on Ubuntu 22.04 can seem challenging, but it is a straightforward process. He can easily set it up by following a few simple steps. This blog post will guide him through each stage, from preparing the system to running the database. In this tutorial we focus on how to install mongodb on Ubuntu 22.04.

An open laptop displaying the Ubuntu 22.04 desktop with a terminal window open. The user types commands to download and install MongoDB

MongoDB is a popular NoSQL database known for its flexibility and scalability. By the end of this article, he will be ready to use MongoDB for his projects. Whether he is new to databases or looking to enhance his skills, this guide is perfect for him.

The first step is to ensure that his system meets the requirements for installation. After that, he will install the necessary packages and set up the MongoDB repository. Following the straightforward instructions will lead him to a successful installation.

Preparing Ubuntu 22.04 for MongoDB Installation

A computer screen displaying the Ubuntu 22.04 desktop with a terminal window open, executing commands for the installation of MongoDB

Preparing Ubuntu 22.04 for MongoDB involves several important steps. These include updating the package database, installing necessary dependencies, and adding the MongoDB repository.

Updating the Package Database

Before installing any new software, it is crucial to update the package database. This ensures that Ubuntu has the latest information about available software and their versions.

To start, open a terminal. Then, run the following command:

sudo apt update

This command updates the list of packages. Once completed, it’s a good practice to upgrade the installed packages. Use this command:

sudo apt upgrade -y

The -y option automatically confirms prompts. After the upgrade finishes, the system will be ready for new software installations.

Installing Required Dependencies

Next, installing required dependencies is essential for a smooth MongoDB installation. These packages prepare the system for the MongoDB setup.

Run this command in the terminal to install wget and gnupg:

sudo apt install -y wget gnupg
  • wget helps to download files from the web.
  • gnupg is necessary for managing keys.

These dependencies ensure that the MongoDB installation process can download and verify packages correctly.

Adding the MongoDB Repository

The final preparation step involves adding the official MongoDB repository to the system. This step allows Ubuntu to access MongoDB packages directly.

First, import the MongoDB public GPG key. Use this command:

wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo gpg --dearmor -o /usr/share/keyrings/mongodb-archive-keyring.gpg

Next, add the repository by creating a .list file in the sources directory. Execute:

echo "deb [signed-by=/usr/share/keyrings/mongodb-archive-keyring.gpg] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list

After adding the repository, update the package database again using:

sudo apt update

Now, the system is fully prepared for the MongoDB installation.

Installing and Setting Up MongoDB – how to install mongodb on Ubuntu 22.04:

A computer screen displaying the installation process of MongoDB on Ubuntu 22.04, with the terminal open and commands being entered

To install MongoDB on Ubuntu 22.04, several key steps must be followed. These include installing the necessary packages, configuring the MongoDB service, enabling it to start on boot, and verifying the installation.

Installing MongoDB Packages

First, open the terminal. MongoDB packages can be installed using the package manager. Begin by importing the public key:

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

Next, create a list file for MongoDB by using:

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

After setting up the source, update the package list:

sudo apt update

Finally, install MongoDB by running:

sudo apt install -y mongodb-org

Configuring the MongoDB Service

Once the installation is complete, it is necessary to configure the MongoDB service settings. The default configuration file is located at /etc/mongod.conf.

Users can edit this file to change settings, such as:

  • Storage Engine: The default is WiredTiger.
  • Network Bind IP: This controls which IP addresses can connect to MongoDB.

To edit the file, use:

sudo nano /etc/mongod.conf

After making changes, save and exit the editor.

Enabling MongoDB to Start on Boot

To ensure MongoDB automatically starts when the system boots, use the following command:

sudo systemctl enable mongod

This command enables the service. You can also start the service right away using:

sudo systemctl start mongod

To further manage the service, the following commands can be helpful:

  • Start: sudo systemctl start mongod
  • Stop: sudo systemctl stop mongod
  • Restart: sudo systemctl restart mongod

Verifying the Installation

Verifying the MongoDB installation is crucial to ensure it is running correctly. To check the status of the service, use:

sudo systemctl status mongod

The output should indicate that it is active and running.

Another way to verify is to connect to MongoDB using the Mongo shell. Run:

mongosh

If connected, the shell will confirm the version of MongoDB, indicating a successful installation.

Share this article: