Installing MongoDB in WSL

I have come across this situation recently.

Even though MongoDB says that it is not supported in WSL, you can actually install it. So, I suggest you follow that steps given in MongoDB docs.

Note : If you have already installed mongodb please remove all those before you install mongodb-org since it may cause some issues during installation :

sudo dpkg --remove --force-remove-reinstreq mongo-tools
sudo dpkg --remove --force-remove-reinstreq mongodb-server-core
sudo apt-get --fix-broken install

For installing mongodb community edition, I have added the commands below:

wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt-key add -
sudo apt-get install gnupg
wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt-key add -
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.2.list

sudo apt-get update
sudo apt-get install -y mongodb-org

Now, to get mongoDB running,

sudo nano /etc/init.d/mongod

and paste the contents in this link into the file and save it.

#give permissions
sudo chmod +x /etc/init.d/mongod

#start the service
sudo service mongod start

Now, you can run mongo to reach the database.

Hope it helps.


@bonyem's solution works if you try on Ubuntu 18.04 (and apparently in 19.10) but not in 20.04. WSL version should be 2 (please see this to update WSL).

You also need to "fake" the bash binary by following the steps mentioned in https://github.com/shayne/wsl2-hacks to make systemctl work.