How to Install MongoDB on Ubuntu 16.04 to 22.04

0 comments 8.7K views 1 minutes read
Published on: August 12, 2022 | Last updated on: October 11, 2024

This tutorial will show you how to install MongoDB on Ubuntu 16.04 to 22.04 LTS.

The modern web is littered with apps that rely on MongoDB, a document database. The fact that it does not use the more common table-based relational database structure is what gives rise to its classification as a NoSQL database.

Since it employs JSON-like documents with dynamic schemas, adding data to a MongoDB database does not necessitate creating a schema in advance, as is the case with relational databases. Anytime you need to make a change to the schema, you may do it without having to create a whole new database.

Prerequisites

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

Install Libssl1 to avoid the error E: Unable to correct problems, you have held broken packages. Add its repository to your system using the command below.

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

Update your system to apply changes.

sudo apt update

Then install Libssl1 using the following command.

sudo apt install libssl1.1

To Install MongoDB on Ubuntu 22.04, start by adding MongoDB APT repo signing key using the following command.

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

Install the MongoDB APT Repo after that using the command below.

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

Next update changes in your system.

sudo apt update

Install MongoDB on Ubuntu

Install the latest version of MongoDB on Ubuntu by running the command below.

sudo apt install -y mongodb-org

In case you want MongoDB to run automatically on boot run the following command.

sudo systemctl enable mongod

Start MongoDB by running the following command.

sudo systemctl start mongod

In case MongoDB doesn’t start run the command below to reload.

sudo systemctl daemon-reload

Confirm if MongoDB is running.

sudo systemctl status mongod

Sample output

● mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)
     Active: active (running) since Tue 2022-02-27 23:24:25 UTC; 2min 8s ago
       Docs: https://docs.mongodb.org/manual
   Main PID: 6618 (mongod)
     Memory: 60.4M
     CGroup: /system.slice/mongod.service
             └─6618 /usr/bin/mongod --config /etc/mongod.conf
lines 1-10/10 (END)

To stop MongoDB run the command below.

sudo systemctl stop mongod

To connect to the MongoDB shell simply type.

mongosh

Uninstall MongoDB on Ubuntu

You can completely remove MongoDB on your system using the following steps.

Start by stopping MongoDB process using the command below.

sudo systemctl stop mongod

Then remove any previously installed MongoDB packages.

sudo apt remove --purge --auto-remove mongodb-org*

Next run the following command to remove MongoDB databases.

sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongodb