How to Install MongoDB in Ubuntu 22.04

To install Mongodb in Ubuntu 22.04, you just need to type the sudo apt install MongoDB-org command on the terminal and press enter button, then it will install MongoDB in your Ubuntu system.

In this tutorial guide, we will show you how to install and configure different versions of MongoDB 4.4, 5.0, 6.0, 7.0 on Ubuntu 22.04 or 20.04.

How to Install and Configure MongoDB on Ubuntu 22.04

Here are some steps to install, configure, and access Mongodb 4.4, 5.0, and 6.0 on ubuntu 22.04 or 20.04 system:

Step 1 – Update System dependencies

First you need to type this command on the terminal, which will update your system dependencies:

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

Step 2 – Add MongoDB 4.4, 5.0, 6.0, 7.0 Repository

For version 4.4, If you want to install mongoDB 4.4 version, for this, you need to import the public key and add the public key to /etc/apt/sources.list.d directory, you can do this with the command,

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

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

To mongoDB 5.0 version, for this, you need to import the public key and add the public key to /etc/apt/sources.list.d directory, you can do this with the command,

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

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

For MongoDB 6.0, for this, you need to import the public key and add the public key to /etc/apt/sources.list.d directory, you can do this with the 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

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.list

For MongoDB 7.0, for, you need to import the public key and add the public key to /etc/apt/sources.list.d directory, you can do this with the command:

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

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

Step 3 – Install MongoDB on Ubuntu 22.04

The version of MongoDB 4.4, 5.0, 6.0, and 7.0 for which you have imported the public key and added it to the directory. Now, Simply type the command sudo apt install MongoDB-org on the Ubuntu 22.04 terminal, and the Mongodb installation will start:

sudo apt install mongodb-org

Now MongoDB needs to be enabled, which you can do:

sudo systemctl enable --now mongod

Step 4 – Configuring MongoDB

Now let’s talk about the configuration of mongoDB, for its configuration there is a mongod.conf file, which you can access with sudo nano /etc/mongod.conf command and in it you can make the configuration as per your choice:

sudo nano /etc/mongod.conf

Here we will just do the configuration to enable mongoDB by adding authorization:enabled in mongo.conf file

security:
    authorization: enabled

Now restart the mongod service for changes to take effect:

sudo systemctl restart mongod

Step 5 – Creating Administrative MongoDB User

An administrative user has to be created for mongoDB, for this you can use mongo and admin commands:

mongo

Then type the following query:

use admin

The output will be:

switched to db admin

Now, execute the following command to create a new user named mongoAdmin, with password changeMe and userAdminAnyDatabase role:

db.createUser(
   {
     user: "mongoAdmin",
     pwd: "changeMe",
     roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
   }
 )

Once done, exit the mongo shell with:

quit() 

Step 6 – Restart MongoDB Server

After successfully installing and configuring MongoDB on ubuntu 20.04, You need to restart server by using the following command:

sudo systemctl restart mongod

Here is the video tutorial on how to install mongodb 4.4, 5.0, 6.0, and 7.0 on ubuntu 22.04:

Conclusion

This guide shows us how to install and configure MongoDB on Ubuntu 20.04/22.04. For more information on this topic, visit the MongoDB Manual.

Recommended Ubuntu Tutorial

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *