How to Install MongoDB on Ubuntu & macOS

Themomsco [CPS] IN

Introduction to MongoDB

MongoDB is an open-source, NoSQL document database designed for development. This guide introduces the simple steps of how to install MongoDB on your server for application development.

MongoDB Key Features

  • High Performance – MongoDB supports embedded data models as well as indexes that help to faster queries.
  • Rich Query Language – MongoDB provides rich query language support, eg – CRUD Operation, Search & Data Aggregation.
  • Support for Multiple Storage Engines – MongoDB supports multiple storage engines, eg – WiredTiger Storage Engine, In-Memory Storage Engine, MMAPv1 Storage Engine. It also provides storage engine API that can be used to develop our own storage engines.
  • High Availability – MongoDB has a facility to replicate the data, It means you can create the group of a server that maintains the same data set and in return, you get redundancy & benefit of increased data availability.
  • Horizontal Scalability – MongoDB provides horizontal scalability, Sharding is a great example of horizontal scalability


Make sure you have a running computer with Ubuntu installed and a sudo non-root user.

Install MongoDB on Ubuntu

Step 1 — Add MongoDB Repository

MongoDB already comes with Ubuntu package repositories but the MongoDB provide by Ubuntu is not maintained by official MongoDB.

So it is advised to use only official MongoDB because they provide the best-recommended way and up to date version of MongoDB software.

NOTE: To check if MongoDB package is installed or not on your ubuntu system, simply run the command sudo apt list --installed | grep mongodb

Import the public key used by the Ubuntu package management system ((i.e. dpkg and apt)

Run the command shown below to import the MongoDB public GPG Key:

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

Now, run the following command to create a list file for MongoDB. So that apt will know, from where to download the packages.

echo "deb [ arch=amd64,arm64 ] xenial/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list

After adding the repository, run the following command to update the local package database

sudo apt-get update

Step 2 — Install the MongoDB

To install the latest stable version of MongoDB use the following command. This commands install several packages with helpful tools for the MongoDB server.

sudo apt-get install -y mongodb-org

To install a specific release of MongoDB, you can specify any available version of MongoDB. Please see the command below:

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

Step 3: Launch MongoDB as a Service on Ubuntu

Start MongoDB — After successful installation now it’s time to start MongoDB server and to do that run the following command.

sudo service mongod start

Stop MongoDB — Run the following command to stop MongoDB.

sudo service mongod stop

Restart MongoDB — To restart MongoDB run the following command.

sudo service mongod restart

Step 4: Connect using MongoDB

Open MongoDB shell on your host machine by running the below command:


This opens the MongoDB shell. Now, check if the mongo shell is working fine.

Show existing databases by typing the below command:

show dbs

You can add or switch to the new database using the following command:

use userdatabase

You can also create the new user right from the MongoDB shell by using the following command:

db.createUser({user:"admin", pwd:"password", roles:[{role:"root", db:"userdatabase"}]})

Install MongoDB on macOS

We are going to use Homebrew to install MongoDB latest version on macOS systems.

Make sure you have macOS versions 10.11 and later on Intel x86-64 because MongoDB does not support below version. You can check out the supported platforms for your information.

Now, we assume that you have Homebrew installed on your system. If not, install brew using the instructions here.

After successful installation of brew, open the terminal and add the custom tap in a macOS terminal session by using the following command:

$ brew tap mongodb/brew

Once the tap has is added locally, you can install MongoDB latest software packages using the command below:

$ brew install mongodb-community

You can also install a specific version of MongoDB by following the command, It will install the MongoDB 4.0 version:

$ brew install mongodb-community@4.0

Run MongoDB on macOS

From the terminal run the following command to start mongod process in foreground.

$ mongod --config /usr/local/etc/mongod.conf

You can run MongoDB as a service by following the below command:

$ brew services start mongodb-community

To stop the MongoDB server follow the command below:

$ brew services stop mongodb-community

Connect and Use MongoDB on macOS

To start using MongoDB, you need to connect a mongo shell to the running instance. Open the terminal and run the following command:


Uninstall MongoDB

To uninstall and completely remove the MongoDB packages from your system, follow the necessary steps that are required.

1. Stop MongoDB

The First step is to stop the mongod process by running the following command:

sudo service mongod stop

2. Remove Packages

The second step is to remove any MongoDB packages that you have installed on your system by issuing the command below.

sudo apt-get purge mongodb-org*

3. Remove Data Directories

To completely remove the databases and log files use the command shown below.

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


Finally, the MongoDB installation guide is over. If you want in-depth details and instruction on how to install MongoDB, you can find it here.

Want to learn Angular? you can start from here.


Themomsco [CPS] IN

Leave a Reply