My dev environment in Windows 10, WSL2 and Docker
I've kept switching between Ubuntu and Windows for the last 3 years since I bought my latest laptop back in November 2017, a Razer Blade Stealth.
My previous laptop was a Macbook Pro so switching to Windows was not ideal and I decided to install Ubuntu on it. I used Ubuntu for almost 2 years but I was never 100% comfortable with it due to some small issues with Bluetooth and not being able to hibernate the laptop. A few months ago I decided to give Windows 10 another opportunity when I heard about Windows Subsystem for Linux, which basically allows you to run Linux inside Windows. I've been running this setup for the last 6 months and I'm pretty happy with it so if you want to give it a try, find below all the details.
Install WSL2 in Windows 10
There are plenty of guides about how to install WSL2 so I'll not explain it myself. I followed this one I found in Scotch.io. You just have to keep in mind that you'd need a Windows 10 Pro license. I installed Ubuntu 18.04 LTS from the Windows 10 store (I guess it's time to upgrade to 20.4 now that it's out 😄).
With WSL2, the Ubuntu (or the distro you choose) home folder will be available as a network folder under
\\wsl$. For example, I have my home in
\\wsl$\Ubuntu\home\uf4no and I can navigate and do anything in this folder with the Windows explorer, like copy/pasting, renaming files, creating folders etc...
Once WSL2 and your preferred Linux distribution is installed, you can go ahead and install whatever programming languages and runtimes you need. I code most of my projects in Node.js and PHP/Laravel) so I used NVM (docs in the repo) for Node and followed the official Laravel docs.
Move WSL2 Linux distribution to other partition
I have 2 partitions in my 512GB SSD, one with ~75GB for Windows and the rest as a "data" one. By default WSL and its Linux distributions are installed in the Windows drive so it might cause you some issues if, like me, your partition is not very big (just the Ubuntu installation is 12GB).
Luckily I found this program that allows you to move your distributions to any other drive in minutes.
This guide covers everything, from the installation of the program (
choco install lxrunoffline if you use Chocolatey, very recommended) to which commands to run to move your distribution. (
lxrunoffline move -n <distro name> -d <destination folder>)
Install and configure Docker
I use Docker to deploy most of my projects or to run services I don't have installed, like a MongoDB or Redis. The newest versions of Docker Desktop for Windows come with WSL2 integration so, even if you install Docker in Windows, you can run and manage your containers from your Linux distribution 🤯. You can find the installation guide here. Once installed make sure to go to Settings and enable the WSL2 based engine as shown in the image below:
VisualStudio Code WSL and Docker integration
With VSCode the situation is similar as with Docker. We can have VSCode installed in Windows but use it to open a remote session of a project stored inside our Linux file system using the remote WSL extension. For example, I have a "Projects" folder in my Ubuntu home (
\\wsl$\Ubuntu\home\uf4no\Projects) and I can open bash, navigate to one of my projects and run
code . to open the project in VSCode inside Windows. In addition the Shell in VSCode will change to Bash when opening a WSL remote session and you can even choose to use WSL as the default Shell when opening projects inside Windows.
You can read more about the VSCode integration with WSL in this article.
One of the most useful extensions for VSCode is the Docker one. It allows you to automatically generate Dockerfiles, docker-compose files, create and tag images and run containers. It has become the default way to manage my containers so it's one of the firsts I install. For some reason, it was not able to connect to the docker daemon and it was showing the following error:
I looked around and in order to fix it, I just had to remove the DOCKER_* environment variables from my .bashrc file inside Ubuntu (I think at some point I installed Docker in Ubuntu for some reason so it was probably messing things up). Once removed, the extension recognized the docker daemon running from the Windows installation and I was able to manage my container with no issues.
I hope you find this useful. If you're trying to setup a similar environment and you're facing some issues, feel free to contact me on Twitter and I'll be happy to help.
If you enjoyed this article consider sharing it on social media or buying me a coffee ✌️
Oh! and don't forget to follow me on Twitter where I share tons of dev tips 🤙