Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

Install Mongo with WSL for Windows.

Home

This doc will guide you through installing Mongo DB using WSL through the Command Line.

Most of the steps are listed out here, but this guide will trim them down and make it more straight forward for our needs. There is also 1 step that is not in the link above as well, which will be noted when we come across it.

Install MongoDB Community Edition.

  1. Follow the directions listed on the mongodb official website for your distro of choice.

Add the data/db/ directories. NOTE: This step is not included in the link above.

  1. Make sure you are still on the root of the Ubuntu FS by typing cd ~. If you type pwd it should output /home/<user>/
  2. Type mkdir -p data/db
  • This will make a data directory with a db sub directory.
  • The -p flag means make the parent directory if it doesn't exist.

Run mongod server and mongo shell.

  1. Open a new WSL window and type mongod --dbpath ~/data/db.
  • You should see a bunch of stuff pop up, but the last line should be something like waiting for connections on port 27017.
  • This will run your mongod service for you, allowing users to connect to it.
  • The command --dbpath will change the path where mongo saves your databases and records. You can choose your own location if you want, but in the section above we just added it to the root of your Ubuntu File System.
  • Tip: You can add an alias to your .bashrc or .zshrc to make a shortcut for the command in step 1: alias mongostart="mongod --dbpath ~/data/db
  1. Open a new WSL window and type mongostart.
  • The server should start and output several lines to indicate that it is running. This terminal needs to be kept open for the mongodb server to run.
  • To enter the mongo shell, you can now open a new terminal and type mongo which will launch the new terminal into the shell.
  • In the new window you should see some messages pop up and finally your command line should be changed to a > symbol which means that you are in the mongo shell.
  1. Follow this link to test some commands in your new mongo database.
  2. To exit the shell, press ctrl + c. You should get a neat message, then you'll be returned to your command line!
@scotthernandez

This comment has been minimized.

Copy link

scotthernandez commented Jun 23, 2018

I found this gist following a WSL issue about starting mongodb.

It is not a good idea to run mongodb as root, or for that matter anything you don't need to. I'd suggest changing this doc to not have data/db be owned by root, nor start mongod as root. There is no need for sudo at all once you have installed the binaries, since all data will be in the home directory of the current users...

@GK-Hynes

This comment has been minimized.

Copy link

GK-Hynes commented Oct 14, 2018

I got as far as step 5 and then ran into this error:

E: Type '"deb' is not known on line 1 in source list /etc/apt/sources.list.d/mongodb-org-3.4.list
E: The list of sources could not be read.

Any suggestions?

@w-biggs

This comment has been minimized.

Copy link

w-biggs commented Nov 21, 2018

service mongod start still doesn't work after following these steps, unfortunately.

@deavy

This comment has been minimized.

Copy link

deavy commented Nov 23, 2018

You can also run mongod service with sudo mongod --fork --config /etc/mongod.conf command.

@DavidCannon

This comment has been minimized.

Copy link

DavidCannon commented Jan 17, 2019

I was getting dependency errors on step 6 when using Mongo 3.6 in step 4. So, I changed it to Mongo 4.0, and it resolved it for me and installed successfully. If you're having a similar issue, try changing step 4 to use version 4.0 (or the latest version), like this:

echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/4.0 multiverse" | sudo t
ee /etc/apt/sources.list.d/mongodb-org-4.0.list
@pikadun

This comment has been minimized.

Copy link

pikadun commented Mar 29, 2019

You can also run mongod service with sudo mongod --fork --config /etc/mongod.conf command.

how to restart it?

@rgalbo

This comment has been minimized.

Copy link

rgalbo commented Aug 10, 2019

i get the error message aborted (core dumped)1 when i try running service mongod start or sudo mongod --dbpath ~/data/db

@Mikeysax

This comment has been minimized.

Copy link
Owner Author

Mikeysax commented Aug 12, 2019

FYI, I've updated the instructions to be more streamlined.

For anyone looking here, you cannot start mongodb with the service command in WSL. You need to use the command above to start the server. You can make an alias to run the command so you do not have to type in the full command each time by editing your ~/.bashrc, or if you use zsh, ~/.zshrc, and write the following:

alias mongostart="mongod --dbpath ~/data/db

Save the file and reload the .bashrc or .zshrc by typing: source ~/.bashrc or source ~/.zshrc.

You can now start mongodb with the mongostart command.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.