Install MongoDB Community Edition 4.0 on Linux

MongoDB is an open source no-schema and high-performance document-oriented NoSQL database (NoSQL means it doesn’t provide any tables, rows, etc.) system much like Apache CouchDB. It stores data in JSON-like documents with dynamic schema’s for better performance.

MongoDB Packages

Following are the supported MongoDB packages, comes with own repository and contains:

  1. mongodb-org – A metapackage that will install following 4 component packages automatically.
  2. mongodb-org-server – Contains the mongod daemon and releated configuration and init scripts.
  3. mongodb-org-mongos – Contains the mongos daemon.
  4. mongodb-org-shell – Contains the mongo shell.
  5. mongodb-org-tools – Contains the MongoDB tools: mongo, mongodump, mongorestore, mongoexport, mongoimport, mongostat, mongotop, bsondump, mongofiles, mongooplog and mongoperf.

In this article, we will walk you through the process of installing MongoDB 4.0 Community Edition on RHEL, CentOS, Fedora, Ubuntu and Debian servers with the help of official MongoDB repository using .rpm and .deb packages on 64-bit systems only.

Step 1: Adding MongoDB Repository

First, we need to add MongoDB Official Repository to install MongoDB Community Edition on 64-bit platforms.

On Red Hat, CentOS and Fedora

Create a file /etc/yum.repos.d/mongodb-org-4.0.repo to install MongoDB directly, using yum command.

# vi /etc/yum.repos.d/mongodb-org-4.0.repo

Now add the following repository file.

name=MongoDB Repository

On Ubuntu Systems

MongoDB repository only provides packages for 18.04 LTS (bionic), 16.04 LTS (xenial) and 14.04 LTS (Trusty Tahr) long-term supported 64bit Ubuntu releases.

To install MongoDB Community Edition on Ubuntu, you need to first import the public key used by the package management system.

$ sudo apt-key adv --keyserver hkp:// --recv 9DA31620334BD75D9DCB49F368818C72E52529D4

Next, create a MongoDB repository file and update the repository as shown.

On Ubuntu 18.04
$ echo "deb [ arch=amd64 ] bionic/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list
$ sudo apt-get update
On Ubuntu 16.04
$ echo "deb [ arch=amd64,arm64 ] xenial/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list
$ sudo apt-get update
On Ubuntu 14.04
$ echo "deb [ arch=amd64 ] trusty/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list
$ sudo apt-get update

On Debian Systems

MongoDB repository only provides packages for 64-bit Debian 9 Stretch and Debian 8 Jessie, to install MongoDB on Debian, you need to run the following series of commands:

On Debian 9
$ sudo apt-key adv --keyserver hkp:// --recv 9DA31620334BD75D9DCB49F368818C72E52529D4
$ echo "deb stretch/mongodb-org/4.0 main" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list
$ sudo apt-get update
On Debian 8
$ sudo apt-key adv --keyserver hkp:// --recv 9DA31620334BD75D9DCB49F368818C72E52529D4
$ echo "deb jessie/mongodb-org/4.0 main" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list
$ sudo apt-get update

Step 2: Installing MongoDB Community Edition Packages

Once the repo installed, run the following command to install MongoDB 4.0.

# yum install -y mongodb-org               [On RPM based Systems]
$ sudo apt-get install -y mongodb-org      [On DEB based Systems]

To install a particular MongoDB release version, include each component package individually and add the version number to the package name, as shown in the following example:

-------------- On RPM based Systems --------------
# yum install -y mongodb-org-4.0.6 mongodb-org-server-4.0.6 mongodb-org-shell-4.0.6 mongodb-org-mongos-4.0.6 mongodb-org-tools-4.0.6

-------------- On DEB based Systems --------------
$ sudo apt-get install -y mongodb-org=4.0.6 mongodb-org-server=4.0.6 mongodb-org-shell=4.0.6 mongodb-org-mongos=4.0.6 mongodb-org-tools=4.0.6

Step 3: Configure MongoDB Community Edition

Open file /etc/mongod.conf and verify below basic settings. If commented any settings, please un-comment it.

# vi /etc/mongod.conf
path: /var/log/mongodb/mongod.log

