Installing MongoDB on Windows Subsystem for Linux (WSL) 2
Tags: wsl, mongodb, tutorial, webdev
Published: 2020-11-12 01:43:17

[MongoDB](https://www.mongodb.com/) is one of the most popular databases for modern Web Development, especially for those using JavaScript and JSON in their applications. It fits well into the [MERN Stack](https://www.geeksforgeeks.org/mern-stack/) structure, and provides dynamic and scalable performance across the board.

[Windows Subsystem for Linux, or WSL](https://en.wikipedia.org/wiki/Windows_Subsystem_for_Linux) is a service provided to developers that allows a Windows 10 user to have a full Linux kernel installed and running in a virtual machine under the hood, allowing for access to a complete Unix filesystem and command line while continuing to use Windows GUI apps.

While WSL provides a potentially fantastic service for developers (I use it extensively, myself), its nature of being a bit more of an experimental feature can sometimes leave documentation for specific tasks a bit hard to find. It's a Linux terminal, yes, but one running with specific concessions that occasionally make installing applications not as simple as the "Ubuntu installation instructions" might make them seem when you're Googling for a solution.

I'm writing this blog post because I specifically had a little trouble getting **MongoDB** installed and running on my WSL 2 installation, and wanted to put something out there to help others who might be trying to do the same thing.

Let's get started!

#Installing MongoDB

First off, I'm going to assume that you're using Ubuntu as your WSL installation of choice. This is the most popular and commonly used Linux distro, and the one that I have experience with.

My examples will also be using [Windows Terminal](https://www.microsoft.com/en-us/p/windows-terminal/9n0dx20hk701?activetab=pivot:overviewtab) for my command line interaction, which I highly recommend for WSL. It's lightweight and customizable, and fits great with the workflow.

## Step 1.) Update Ubuntu packages

This is just to make sure everything is up to date in your WSL installation. Run `sudo apt update` in your terminal, enter your Ubuntu password, and let the process finish.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/7j1z42f2xzfszoq7m9th.jpg)

## Step 2.) Install MongoDB on the command line

Install the current working package version of MongoDB by typing the following into your terminal:

`sudo apt-get install mongodb`

Type `Y` to confirm when prompted, and a big installation process will take place.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/psaji5ymi9lbv9vo8uum.jpg)

## Step 3.) Get familiar with MongoDB services

Once the installation is complete, you should now have access to two services on your command line:

1. `mongod`
2. `mongo`


`mongod` starts your local database server and runs it on the default port (27017), and `mongo` runs the MongoDB shell-- allowing you to connect with the database and interact with it manually.

## Step 4.) Set up your local database

Now here's the tricky part, where the WSL version of MongoDB gets a little bit funky.

Normally, MongoDB's default directory for the database is `/data/db`. However, if you run the `mongod` command to start the server, you'll likely be hit with this error:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/cef4c3dif8jpi3ekug8h.jpg)

MongoDB hasn't created the folder for its database properly, so we're going to have to do it ourselves!

Important to note, for those new to Linux or Unix in general, your default starting place when you open up a new terminal window is `~`, which is your **home** directory. Any new folders or files you create there will show up as `~/code` or `~/pictures`, etc. This is different from the **root** directory, which is the actual core of your Linux installation. This is represented by a simple `/`, and that's the directory where MongoDB is looking for its folders.

This is a small distinction, `~/` versus `/`, but it makes *all* the difference in Unix.

To see this in the GUI, this is where the **home** folder lives in your WSL file structure:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/x9o66y9rp3b76aaqjlzo.jpg)

Whereas the root directory lives here:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ikcrbonqmo6a6fui6gzd.jpg)

And this is where we'll need to put the `/data/db` folder structure.

## Step 5.) Creating the folders and permissions

Thankfully the solution is easy, once you know what to do!

Type `cd /` into your terminal to navigate to your root directory, and type `sudo mkdir -p data/db`. The `-p` creates the parent folder(s) as well, if they don't already exist.

The folder now exists in your root directory! But we're not quite done. If you try to run `mongod` now, you'll hit a new error:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/0cjbge6t124fopzvfqlb.jpg)

This is because while we've created the folder, we now have to give it administrative *write* privileges, so that MongoDB can put its data there.

To do this, type ``sudo chown -R `id -un` data/db``. This will recursively give the proper permission to those folders.

Now, type `mongod` again to start the server, and you're done!

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/01g7sh0j80zzdbzjqqq9.jpg)

The final line in the terminal should be something like "Waiting for connections on port 27017". Your server is now officially running!

Test it out by running the MongoDB shell with `mongo` in a separate terminal tab, with the server still running:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ntxkkxr3x6gvbwk71juj.jpg)

If you see the `>` character at the end, congratulations! You're now officially inside of the MongoDB shell and can interact with your local database.

***

I'll end the post here, since this was just a tutorial to get MongoDB installed and running properly, and another post on the functionality of Mongo would be a whole lot longer. I encourage you to take a look at their [documentation](https://docs.mongodb.com/) and experiment with what they have to offer-- it's an incredible service, and a lot of fun to use!

I hope this was helpful to other WSL devs out there, and thanks for reading! 😄