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

A Beginner's Tutorial for Installing MongoDB on Ubuntu with Necessary Commands

ยท

3 min read

MongoDB is a powerful NoSQL database that is widely used in modern web development. If you are running Ubuntu on your machine and want to install MongoDB, this guide will walk you through the steps necessary to get MongoDB up and running.

Step 1: Import the MongoDB public key

The first step is to import the MongoDB public key into your Ubuntu system. This will enable your system to verify the authenticity of the MongoDB package that you will download and install.

Open a terminal window and run the following command:

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 68818C72E52529D4

Step 2: Add the MongoDB repository to the sources list

Next, you need to add the MongoDB repository to your Ubuntu system's sources list. This will enable you to download and install the latest version of MongoDB.

Run the following command to create a list file for MongoDB:

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

This command adds the MongoDB repository to Ubuntu's sources list so that you can install the latest version.

Note: Make sure to replace '5.0' with the version you want to install. Also, replace the Ubuntu version name '$(lsb_release -sc)' with your version's name (e.g., 'focal' for Ubuntu 20.04 LTS).

Step 3: Update the package list:

Enter the following command to update the package list:

sudo apt-get update

This command updates the list of packages available in the repositories.

Step 4: Install MongoDB:

Enter the following command to install the MongoDB package:

sudo apt-get install -y mongodb-org

This command installs MongoDB along with its dependencies.

Step 5: Start the MongoDB service:

Enter the following command to start the MongoDB service:

sudo systemctl start mongod

This command starts the MongoDB service, and you can now use it to store data.

Step 6: Enable the MongoDB service:

Enter the following command to enable the MongoDB service to start automatically when you boot your system:

sudo systemctl enable mongod

This command enables the MongoDB service, so it starts automatically each time you boot your system.

Step 7: Verify the MongoDB installation:

Enter the following command to verify that MongoDB is running correctly:

sudo systemctl status mongod

This command shows you the status of the MongoDB service. If the service is running correctly, you should see a message indicating that the service is active and running.

Congratulations! You have successfully installed MongoDB on Ubuntu, and you can now start using it to store data for your web applications.

If you have any questions or suggestions then, feel free to reach out to me on Twitter or LinkedIn. You can find me on Twitter @DivyParekh and LinkedIn at LinkedIn. I look forward to connecting with you and discussing all things!

Did you find this article valuable?

Support Divy Parekh by becoming a sponsor. Any amount is appreciated!

ย