Blog Cover

How to be a productive programmer in Windows with WSL2

Author profile image
Aitor Alonso

Jun 17, 2021

Updated Jul 31, 2022

4 min read

I will explain how to configure WSL 2 to fully integrate it with VS Code and Docker Desktop, to achieve on Windows a full programming environment with productivity in mind.

In case you missed it, in a previous post I explained how to install WSL 2 on your Windows 10 machine.

Wait, can you really be a productive programmer in Windows?

Yes, or at least, WSL 2 will provide you with a Linux environment with all the fancy stuff such as a UNIX terminal (bash, zsh, ...), unix tools like curl, wget, ssh, ..., and everything you would need in a normal development environment, while still having a Windows OS where you can install some proprietary Windows-only apps.

This is how I configured my PC for my daily remote work as a professional developer. Before that, I had a dual boot windows + Linux setup, which I encourage you to do if you want to enjoy the full Linux experience. However, currently I prefer an easier approach, because I don't want to maintain two different OSes, nor having my disk space divided or a boot manager asking me what to boot.

First step: installing docker

To install docker on both Windows and WSL 2 you only need to install Docker Desktop with its .exe installer. That's it. Here is the official download URL:

https://www.docker.com/products/docker-desktop

Install it like any other Windows app. If it asks about enabling WSL 2 support, just allow it like in the picture below:

Installing Docker Desktop with WSL 2 support

Once installed, open the Docker Desktop app and go to settings by clicking on the gear icon on the upper bar (1). Then, check if effectively WSL 2 support is enabled (2). If you want, you can also disable Docker Desktop to automatically start when you log in to Windows, or to send usage statistics, from this settings page.

Checking WSL 2 support on Docker

If you have more than one Linux distribution installed on WSL 2, you may want to enable Docker for them as well. To do so, just go Resources > WSL integration as you see in the picture below.

Enabling WSL for individual distros

After that, restarting Docker and WSL is required. You can just reboot your Windows PC, or manually:

  1. Open a PowerShell terminal and type wsl --shutdown
  2. Right-clicking on the docker icon on task bar --> Restart Docker

Testing and troubleshooting

You can test your Docker integration with WSL by running the following commands from WSL 2:

docker --version
docker-compose --version

If everything is correctly configured, those commands will show your docker installed version.

In the case it prints an error message saying that the docker or docker-compose command couldn't be found, check that the Docker Desktop app is up and running by looking for the Docker icon on the Windows task bar. If Docker is running and the error persists, you should double-check that you enabled WSL 2 integration on Docker Desktop settings, like in the pictures above. Try enabling it on all your Linux distros by activating the toggle button on Resources > WSL integration settings page or restarting your PC if you didn't do it already.

Next: installing VS Code Remote WSL extension

I suppose you already have installed the good knowing VS Code editor on your Windows. If you haven't done it yet, just download it from the official page and install it like any other Windows program. There is nothing especial with its installation process.

Then, open VS Code and install the Remote WSL extension. You can read more about this extension in the previous link.

Once the extension is installed and enabled, on VS Code press Ctrl + Shift + P and search for wsl commands. You should be able to see commands for opening a folder inside your WSL distro or just opening a new VS Code instance on your home directory, like in the next picture.

WSL commands from VS Code

After executing one of the above commands, you should be now using VS Code over a WSL directory. If you try to open the VS Code integrate terminal, you will load bash (or zhs or whatever your default distro shell is it) instead of PowerShell or CMD. You are, effectively, working inside your WSL environment. Check pwd command output!

Pro-tips: you can open a VS Code instance from/in a WSL directory by just typing on the WSL terminal:

code .

The same with the Windows Files explorer. It will open the WSL folder as a network storage.

explorer.exe .

Conclusions

Congratulations! Now you have a full working dev environment with VS Code, fully integrated with Docker, and with your favorite Linux shell and tools available. In fact, you are working on a Linux-like system without leaving Windows, so Linux specific tools (such as apt or apt-get if you chose a Debian based distro), configurations and project setups will work out of the box inside WSL.

With this setup, it is easy to feel productive programming on Windows!


I hope my article has helped you, or at least, that you have enjoyed reading it. I do this for fun and I don't need money to keep the blog running. However, if you'd like to show your gratitude, you can pay for my next coffee(s) with a one-time donation of just $1.00. Thank you!

No by AICC-BY 4.0