How to install MongoDB

This article is about How to Install MongoDB. In this topic, we will cover various operating systems where how we can easily install MongoDB and start the MongoDB server. These are some of the OS that we are covering:

  • How to download MongoDB
  • How to start MongoDB server
  • How to install MongoDB in Windows
  • How to install MongoDB in Ubuntu
  • How to Install MongoDB in macOS

While talking about MongoDB, It supports many platforms, there are the following operating systems that are recommended for production use:

  • Windows
  • Ubantu
  • RHEL/ CentOS 7 and 8
  • Debian 9 and 10
  • Amazon Linux 2

First of all, we have to download the latest release of MongoDB:

How to download MongoDB

For downloading the MongoDB, You can download any version of MongoDB as per the system support.

For that, first of all, you need to check system architecture like it is 32 bit or 64 bit based upon this you can download any version.

You can download MongoDB by using this link and download any version as per your requirement.

Download MangoDB

Click on the download button you can easily download it.

How to install MongoDB in windows

Let us now understand how to install MongoDB on Windows:

In google chrome, locate the downloaded MongoDB msi file, double click on that file.

After that, from here you have to follow the instructions step by step:

  • Click on next button
Install MongoDB
  • Now, It will ask for End-User license agreement, Accept this agreement and click on next button.
Install MongoDB in Windows
  • After that, In the service Configuration part it is showing all the details, You need to simpliy Mark at Install MongoD as a service and click on next button.
MongoDB installation
  • Its time to install, So simply click on Install button to start the installation.
Install MongoDB in Windows 10
  • Installation begins, Now you have to wait for a while it will take few minutes to Install MongoDB and click next once completed.
MongoDB install in OS
  • In the final step, After installation is successfully completed. Then simply click on Finish button.
Install MongoDB in windows 8

Here, We successfully installed the latest version of MongoDB 5.0.2.

Read : How to create new database in MongoDB

How to start MongoDB server

After successful installation of MongoDB, We can simply start the MongoDB.

Now if you want to check that where MongoDB was installed you need to follow these steps

  • First of all, you have to go in Local Disk (C), where you find a folder called as program files.
  • In the program files folder you find a folder of MongoDB.
  • This is the location where MongoDB installed.
MongoDB installed location Path

Start MongoDB server

  • Open the command prompt.
  • Now we need to go in the MongoDB bin folder location to start MongoDB server.
MongoDB installed path
  • Now, simply type “mongod” to start the server.
C:\Program Files\MongoDB\Server\4.4\bin>mongod
  • After clicking enter, it will start MongoDB server.
Start MongoDB Server
  • After starting the MongoDB server, Now again open command prompt, go to same path till bin folder.
  • Now type command “mongo“, This will connect us to running MongoDB instance.
C:\Program Files\MongoDB\Server\4.4\bin>mongo
  • After this we can successfully start working on MongoDB database.
how we can start mongodb server in windows

The latest version of MongoDB is 5.0 that was released on 13 July 2021. Here, we can also check the version of MongoDB with the help of this command

C:\Program Files\MongoDB\Server\4.4\bin> mongod --version
how to start mongodb server

Here you can see, we are using MongoDB’s latest version 5.0.2 and we can also check some build information git version, modules, and environment like which operating system we are using.

In this, we learn how we start the MongoDB server in Windows using the command prompt.

Read: MongoDB shutting down with code 100

How to install MongoDB in ubuntu

In this topic, we will learn how to install MongoDB in ubuntu and start the server. We can also install different versions of the MongoDB community as per our requirements.

For installing MongoDB in Ubuntu first, we need to install prerequisite packages. So, install the packages by using this command:

sudo apt-get install libcurl4 openssl liblzma5

Now, download the MongoDB by using this link:

After clicking on this link, you will go to the official website of MongoDB from where you can download

  • From version dropdown, select the version of MongoDB that you want to download.
  • From platfrom dropdown, select your opearing system version and architecture.
  • From package dropdown, choose tgz.
  • Now, click on download button.
How to download MongoDB in ubuntu

After Successfully downloading, we need to follow some steps to install in our machine:

  • With the help of tar command extract the files.
tar -zxvf mongodb-linux-*-5.0.2.tgz

