How to Install MongoDB on Ubuntu 22.04


MongoDB is a versatile NoSQL database that operates seamlessly across different platforms. It embraces an object-oriented approach, offering a straightforward and flexible solution for data storage. Instead of traditional rows and columns, MongoDB stores data in document structures resembling JSON. These documents are saved as distinct BSON items. Applications can easily retrieve these data elements in JSON format. Developed in C++, MongoDB boasts remarkable scalability and flexibility, empowering developers with effortless querying and indexing capabilities.

In this guide, we will learn how to install MongoDB on Ubuntu 22.04 Jammy Jellyfish distribution using the command line.

Prerequisites

  • A running Ubuntu 22.04 Server
  • You should have root access or sudo privileges for running administrative installation commands.

Step-by-step installation of MongoDB on Ubuntu 22.04

To install MongoDB on Ubuntu 22.04 system, you need to perform a few steps:

Step 1: Update and upgrade System Packages

It’s recommended step, for all Linux users to update all packages of their system before installing a new software application on Ubuntu 22.04. So, update system packages using the below command:

$ sudo apt update
$ sudo apt upgrade

Step 2: Install the required dependencies

After refreshing all packages, install all necessary dependencies to install MongoDB on Ubuntu 22.04 using the following command:

$ sudo apt install wget curl gnupg2 software-properties-common apt-transport-https ca-certificates lsb-release

Step 3: Import the MongoDB GPG key

Here, we will install MongoDB on Ubuntu 22.04 from the MongoDB repository. Therefore, you need to import the MongoDB public GPG key to add the MongoDB repository to your system. So, import the MongoDB GPG key to your Ubuntu 22.04 system using this command:

$ curl -fsSL https://www.mongodb.org/static/pgp/server-6.0.asc|sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/mongodb-6.gpg

Step 4: Add and configure the MongoDB repository to Ubuntu 22.04

Now, add the MongoDB repository to your Ubuntu 22.04 using the following command:

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

Step 5: Install MongoDB on Ubuntu 22.04

First, update the system repositories and then install MongoDB on Ubuntu 22.04 using the following command:

$ sudo apt update

$ sudo apt install mongodb-org

After confirming the installation prompt, MongoDB will be installed on your Ubuntu system:

Step 6: Enable and start MongoDB service on Ubuntu 22.04

Once the MongoDB installation is completed, enable and start the MongoDB service on Ubuntu 22.04 if the service is not automatically started on your system. After enabling it on your system, you can start the MongoDB service using the following command:

$ sudo systemctl enable mongod
$ sudo systemctl start mongod

Now, verify the MongoDB running service status using this command:

$ sudo systemctl status mongod

Step 7: Check MongoDB version

Check the installed MongoDB version using the following command:

$ mongod – version

Step 8: Access MongoDB Database

By default, you don’t need a password to login into the MongoDB shell. But, it is recommended that set password to secure your MongoDB database from unauthorized users. To access the MongoDB shell, use the following command:

$ mongosh

To view MongoDB databases use this command:

$ show dbs

If you want to exit from the MongoDB shell, use this command::

$ exit

After running the above command, you again move back to the terminal command prompt.

Uninstall MongoDB from Ubuntu 22.04

To uninstall MongoDB from Ubuntu 22.04, use the following command:

$ sudo apt purge mongodb-org

Conclusion

We demonstrated in this tutorial how to install MongoDB on Ubuntu 22.04 Jammy jellyfish distribution using the command line. Furthermore, we learned how to access the MongoDB shell and display databases. In the future, we will also explore how to set a password for the MongoDB database and remote access.

If you are interested in installing MariaDB on Ubuntu 22.04. click here.

Print Friendly, PDF & Email
Categories