install mongodb in wsl2 ubuntu

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

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

Full answer on StackOverflow

Install MongoDB in WSL2 Ubuntu: Docker container

The easiest option to install MongoDB on WSL is to use Docker. If you have no fucking idea 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 called Docker Desktop, you can have 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:

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.

Soy Snell Rojas | Consultor SEO

Soy un experimentado Consultor SEO con 5 años de experiencia trabajando con equipos de marketing y desarrollando estrategias SEO para más de 100 sitios web. Mis principales habilidades son diseñar e implementar cambios en la estrategia de SEO que pueden mejorar los objetivos deseados, mejorar aspectos técnicos de SEO (ya que tengo 3 años de experiencia como desarrollador back-end), también me siento muy bien trabajando de la mano con mis clientes para optimizar sus páginas web.