How to Install MongoDB on Ubuntu 22.04: Step-by-step

How to Install MongoDB on Ubuntu 22.04: Step-by-step

Step 1: Install MongoDB on Ubuntu 22.04

Installing Prerequisite Packages

First, install the necessary packages:

sudo apt install software-properties-common gnupg apt-transport-https ca-certificates -y

Setting Up the MongoDB Repository

Import the MongoDB public key:

curl -fsSL https://pgp.mongodb.com/server-7.0.asc | sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg --dearmor

Add the MongoDB 7.0 APT repository:

echo "deb [arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list

Installing MongoDB

Update your package list and install MongoDB:

sudo apt update
sudo apt install mongodb-org -y

Verify the MongoDB installation by checking its version:

mongod --version

Step 2: Start the MongoDB Service

Checking Service Status

Initially, MongoDB's service is disabled. Check its status:

sudo systemctl status mongod

Starting MongoDB

Start the MongoDB service:

sudo systemctl start mongod

Confirm that the service is active:

sudo systemctl status mongod

Check if the server is listening on the default port (27017):

sudo ss -pnltu | grep 27017

Enabling MongoDB on Boot

Enable MongoDB to start automatically on system boot:

sudo systemctl enable mongod

More to read