How to Install MongoDB Community Edition on Ubuntu Linux 20.04/18.04 LTS

MongoDB is an open-source, schema-free document-oriented & cross-platform, NoSQL database. It stores data in collections of JSON-like flexible documents and used to create powerful websites and applications. MongoDB is available on default Ubuntu repositories, however the version in Ubuntu repositories are not the latest one. We need to install MongoDB package repository on Ubuntu to install the latest MongoDB package. In this post, we will guide you, MongoDB Community Edition installation steps using MongoDB package repository. 
Note: MongoDB supports the 64bit platforms and also supports ARM64 and s390x architecures on select platforms only.

  1. Ubuntu 20.04/18.04 running instance
  2. sudo or root privileges 
  3. Internet to download packages
Note: MongoDB package provided by Ubuntu is not maintained by MongoDB Inc. and it may conflict with “mongodb-org” package. you must uninstall the mongodb package from Ubuntu system if it is already installed.

Following are the step-by-step commands to list and remove installed MongoDB packages from Ubuntu System (Click on picture to enlarge it).

$ sudo dpkg-query -l mongodb*
$ sudo systemctl mongod stop
$ sudo apt purge mongodb-org*
$ sudo rm -r /var/log/mongodb

$ sudo rm -r /var/lib/mongodb

Install MongoDB Community Edition


Follow the below steps to install MongoDB Community Edition using apt package manager


Step 1: Import the MongoDB public GPG key (Click on picture to enlarge it).


$ sudo wget -qO – | sudo apt-key add –

Note: Install gnupg with below command and it’s libraries if you get gnupg is not installed message on console.


$ sudo apt install gnupg
Import key once again as shown above on Step 1
Step 2: Create a source.list file


Create the “/etc/apt/sources.list.d/mongodb-org-4.4.list” file for Ubuntu 20.04 (Click on picture to enlarge it).
$ sudo echo “deb [ arch=amd64,arm64 ] focal/mongodb-org/4.4 multiverse” | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list

[For Ubuntu 18.04] 
Create the “/etc/apt/sources.list.d/mongodb-org-4.4.list” file

$ sudo echo “deb [ arch=amd64,arm64 ] bionic/mongodb-org/4.4 multiverse” | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
Step 3: Update Ubuntu system
$ sudo apt update

Step 4: Install MongoDB on Ubuntu
Execute the following command to install MongoDB stable version (Click on picture to enlarge it).
$ sudo apt install -y mongodb-org

Step 5: Directories and Configuration files
Followings are the data and log file location if the MongoDB is installed using package manager (Click on picture to enlarge it).
/var/lib/mongodb —> Data Directory
/var/log/mongodb —> Log Directory
/etc/mongod.conf —> Configuration file
/var/log/mongodb/mongod.log —> Log file
By default MongoDB runs with “mongodb” user. Change or modify the permission of log and data directories for mongodb user if necessary.

Step 6: Manage “mongod service”
Following commands to start|enable|restart|status|stop “mongod service”. By default “mongod.service” is stopped state, start the service shown in below (Click on picture to enlarge it).
$ systemctl start mongod.service
$ systemctl enable mongod.service
$ systemctl restart mongod.service
$ systemctl status mongod.service
$ systemctl stop mongod.service
Note: By default MongoDB is listening on port 27017

Step 8: Start using MongoDB 
Access the mongo shell:
$ mongo
Quit from mongo shell
>quit ()

That’s it! MongoDB installation on Ubuntu 20.04 LTS is complete. Please share it with others and use comment box below to raise a query if any. Also follow us on tecluesdotcom Facebook Official Page.


Leave a Comment