2. Installing MongoDB
Before you start working with MongoDB, it is crucial to install it on your system. This e-book chapter will guide you through the process of installing MongoDB on different operating systems, including Windows, MacOS, and Linux. Let's start with Windows.
2.1 Installation on Windows
To install MongoDB on Windows, follow the steps below:
- Visit the official MongoDB website at https://www.mongodb.com/try/download/community.
- Choose the version of MongoDB you want to download. For beginners, it is recommended to choose the latest stable version.
- Select your operating system, which in this case is Windows.
- Click the "Download" button. The MongoDB installation file download should begin immediately.
- After downloading the file, double-click it to begin the installation process.
- Follow the installation wizard instructions. During installation, you can choose the location where you want to install MongoDB. You can also leave the default option.
- Once the installation is complete, you can verify that MongoDB was installed correctly by opening the command prompt and typing "mongod". If the installation was successful, you will see a message indicating that the MongoDB server is running.
2.2 Installation on MacOS
To install MongoDB on MacOS, you can use the Homebrew package manager. Here are the steps:
- Open the terminal.
- If you don't have Homebrew installed, you can install it by typing the following command in the terminal:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/ install/master/install.sh)"
- After installing Homebrew, you can install MongoDB by typing the following command:
brew tap mongodb/brew
and thenbrew install mongodb-community
- To start MongoDB, type the following command:
brew services start mongodb-community
- To check if MongoDB is working correctly, you can type "mongo" in the terminal. If MongoDB was installed correctly, you will see a message indicating that the MongoDB server is running.
2.3 Installation on Linux
Installing MongoDB on Linux varies depending on the distribution you are using. For this tutorial, we will focus on installing MongoDB on Ubuntu.
- Open the terminal.
- First, you need to import the public key used by the package management system. Enter the following command:
wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
- Create a list of files for MongoDB. Enter the following command:
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
- Update the local database of available packages. Type the following command:
sudo apt-get update
- Install MongoDB. Type the following command:
sudo apt-get install -y mongodb-org
- To start MongoDB, type the following command:
sudo systemctl start mongod
- To check if MongoDB is working correctly, you can type "mongo" in the terminal. If MongoDB was installed correctly, you will see a message indicating that the MongoDB server is running.
With this, we have completed the MongoDB installation section. In the next chapter, we will learn how to configure MongoDB and start working with it.