Install MongoDB on Ubuntu and Windows 10

In this tutorial, you will learn how to install the latest stable version (4.4) of MongoDB on Ubuntu and Windows.

Install MongoDB Community Edition on Ubuntu

#1 First, you have to import the public key used by the package management system.

Open your terminal and run the following command to import the public key.

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

The above operation should respond with an OK. But, if you get an error – gnupg is not installed, then first install the GnuPG using the following command.

sudo apt-get install gnupg

After installing it, now retry to importing the key.

#2 Create a list file for MongoDB.

After successfully importing the key, now you have to create a list file for the MongoDB Community Edition.

Run the following command according to your system –

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

#3 Reload local package database and Install.

After creating the list, reload the local package DB and install the MongoDB.

Reload local packages

sudo apt-get update

Installing the latest version of MongoDB

sudo apt-get install -y mongodb-org

Installing the Specific version of MongoDB

sudo apt-get install -y mongodb-org=4.4.2 mongodb-org-server=4.4.2 mongodb-org-shell=4.4.2 mongodb-org-mongos=4.4.2 mongodb-org-tools=4.4.2

#4 Verify the installation

Run the following command to check the installed MongoDB version –

mongod --version

#5 Some useful commands

sudo systemctl start mongod
sudo systemctl status mongod 
sudo systemctl stop mongod
sudo systemctl restart mongod
mongo

Uninstall MongoDB on Ubuntu

Follow the below steps to completely uninstall or remove the MongoDB Community Edition from your Ubuntu system.

1. First Stop the MongoDB.

sudo service mongod stop

2. Remove the MongoDB Packages.

sudo apt-get purge mongodb-org*

3. Remove MongoDB databases and log files.

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

Install on Windows

First, download the MongoDB community server from the official site.

Mongodb Download
download mongodb for windows

After downloading, double click on the installer to start the installation.

Now click on the Next > accept the terms and then Next.

next to install mongodb
Accept terms next install mongodb

Click on the custom and choose the path (where you want to install) or you can leave it as the default. Click Next –

custom install mongodb
set mongodb install path

Uncheck the install MDB compass then click on the Next > Install > Finish.

Uncheck install mongodb compass
start install mongodb
Installing
mongodb Installation finished

Set Environment Variable

After successfully installing the MongoDB, Now you have to set the MongoDB bin path to the environment variables.

1. Go to the MongoDB installation folder and then go to the server > 4.4 > bin. Copy the bin folder path

MongoDB bin path

2. After that open the Windows Environment Variables

this pc properties
windows advanced system settings
windows enviroment variables

3. Double click on the Path

enviroment path

4. Click on the New and paste the bin path and click ok.

paste bin path
ok
save

5. Now open your PowerShell and check the MongoDB version to confirm the MongoDB installation.

mongod --version
Check MongoDB version on Windows

Leave a Reply