Request-Response

The Full-Stack Blog

How to Install MongoDB

November 14, 2023
Disponible en español

MongoDB is a general purpose, document-based, distributed NoSQL database built for web application developers in the cloud era. It’s one of the most popular database choices for modern applications.

To install MongoDB on your machine, follow the instructions for your operating system below. Some of the images and versions mentioned below may be slightly different from the MongoDB web site, but the general instruction is the same.

Install MongoDB on Windows

Refer to the following instructions if you’re using Windows; scroll down for instructions for macOS.

MongoDB’s Install Documentation is a great place to start when installing MongoDB. It’s updated constantly, and it will provide you with the most recent changes to any part of the installation process. We’ve also provided step-by-step instructions here.

Create a Data Directory for MongoDB

Important: The first step is to create a data directory to store MongoDB’s databases. Without this, you won’t be able to complete the installation.

Open git Bash, then cd into the root directory (c: drive) of your computer:

cd /c

Once you’re in the root directory, run the following command to create a data/db directory:

mkdir -p data/db

This directory will be the default location for MongoDB’s databases. Without a directory for your databases, MongoDB will not run.

Now you can download and install MongoDB.

Download and Install MongoDB on Windows

To install MongoDB on your Windows machine, download the MongoDB installer.

After the installer has downloaded, open it to begin the installation process. Follow the prompts by clicking Next in the Setup Wizard, as shown in the following image:

Welcome screen for the MongoDB setup wizard with the Next button active at the bottom.

Note: A window might appear mid-installation, asking you whether you’re sure you want to download a particular component of MongoDB. Click yes; otherwise, the install will fail.

You’ll also need to accept the terms in the License Agreement before you can continue. When the installer prompts you to Choose Setup Type, click Complete, as shown in the following image:

Choose Setup Type window with Complete button and Custom button. The Complete button is active.

On the next screen, ensure that the checkbox for “Install MongoD as a Service” is checked and that the option “Run Service as Network Service user” is selected. These options should already be selected. Leave everything the way it is and click Next, as shown in the following image:

Service Configuration window shows Install MongoD as a Service and the Next highlighted in green, indicating they should be clicked.

You should then see a screen that says Install MongoDB Compass.

Ensure that the checkbox for Install MongoDB Compass is selected and click Next, as shown in the following image:

Install MongoDB Compass window with that checkbox checked. The Next button in the lower right corner of the window is highlighted, indicating it should be clicked.

On the next screen, you’ll see a message that says “Ready to install MongoDB” followed by the version number. Click Install to finish installing MongoDB, as shown in the following image:

Ready to Install MongoDB window with Install button in lower right corner highlighted.

The last screen you’ll see will say that you’ve completed the installation. Click Finish.

Configure MongoDB on Windows

Now that you’ve installed MongoDB, you’ll need to add the PATH to the MongoDB install to your environmental variables. Follow these steps to do that:

Copy the PATH to MongoDB to your clipboard. Open any folder on your computer and click on This PC in the left-hand navigation bar, as shown in the following image:

In the navigation bar, This PC is highlighted with a green box around it.

Next, double-click your C: drive, as shown in the following image:

Under “Devices and drives,” the `C:` drive is highlighted.

Double-click the folder named Program Files, then double-click the MongoDB folder. Double-click the Server folder, then double-click the folder with the MongoDB version number as its name.

Finally, double-click the folder named bin. Your screen should look like the following image:

Windows Explorer window shows the files inside "This PC / OS (C:) / Program Files / MongoDB / Server / 6.x / bin".

Right-click the address at the top of the screen and select “Copy address as text.” You should see something like the following image.

Windows Explorer window with popup menu with “Copy address as text” highlighted.

Now that you’ve got the PATH copied to your clipboard, press the Windows key on your keyboard and search for “Edit the system environmental variables”. Once that shows up in the search screen, click it, as the following image shows:

“Edit the system environment variables” is highlighted at the top of the search results.

You’ll then be prompted with the System Properties window. Make sure that the Advanced tab is highlighted, and click on Environment Variables, as shown in the following image:

In the Advanced tab on the System Properties window, Environment Variables is highlighted in green at the bottom.

You’ll see a window entitled Environment Variables. In the top section, under “User variables for <user>” (where <user> is your username on that computer), click Path and then click “Edit...”. It should look like the following image:

On the Environment Variables window, Path is selected and highlighted, with the “Edit…” button also highlighted.

The next window to appear will be “Edit environment variable.” Click “New…”, as shown in the following image:

“New..." button is selected on the “Edit environment variable” window.

Your cursor will appear in a new line at the bottom of the variables. Paste your PATH in this box and click OK. It should look like the following image:

On the “Edit environment variable” screen, an empty box is highlighted, and OK is highlighted  at the bottom of the screen.

Click OK on any remaining screens, then restart Git Bash completely. You can verify that the installation was correct by going to Git Bash and typing the following:

mongod

Note: There is no "b" at the end of this command; just mongod.

You should see a large number of characters printed to the console, as in the following image:

Console shows lines of text.

Note: If mongod didn’t run, and instead you received a "command not found" error, make sure you added MongoDB’s bin directory to your PATH variable. Then, close out git bash and try running mongod again.

Important: If mongod starts but closes after a series of prompts, make sure you created the data/db directory in the root directory of your computer (this is the very first step in this walkthrough). MongoDB cannot run without this directory.

Install MongoDB on macOS

MongoDB’s Install Documentation is a great place to start when installing MongoDB. It is updated constantly, so it will provide you with the most recent changes to any part of the installation process. We’ve also provided the following step-by-step instructions.

Important: Ensure that you've installed Xcode and Homebrew before continuing.

To check to see if Homebrew is installed, run brew --version in your terminal window. If your terminal displays a version of Homebrew, you can move on to the installation guide. If you see an error message like command not found, run the following command in your terminal to install Homebrew:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Once you have confirmed that Homebrew is on your machine, move on to the installation guide.

Install MongoDB on macOS

To install MongoDB on macOS, run the following command:

brew tap mongodb/brew

Next, run the following installation command:

brew install mongodb-community@6.0

Note: The version number following the @ symbol may have changed. See the MongoDB tutorial on installing on macOS to make sure that you have the latest version to use with this command.

Run MongoDB as a Service on macOS

To run MongoDB as a service on macOS, follow these steps:

Run the following command in your terminal:

brew services start mongodb-community@6.0

Verify that the service is running by using the following command:

ps aux | grep -v grep | grep mongod

If you don't see any output from the command above, try restarting the service by running the following command in your terminal:

brew services restart mongodb-community

Install MongoDB Compass on macOS

Now that you’ve installed MongoDB, you are ready to install Compass. MongoDB Compass is a powerful graphic user interface (GUI) that allows you to query and visually explore a MongoDB database as well as use an embedded MongoDB shell.

Navigate to MongoDB Compass's Download and Install Documentation to download and install Compass for your machine.

Select your operating system and open the Downloads page.

On the Downloads page, select the most recent stable version that is compatible with your operating system and click on the Download button.

Follow instructions on your local machine to complete the download process and open Compass.

This page was updated 4 months ago
© 2022 edX Boot Camps LLC. Confidential and Proprietary. All Rights Reserved.

Category: MongoDB

Tagged under: mongodb, nosql, installation, guide, compass,

All Posts