Note that, If you download different releases of MongoDB then modify the command.

  • Now copy all the binaries in your PATH environment variable.
sudo cp <mongodb-install-directory>/bin/* /usr/local/bin/

This step is optional because first, you check manually that all the binaries all listed or not. If not then with the help of this command you can do it easily.

  • Now, we need to install MogoDB shell(Mongosh) to connect deployment.
  • You can download by using this link
How to download MongoDB shell in ubuntu

After downloading, you need to follow these steps to run MongoDB:

  • Create the data and log directories
  • For storing the data, create a directory to store data
sudo mkdir -p /var/lib/mongo
  • For string the log, create a directory to store its log
sudo mkdir -p /var/log/mongodb
  • Now we need to provide the read write permission to these directories so that user can easily start MongoDB process
sudo chown `whoami` /var/lib/mongo     
sudo chown `whoami` /var/log/mongodb
  • MongoDB Run
mongod --dbpath /var/lib/mongo --logpath /var/log/mongodb/mongod.log --fork

Note that, here –dbpath means, the directory where MongoDB stores its data and –logpath means, it will send all the logging information to a log file instead of to the host syslog system.

  • MongoDB started successfully and we can also checking the process output by following line in the log file: /var/log/mongodb/mongod.log:
[initandlisten] waiting for connections on port 27017
  • We can start using MongoDB by using this command
mongosh

This command will connect to a mongod that is running on localhost with default port 27017.

In this, We learned how we can install MongoDB in Ubuntu and how easily we can run this in our local system.

Read: How does MongoDB create collection

How to install MongoDB in macOS

In this topic, we will learn how to install MongoDB in macOS and start the server. We can also install different versions of the MongoDB community as per our requirements.

macOS 10.14 or later, we can use for MongoDB 5.0 Community Edition. Now, follow these steps to install MongoDB :

  • First of all, download the MongoDB by using this link.

After clicking on this link, you will go to the official website of MongoDB from where you can download

  • From version dropdown, select the version of MongoDB that you want to download.
  • From platfrom dropdown, select macOS.
  • From package dropdown, choose tgz.
  • Now, click on download button.
How to download MongoDB in macOS
  • Extract (unzips) the file by using this command
tar -zxvf mongodb-macos-x86_64-5.0.tgz
  • Now, check all the binaries in a PATH environment variable, if not then all are binaries are in bin/ directory
  • In the PATH variable, copy all the binaries by usig this command
sudo cp /path/to/the/mongodb-directory/bin/* /usr/local/bin/
  • The directory listed in your PATH variable, create a symbolic links to the binaries
sudo ln -s  /path/to/the/mongodb-directory/bin/* /usr/local/bin/

Follow these steps to Run MongoDB Community Edition:

  • First create the data directory
sudo mkdir -p /usr/local/var/mongodb

You must have to create a directory so that mongod will write the data.

  • After the data directory, you must have to create the log directory in mongod process write its log file
sudo mkdir -p /usr/local/var/log/mongodb
  • We also need to provide user account mongod toread and write permission for these two directories
sudo chown my_mongodb_user /usr/local/var/mongodb
sudo chown my_mongodb_user /usr/local/var/log/mongodb

Note that, if you are running mongod your own user account and all also create above two directories then you don’t need to provide the ownership because they already having the permission to access the user.

  • Run mongod process at the system prompt to run MongoDB by using this command
mongod --dbpath /usr/local/var/mongodb --logpath /usr/local/var/log/mongodb/mongo.log --fork
  • On system prompt, run mongod with configuration file with config parameter
mongod --config /usr/local/etc/mongod.conf
  • With the help of this command we can verify that MongoDB has successfully started
ps aux | grep -v grep | grep mongod

Note that, if the mongod process is not running, then you can check the log file for any error.

  • We can start using MongoDB by using this command
mongosh

After executing this comm This command will connect to a mongod that is running on localhost with default port 27017.

In this, We learned how we can install MongoDB in macOS and how easily we can run this in our local system.

You may like reading the following articles.

At the end of this article, you might be aware of How you can install MongoDB in different operating systems. The following topics we covered in this article:

  • How to download MongoDB
  • How to start MongoDB server
  • How to install MongoDB in Windows
  • How to install MongoDB in Ubuntu
  • How to Install MongoDB in macOS