Skip to content
Snell Rojas

Programming

How to Install MongoDB in WSL2 Ubuntu

Install MongoDB in WSL2 Ubuntu: the native way (lots of steps) and the easy way to go (one Docker command).

Snell RojasPublished Updated 3 min read

If you want to install MongoDB on WSL2 Ubuntu, I have good news and bad news for you. On the one hand, the bad news is that there is no "easy" way to make it work natively within WSL2. However, the good news is that there is a way to install it easily with just one console command, under the condition that it won't be native.

Install MongoDB in WSL2 Ubuntu: Native #

After searching the internet for a long time, I found this answer on Stack Overflow, which is the only one that worked for me. But before showing it to you, I want you to know that it requires a lot of steps and can be quite confusing.

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

bash
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:

bash
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:

bash
sudo nano /etc/init.d/mongod

Paste the contents of this link into the file and save it.

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

# start the service
sudo service mongod start

Full answer on Stack Overflow

Install MongoDB in WSL2 Ubuntu: Docker container #

The easiest option to install MongoDB on WSL is to use Docker. If you have no clue what Docker is, don't worry, it's easier than it seems. Docker is a platform that allows you to create minimal virtual machines.

The best thing about Docker is that with its management software, Docker Desktop, you get full integration with WSL2.

Once Docker Desktop is installed on Windows and integrated with WSL2, all you have to do is run the following command:

bash
docker run -d -p 27018:27017 -v /path/to/host/data:/data/db --name mongodb_container mongo

Let's break down the command

  • docker run: This command is used to run a Docker container.
  • -d: It runs the container in the background (detached mode).
  • -p 27018:27017: This option maps port 27018 of the host machine to port 27017 inside the container. The format is -p hostPort:containerPort.
  • -v /path/to/host/data:/data/db: This option creates a volume to persist the MongoDB data. It maps the /path/to/host/data directory on the host machine to the /data/db directory inside the container. You can replace /path/to/host/data with the actual path on your host machine where you want to store the MongoDB data.
  • --name mongodb_container: This option assigns a name to the container, in this case, "mongodb_container".
  • mongo: This specifies the name of the Docker image to use for the container. In this case, we are using the official MongoDB image.