Install MongoDB on Ubuntu 22.04

This article is going to teach you how to Install MongoDB on Ubuntu 22.04. MongoDB is a document-oriented NoSQL database for storing large amounts of data. MongoDB uses collections and documents instead of tables and rows, as in traditional relational databases. Documents are made up of key-value pairs, which are MongoDB’s basic data unit. Collections are the equivalent of relational database tables in that they include sets of documents and functions.

How to Install MongoDB on Ubuntu 22.04

  • Install required dependencies.
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 22.04

  • Install the latest version of MongoDB on Ubuntu 22.04 by running the command below.
sudo apt install -y mongodb-org

Sample output

Reading package lists... Done
Reading package lists... Done
    Building dependency tree... Done
    Reading state information... Done
    The following additional packages will be installed:
      mongodb-database-tools mongodb-mongosh mongodb-org-database
      mongodb-org-database-tools-extra mongodb-org-mongos
      mongodb-org-server mongodb-org-shell mongodb-org-tools
    The following NEW packages will be installed:
      mongodb-database-tools mongodb-mongosh mongodb-org
      mongodb-org-database mongodb-org-database-tools-extra
      mongodb-org-mongos mongodb-org-server mongodb-org-shell
      mongodb-org-tools
    0 upgraded, 9 newly installed, 0 to remove and 45 not upgraded.
    Need to get 146 MB of archives.
    After this operation, 461 MB of additional disk space will be used.
    Get:1 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0/multiverse amd64 mongodb-database-tools amd64 100.5.3 [49.7 MB]
    Get:2 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0/multiverse amd64 mongodb-mongosh amd64 1.5.0 [37.2 MB]
    Get:3 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0/multiverse amd64 mongodb-org-shell amd64 5.0.9 [14.4 MB]
    Get:4 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0/multiverse amd64 mongodb-org-server amd64 5.0.9 [26.4 MB]
    Get:5 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0/multiverse amd64 mongodb-org-mongos amd64 5.0.9 [18.5 MB]
    Get:6 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0/multiverse amd64 mongodb-org-database-tools-extra amd64 5.0.9 [7,756 B]
    Get:7 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0/multiverse amd64 mongodb-org-database amd64 5.0.9 [3,540 B]
    Get:8 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0/multiverse amd64 mongodb-org-tools amd64 5.0.9 [2,892 B]
    Get:9 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0/multiverse amd64 mongodb-org amd64 5.0.9 [2,932 B]
    Fetched 146 MB in 9min 26s (258 kB/s)                           
    Selecting previously unselected package mongodb-database-tools.
    (Reading database ... 195492 files and directories currently inst
    alled.)
    Preparing to unpack .../0-mongodb-database-tools_100.5.3_amd64.de
    b ...
    Unpacking mongodb-database-tools (100.5.3) ...
    Selecting previously unselected package mongodb-mongosh.
    Preparing to unpack .../1-mongodb-mongosh_1.5.0_amd64.deb ...
    Unpacking mongodb-mongosh (1.5.0) ...
    Selecting previously unselected package mongodb-org-shell.
    Preparing to unpack .../2-mongodb-org-shell_5.0.9_amd64.deb ...
    Unpacking mongodb-org-shell (5.0.9) ...
    Selecting previously unselected package mongodb-org-server.
    Preparing to unpack .../3-mongodb-org-server_5.0.9_amd64.deb ...
    Unpacking mongodb-org-server (5.0.9) ...
    Selecting previously unselected package mongodb-org-mongos.
    Preparing to unpack .../4-mongodb-org-mongos_5.0.9_amd64.deb ...
    Unpacking mongodb-org-mongos (5.0.9) ...
    Selecting previously unselected package mongodb-org-database-tool
    s-extra.
    Preparing to unpack .../5-mongodb-org-database-tools-extra_5.0.9_
    amd64.deb ...
    Unpacking mongodb-org-database-tools-extra (5.0.9) ...
    Selecting previously unselected package mongodb-org-database.
    Preparing to unpack .../6-mongodb-org-database_5.0.9_amd64.deb ..
    .
    Unpacking mongodb-org-database (5.0.9) ...
    Selecting previously unselected package mongodb-org-tools.
    Preparing to unpack .../7-mongodb-org-tools_5.0.9_amd64.deb ...
    Unpacking mongodb-org-tools (5.0.9) ...
    Selecting previously unselected package mongodb-org.
    Preparing to unpack .../8-mongodb-org_5.0.9_amd64.deb ...
    Unpacking mongodb-org (5.0.9) ...
    Setting up mongodb-mongosh (1.5.0) ...
    Setting up mongodb-org-server (5.0.9) ...
    Adding system user `mongodb' (UID 128) ...
    Adding new user `mongodb' (UID 128) with group `nogroup' ...
    Not creating home directory `/home/mongodb'.
    Adding group `mongodb' (GID 136) ...
    Done.
    Adding user `mongodb' to group `mongodb' ...
    Adding user mongodb to group mongodb
    Done.
    Setting up mongodb-org-shell (5.0.9) ...
    Setting up mongodb-database-tools (100.5.3) ...
    Setting up mongodb-org-mongos (5.0.9) ...
    Setting up mongodb-org-database-tools-extra (5.0.9) ...
    Setting up mongodb-org-database (5.0.9) ...
    Setting up mongodb-org-tools (5.0.9) ...
    Setting up mongodb-org (5.0.9) ...
    Processing triggers for man-db (2.10.2-1) ...
  • 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 22.04

  • 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
  • You have reached the end of the article, Congratulations. You have learned how to install MongoDB on Ubuntu 22.04.

Other Tutorials

Install MariaDB 10.7 on Ubuntu 18.04

Install MariaDB 10.7 on CentOS 7

Install PHP 7.4 on CentOS 7

System administrator | Software Developer | DevOps

5 thoughts on “Install MongoDB on Ubuntu 22.04”

  1. I keep getting this error! Fresh install 22.04, not sure what the issue may be.

    The following packages have unmet dependencies:
    mongodb-org-mongos : Depends: libssl1.1 (>= 1.1.1) but it is not installable
    mongodb-org-server : Depends: libssl1.1 (>= 1.1.1) but it is not installable
    mongodb-org-shell : Depends: libssl1.1 (>= 1.1.1) but it is not installable
    E: Unable to correct problems, you have held broken packages.

    Reply

Leave a Comment