Note: This step is only applicable for Red Hat based distributions, Debian and Ubuntu users can ignore it.

Now open port 27017 on the firewall.

-------------- On FirewallD based Systems --------------
# firewall-cmd --zone=public --add-port=27017/tcp --permanent
# firewall-cmd --reload

-------------- On IPtables based Systems --------------
# iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 27017 -j ACCEPT

Step 4: Run MongoDB Community Edition

Now it’s time to start the mongod process by issuing the following command:

# service mongod start
$ sudo service mongod start

You can make sure that the mongod process has been started successfully by verifying the contents of /var/log/mongodb/mongod.log log file for a line reading.

2019-03-05T01:33:47.121-0500 I NETWORK  [initandlisten] waiting for connections on port 27017

Also you can start, stop or restart mongod process by issuing the following commands:

# service mongod start
# service mongod stop
# service mongod restart

Now enable mongod process at system boot.

# systemctl enable mongod.service     [On SystemD based Systems]
# chkconfig mongod on                 [On SysVinit based Systems]

Step 5: Begin using MongoDB

Connect to your MongoDB shell by using following command.

# mongo

Command Ouput :

MongoDB shell version v4.0.6
connecting to: mongodb://
Implicit session: session { "id" : UUID("70ffe350-a41f-42b9-871a-17ccde28ba24") }
MongoDB server version: 4.0.6
Welcome to the MongoDB shell.

This command will connect to your MongoDB database. Run the following basic commands.

> show dbs
> show collections
> show users
> use <db name>
> exit

Step 6: Uninstall MongoDB Community Edition

To completely uninstall MongoDB, you must delete the MongoDB applications, configuration files and directories contains any data and logs.

The following instructions will walk through you the process of removing MongoDB from your system.

On RHEL, CentOS and Fedora

# service mongod stop
# yum erase $(rpm -qa | grep mongodb-org)
# rm -r /var/log/mongodb
# rm -r /var/lib/mongo

On Debian and Ubuntu

$ sudo service mongod stop
$ sudo apt-get purge mongodb-org*
$ sudo rm -r /var/log/mongodb
$ sudo rm -r /var/lib/mongodb

For more information visit official page at

Best Affordable Linux and WordPress Services For Your Business
Outsource Your Linux and WordPress Project and Get it Promptly Completed Remotely and Delivered Online.

If You Appreciate What We Do Here On TecMint, You Should Consider:

  1. Stay Connected to: Twitter | Facebook | Google Plus
  2. Subscribe to our email updates: Sign Up Now
  3. Get your own self-hosted blog with a Free Domain at ($3.45/month).
  4. Become a Supporter - Make a contribution via PayPal
  5. Support us by purchasing our premium books in PDF format.
  6. Support us by taking our online Linux courses

We are thankful for your never ending support.

Ravi Saive

I am Ravi Saive, creator of TecMint. A Computer Geek and Linux Guru who loves to share tricks and tips on Internet. Most Of My Servers runs on Open Source Platform called Linux. Follow Me: Twitter, Facebook and Google+

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide

You may also like...

7 Responses

  1. amruta says:

    while loading the packeges.,m getting the error 256…what it is..? plz give me solution for this

  2. srinu chilukuri says:

    i’m following your article to install mongo db in centos 6.2 installation completed ,
    but when iam start or restart mongodb using following commands:

    /etc/init.d/mongod start (or) sudo service mongod start
    /etc/init.d/mongod restart (or)sudo service mongod restart

    it will not start or restart getting error :”Starting mongod: [FAILED]”,

  3. Vinod says:


    Thanks for the excellent article. One small correction in the following command
    chkconfig –levels 235 mongod
    It should be
    chkconfig –levels 235 mongod on
    Otherwise it will throw warnings.

  4. Anshul Sisodia says:

    Sometimes this happens because packages were not downloaded properly or are corrupted on the website link. Also, check the repo file you created, it might be sometimes the problem of your http server.

  5. TJ says:


    I have followed the instructions, but I am still getting package not available? I am running CentOS 6.3. Any ideas why? I have tried yum update -y and clean up.


Leave a Reply to TJ Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.