Installing MongoDB on Windows Subsystem for Linux (WSL) 2

Sean Welsh Brown - Nov 12 '20 - - Dev Community

MongoDB 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 structure, and provides dynamic and scalable performance across the board.

Windows Subsystem for Linux, or WSL 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 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

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

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

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

Whereas the root directory lives here:

Alt Text

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

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

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

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 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! 😄

. . . . . . . . . . . . . . . . . . . . .
Terabox Video Player