MongoDB is a full flexible index support and rich queries database. Mongodb is a NoSQL database. MongoDB provides large media storage with GridFS. Click here for more details about this version of MongoDB.

Advertisement

This tutorial will help you to install MongoDB 4.4 community release on Ubuntu 20.04 LTS (Focal), 18.04 LTS (Bionic) and 16.04 LTS (Xenial) systems.

Step 1 – Setup Apt Repository

First of all, import GPK key for the MongoDB apt repository on your system using the following command. This is required to test packages before installation

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 656408E390CFB1F5

Lets add MongoDB APT repository url in /etc/apt/sources.list.d/mongodb.list.

Ubuntu 18.04 LTS:
echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb.list
Ubuntu 16.04 LTS:
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.list

Step 2 – Install MongoDB on Ubuntu

After adding required APT repositories, use the following commands to install MongoDB on your systems. It will also install all dependent packages required for MongoDB.

sudo apt update
sudo apt install mongodb-org

If you want to install any specific version of MongoDB, define the version number as below

sudo apt install mongodb-org=4.4.1 mongodb-org-server=4.4.1 mongodb-org-shell=4.4.1 mongodb-org-mongos=4.4.1 mongodb-org-tools=4.4.1

Step 3 – Manage MongoDB Service

After installation, MongoDB will start automatically. To start or stop MongoDB uses init script. Below are the example commands to do.

sudo systemctl enable mongod.service
sudo systemctl start mongod.service

Once the service started, check the status by running command:

sudo systemctl status mongod.service

Installing Mongodb on Ubuntu

Use the following commands to stop or restart MongoDB service.

sudo systemctl stop mongod.service
sudo systemctl restart mongod.service

Step 4 – Verify MongoDB Installation

Finally, use the below command to check installed MongoDB version on your system.

mongod --version 

Output:

db version v4.4.1
Build Info: {
    "version": "4.4.1",
    "gitVersion": "ad91a93a5a31e175f5cbf8c69561e788bbc55ce1",
    "openSSLVersion": "OpenSSL 1.1.1  11 Sep 2018",
    "modules": [],
    "allocator": "tcmalloc",
    "environment": {
        "distmod": "ubuntu1804",
        "distarch": "x86_64",
        "target_arch": "x86_64"
    }
}

Also, connect MongoDB using the command line and execute some test commands for checking proper working.

mongo 

> use mydb;

> db.colors.insert({ "id": 100, "color": "Pink"})
> db.colors.insert({ "id": 101, "color": "Purple"})

> db.colors.find()

  { "_id" : ObjectId("5f75a76194d0a08201f26f25"), "id" : 100, "color" : "Pink" }
  { "_id" : ObjectId("5f75a7d594d0a08201f26f26"), "id" : 101, "color" : "Purple" }

More Useful tutorials

Here is the list of useful tutorials for MongoDB server.

Share.

29 Comments

    • Hi, Ubuntu 18.10 is a nonLTS release, Which supports for a limited time. MongoDB official repo has removed this from the repository.

      You can upgrade to Ubuntu 19.04.

  1. Paras chowdhary on

    i am getting error saying that unable to locate pacage mongodb-org? how to solve this issue can any one solve this

  2. Great article! These blog is really useful for beginner’s. Nice Informative Information present on Install mogodb on ubuntu. Thanks for sharing.

  3. Hi i am trying to setup replication once i add the replication tag and try to start the service is getting failed on Ubuntu 16.04.4 LTS

    Could anyone please help

  4. Great Post..!!! This is vital blog , a debt of gratitude is in order for offering to us. It is exceptionally fascinating web journal which is useful for everybody. I value your blog on best blog. Your blog will help us a great deal. In this way, I need to express profound gratitude to you for offering this fundamental blog to us.

  5. Kelvin Morris on

    Hi, Thank you for the post. I think there’s a typo in starting service ‘systmectl’, the correct one is ‘systemctl’

      • Hello Rahul,

        I’m not able to install mongodb on my ubuntu machine.
        i have tried your steps from this blog but same error occured.
        apt error = sudo apt-key adv –keyserver hkp://keyserver.ubuntu.com:80 –recv 9DA31620334BD75D9DCB49F368818C72E52529D4
        [sudo] password for rlabhane:
        Executing: /tmp/apt-key-gpghome.zAgdHqoYjn/gpg.1.sh –keyserver hkp://keyserver.ubuntu.com:80 –recv 9DA31620334BD75D9DCB49F368818C72E52529D4
        gpg: keyserver receive failed: No data

        please give me suggestion.

  6. nice tutorial dude, thanks for sharing. because of you i am able fix the connective issue in mongodb server

  7. Please help me , I can not install mongodb 🙁

    sudo apt-get install -y mongodb-org
    Reading package lists… Done
    Building dependency tree
    Reading state information… Done
    E: Unable to locate package mongodb-org

  8. Andrew Soloninka on

    I am running Linux Mint 17.1 which is based on Umbuntu 14.04. When I try to install, I get the following error:

    Could not get lock /var/lib/apt/lists/lock – open (11: Resource temporarily unavailable)
    E: Unable to lock directory /var/lib/apt/lists/

    when I run the command: sudo apt-get update

    • delete /var/lib/apt/lists/lock file using
      sudo rm /var/lib/apt/lists/lock command and run the
      sudo apt-get update command. it willl work fine

  9. I am using Ubuntu 15.04. I have followed all the steps above and the installation was successful.
    But, when I try to start the service, I get an error:
    Failed to start mongod.service: Unit mongod.service failed to load: No such file or directory.

  10. This ‘tutorial’ is not gonna work for Debian Jessie, since it is using systemd and not the old sysvinit.
    Mongo has as yet not released a package for systemd.
    Creating a mongod.service yourself is not difficult, but I still experience issues with starting the service at boot…

    • Hi Pumpkin,

      How did you do then ? My server is based on Debian “Jessie”, and I can’t find a way to install MongoDB properly.
      No tutorials on internet, and I’m afraid to install those recommended by MongoDB-org.

      Did you find any solution ? Or any web site to recommend ?
      Cheers,

      Max

  11. Hi,
    thank you for this tutorial, but there is a problem when installing Mongo on Ubuntu 14.10 because doesn’t exists the Mongo repository for 14.10 version.

    Could you help me to resolve this problem?

    Thanks.

    • Hi Lorenzo,

      I had the similar problem on Linux Mint 17.2. I fixed that by editing the mongodb repository in the source list. You need to replace the Unbuntu’s 14.10 name to 14.04 which is trusty. My final repo looks like this:

      deb http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.0 multiverse

      Hope that helps,
      Vitali

  12. How to run and connect mongo db in browser just like for mysql we use localhost/phpmyadmin. Any suggestions will be helpful

Exit mobile version