Skip to main content

Install MongoDB on Ubuntu 22.04

This guide provides step-by-step instructions for installing MongoDB on Ubuntu. MongoDB is a NoSQL database that offers high performance, high availability, and easy scalability.

Prerequisites

  • A system running Ubuntu 22.04
  • A user account with sudo privileges
  • An internet connection

Get Started

  • Begin by updating your apt repository to ensure you have the latest package information and dependencies. Open a terminal and run the following command:
sudo apt update

This command will refresh the package lists and update any outdated packages on your system.

  • Next, you need to install the necessary packages for MongoDB. These include gnupg for handling GPG keys and curl for downloading files. Execute the following command in the terminal:
sudo apt install gnupg curl

This command will download and install the required packages from the Ubuntu repositories.

  • Add the MongoDB GPG key to your system to verify the authenticity of the MongoDB packages. Run the following command in the terminal:
curl -fsSL https://pgp.mongodb.com/server-7.0.asc | sudo apt-key add -

This command fetches the MongoDB GPG key and adds it to your list of trusted keys.

  • Add the MongoDB repository to your system’s package source list. Execute the following command:
echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu $(lsb_release -cs) mongodb-org 7.0" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list

This command adds the MongoDB repository to your apt sources list, allowing you to install MongoDB from there.

  • Update your package list to include the MongoDB packages from the newly added repository:
sudo apt update
  • Install MongoDB by running the following command:
sudo apt install mongodb-org

This command will download and install MongoDB along with its dependencies.

  • Once the installation is complete, start the MongoDB service and enable it to start on boot:
sudo systemctl start mongod
sudo systemctl enable mongod
  • Verify that MongoDB is running correctly by checking its status:
sudo systemctl status mongod

You should see an output indicating that MongoDB is active and running.

  • Optionally, you can check the MongoDB version installed by running:
mongod --version

This command displays the version of MongoDB installed on your system.

Finished

Congratulations! You have now installed MongoDB on your Ubuntu machine. MongoDB is ready to be used, allowing you to leverage its powerful NoSQL database capabilities for your projects and applications.