Installing MongoDB

Installing using WSL (2022 Update for Windows Users)

Instructions are copied from here

To install MongoDB (version 5.0) on WSL (Ubuntu 20.04):

  1. Open your WSL terminal (ie. Ubuntu) and go to your home directory: cd ~
  2. Update your Ubuntu packages: sudo apt update
  3. Import the public key used by the MongoDB package management system: wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -
  4. Create a list file for MongoDB: echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list
  5. Reload local package database: sudo apt-get update
  6. Install MongoDB packages: sudo apt-get install -y mongodb-org
  7. Confirm installation and get the version number: mongod --version
  8. Make a directory to store data: mkdir -p ~/data/db
  9. Run a Mongo instance:sudo mongod --dbpath ~/data/db
  10. Check to see that your MongoDB instance is running with: ps -e | grep 'mongod'
  11. To exit the MongoDB Shell, use the shortcut keys: Ctrl + C
Installing using Homebrew
  1. Check if you already have Homebrew installed:
    • In a terminal window, type brew -v. If it responds by telling you the version number installed on your machine, just do a brew update. If instead you see -bash: brew: command not found, proceed to the next step.
  2. Open the Homebrew home page and follow the (one) instruction for installing Homebrew: paste the code into the terminal and hit enter.
  3. Make sure Homebrew is up-to-date (especially if you already had it installed): Run brew update to update Homebrew to the newest stable version. This might take a little while to complete.
  4. Run brew tap mongodb/brew
  1. Run the command found under the "Install MongoDB" header on this page. (At the time of writing this article, the command was brew install mongodb-community@4.2)
  2. Once brew finishes installing MongoDB, it should show a message telling you that you can start the MongoDB server running by using an upstart process (a process that runs as soon as your machine starts up) by running the command brew services start mongodb/brew/mongodb-community. If running that line doesn't give you an error, you're all set to go and can skip the next section, "Running the MongoDB Server".
Running the MongoDB server

If the above command didn't work, open a terminal window and type mongod. This window will be taken over by the MongoDB process, and you'll need to leave this running as long as you are planning on doing work with MongoDB.

When you're done using MongoDB for the time being (as in, until the next time you're developing) it's best to shut it down cleanly. Don't just close the terminal window with the mongod process running. Instead, hit Control + c to quit the process. Or you could use one of these other methods if you really want.


Conclusion

You've got MongoDB installed on your local machine now! One of the trickiest things for a beginner to grasp is the idea of the database running on its own server on its own port. In the same way a web server needs to be running to accept requests and send back responses in order to work, a database needs to be running on its own server too. So your front-end web application speaks to your back-end application, which in turn speaks to the database.

Happy developing!