22

I was trying to install MongoDB in WSL running Ubuntu 18.04 after seeing this documentation. But MongoDB says that:

IMPORTANT
The mongodb package provided by Ubuntu is not maintained by MongoDB Inc. and conflicts with the official mongodb-org package. If you have already installed the mongodb package on your Ubuntu system, you must first uninstall the mongodb package before proceeding with these instructions.

MongoDB also says that WSL doesn't support mongodb-org

WINDOWS SUBSYSTEM FOR LINUX (WSL) - UNSUPPORTED
MongoDB does not support the Windows Subsystem for Linux (WSL).

So, I installed mongod using sudo apt install mongodb and when I ran mongo it shows the error:

connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
2020-06-21T13:33:40.606+0530 E  QUERY    [js] Error: couldn't connect to server 127.0.0.1:27017

Is there is a way to install the appropriate package and make it running?

halfer
  • 19,824
  • 17
  • 99
  • 186
evolving_kid
  • 293
  • 1
  • 2
  • 8
  • I think these are the instructions you should follow to install mongodb on WSL: https://learn.microsoft.com/en-us/windows/wsl/tutorials/wsl-database#install-mongodb. They work for me. – msoutopico Oct 08 '20 at 17:10
  • 1
    @msoutopico They work for me too, but the problem is that it's an outdated version – Conner Dassen Oct 08 '20 at 20:03

8 Answers8

61

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
  • 1,148
  • 11
  • 20
  • 14
    Running the command `sudo apt-get install -y mongodb-org` gives me multiple errors: `System has not been booted with systemd as init system (PID 1). Can't operate. Failed to connect to bus: Host is down dpkg: error processing package mongodb-org-server (--configure): installed mongodb-org-server package post-installation script subprocess returned error exit status 1` The failure to setup mongodb-org-server leads to dependency problems for mongodb-org which depends on it. – Conner Dassen Sep 29 '20 at 11:59
  • 1
    Also, starting the database using `sudo service mongod start` fails. – Conner Dassen Sep 29 '20 at 12:07
  • Yes, same for me, same as @ConnerDassen. See my comment to the original post above. – msoutopico Oct 08 '20 at 17:13
  • 1
    @ConnerDassen Yes but the solution above worked for me. I am under WSL2. I can confirm that `sudo service mongod start` started successfully. – Maverick Oct 09 '20 at 09:16
  • `sudo service mongod start` now runs but gives this error - `/etc/init.d/mongod: 1: nit.d: not found`, it shows starting database mongod but doesn't start it actually. (WSL2) – Rohit Kaushal Oct 11 '20 at 03:06
  • here's a nice documentation by microsoft on installing databases on WSL2 -> https://learn.microsoft.com/en-us/windows/wsl/tutorials/wsl-database – kybrdbnd Nov 03 '20 at 06:21
  • 1
    @kybrdbnd this tutorial installs an old version (3.6) of MongoDB – Renato Byrro Jan 27 '21 at 14:30
  • @ConnerDassen You can try removing it by `sudo apt-get purge mongodb-*` then do it again – Md. A. Apu Nov 20 '22 at 19:22
7

@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.

Balaji Dubey
  • 446
  • 5
  • 10
  • I got this to work in 20.04, but I also needed to change the ownership of `/var/lib/mongodb` to `mongodb`. – Neil Jul 02 '21 at 05:35
6

This worked for me.

Run sudo apt install mongodb

Then after that installs run sudo service mongodb start

It will say * Starting database mongodb

And after that I can connect using mongo

Dharman
  • 30,962
  • 25
  • 85
  • 135
phocks
  • 2,933
  • 5
  • 27
  • 28
6

I tried all the methods above but none of the methods worked. I came across Microsoft's official documentation and everything works like a charm.

https://learn.microsoft.com/en-us/windows/wsl/tutorials/wsl-database#install-mongodb

Blessing
  • 2,450
  • 15
  • 22
  • After do this, if on the log (/var/log/mongodb/mongod.log) appears this message: Failed to unlink socket file","attr":{"path":"/tmp/mongodb-27017.sock","error":"Operation not permitted" Delete mongodb-27017.sock (sudo rm -r /tmp/mongodb-27017.sock) Then start – eduardo a Jul 20 '22 at 19:02
4

Easiest way to bypass this issue is to run MongoDB on Windows. You'll still be able to connect from WSL2 over localhost.

For me, @bonyem's solution resulted in an error:

System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down

I'm going to install Mongo 4.4. Many instructions were taken from @Lyubosumaz's directions here.

Get rid of all your Mongo PPAs.

~ ❯ sudo ls /etc/apt/sources.list.d
mongodb-org-4.4.list  yarn.list
~ ❯ sudo rm -i /etc/apt/sources.list.d/mongodb-org-4.4.list

Remove all Mongo packages and bits and pieces.

sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongodb
sudo dpkg --remove --force-remove-reinstreq mongo-tools
sudo dpkg --remove --force-remove-reinstreq mongodb-org
sudo dpkg --remove --force-remove-reinstreq mongodb-org-server
sudo dpkg --remove --force-remove-reinstreq mongodb-server-core
sudo dpkg --remove --force-remove-reinstreq mongodb-org-mongos
sudo dpkg --remove --force-remove-reinstreq mongodb-org-shell
sudo dpkg --remove --force-remove-reinstreq mongodb-org-tools
sudo apt-get --fix-broken install
yes | sudo apt autoremove

Add new PPA and install Mongo 4.4.

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

You'll get the same error from before. This is caused by mongodb-org package trying to use systemctl to start the DB after install (source). Notice that the same error is thrown when running the command manually.

~ ❯ sudo systemctl start mongod
System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down

However, running mongod should still work!

If you run into errors when running mongod, you might need to modify some permissions and create a data directory. I found the answer to these issues here, here, and here.

sudo chown $USER /tmp/mongodb-27017.sock
sudo mkdir -p /data/db
sudo chown -R $USER /data/db
Raphael
  • 441
  • 1
  • 6
  • 12
1

I have tried some of the answers above, it did not work for me. Then I followed a guide 1:

  1. Update ubuntu packages
  2. Install MongoDB on the command line
  3. Set up your local database
  4. Creating the folders and permissions

It worked in my case!

0

I was able to make @bonyem's solution work on Ubuntu 22 just now with the following addition for local dev. I'm stupidly still not allowed to comment on @bonyem's answer because I don't have 50 rep so I had to add this as an answer so I don't have to dig this up for next time I need it.

sudo service mongod stop
sudo rm -rf /tmp/mongod*
sudo mkdir -p /var/lib/mongodb
sudo chmod -R a+wxr /var/lib/mongodb
sudo chown -R mongodb:mongodb /var/lib/mongodb
user296656
  • 46
  • 4
0

I was running into an error when using @bonyem's solution. I saw this error in the logs that it did not have permission to write to /var/run/mongod.pid. I saw this by running sudo cat /var/log/mongodb/mongod.log I had to make a few permission changes and changes to the init.d script to get it to work. first I changed this line PIDFILE=/var/run/$NAME.pid to PIDFILE=/var/run/mongodb/$NAME.pid.

Then ran the command sudo chown -R mongodb:mongodb /var/run/mongodb/ I tried it at first without the -R and each time the .pid file was created it was owned by root. Adding the -R seemed to permanently make new files owned by mongodb. @user296656's answer helped me figure that out!

After doing that running sudo service mongodb start worked and the error was gone in the logs.

John
  • 126
  • 1
  • 6