How to easily set up a Linux development environment on Windows using WSL2
During my project work, I frequently found myself wanting a solid development environment on Linux. In my experience, it’s much easier to manage, packages are easier to install and if I want to deploy my software on a Linux server later on, there’s just much less fuzz. Also, Free and Open-Source Software (FOSS) for the win in general 🐧!
However, my work computer was always restricted to Windows so far. I couldn’t go the traditional route of dual-booting and running a full Virtual Machine (VM) on top of Windows seemed a bit counter-intuitive for me. No. I wanted to seamlessly integrate Linux tools and workflows into my Windows machine. To my surprise, this was possible using the Windows Subsystem for Linux 2 (WSL2).
WSL2 vs native Linux
WSL2 is the updated version of WSL, a virtualisation technology that is a capable of running a full Linux kernel inside a utility VM on Windows. It essentially is a very lightweight VM without the traditional overhead. WSL2 is directly integrated in linux and is compatible with system calls. This sounds great, right?
Initially, one of my main concerns was performance. However, it turns out that these worries were in vain. According to Techradar, WSL2 operated at about 94% of the speed of a native Linux installation on Windows 11 when combined with Ubuntu 20.04 LTS. Though, there is some minor lag in I/O operations due to the added layer of virtualisation. But for development and testing, this shouldn’t be much of an issue.
Step-by-Step installation
Now, to the actual content of this article: How to easily set up a reasonable development environment using WSL2 on Windows?
Step 1: Make sure you have WSL2 installed
If you’re using Windows 11 or any newer build of Windows 10, WSL2 should already be installed by default. You can check it by running the command wsl --help
in a PowerShell terminal. If it runs successfully, then all good. If not, you can install WSL2 as described here.
Step 2: Install a distro
Next, you need to install a Linux distribution that will act as a basis for your development VM. In my case, I used Debian as my go-to-distro. However, there are plenty of others like Ubuntu or Oracle Linux available too. You can check all distros available using the comment wsl --list --online
. Once you have made a choice, you can install your Linux system using the command:
wsl --install -d Debian --web-download
You can, of course, replace Debian
with your distro name.
Step 3 (only if needed): Fix internet connectivity
After installing and setting up the root username and password, you should be able to access your Linux VM by merely entering the command wsl
in a PowerShell terminal. The first thing I always do on Linux is to update the software installed using apt update && apt upgrade
. However, on some machines, I found that the internet speeds were horribly slow within WSL2. To fix this, you might need to adjust your nameserver settings by changing up your resolv.conf
file as follows:
sudo rm /etc/resolv.conf
sudo bash -c 'echo "nameserver 8.8.8.8" > /etc/resolv.conf'
sudo bash -c 'echo "[network]" > /etc/wsl.conf'
sudo bash -c 'echo "generateResolvConf = false" >> /etc/wsl.conf'
sudo chattr +i /etc/resolv.conf
Again, this step must only be followed there are issues with the internet connection within your WSL2 system. I didn’t encounter this issue on my newer Windows 11 machine.
Step 4: Install required software and set environment variables
If you need environment variables that persist across sessions on your virtual Linux system, you can adopt your .bashrc
file accordingly:
sudo nano ~/.bashrc
export VARIABLE_NAME=variable_value # add this line for each variable
Then save using Ctrl+X, close .bashrc
and restart your terminal to apply the changes. Your environment variable should then be visible when typing echo $VARIABLE_NAME
.
You can also rapidly install Miniconda with Python by running the following chain of commands:
sudo apt install wget
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh
Step 5: Limit WSL2 memory usage
WSL2 can be a bit RAM-hungry by default. To limit memory usage and automatically drop cached files, you can create a .wslconfig
file in your Windows home directory (that is, C:\Users\<UserName>
) and fill it with the following configuration:
[wsl2]
memory=48GB
[experimental]
autoMemoryReclaim=dropcache
For context, I had 64GB of RAM on my machine available. Setting a 48GB cap ensures that WSL2 doesn’t use more than I need and activates automatic memory reclamation. Feel free to adapt this number accordingly. autoMemoryReclaim=dropcache
further enables the instant relase of cached memory if not needed anymore.
Step 6: Start developing
To start developing, you can now connect to your WSL2 machine via Visual Studio Code’s Remote Development extension. It is even possible to connect from a remote computer to your windows machine into WSL2. Quite amazing!
Done!
WSL2 with Debian on Windows provides an efficient development environment for my needs. I honestly believe that WSL2 hits the mark for anyone looking to combine Linux tools with Windows. It’s best of both world, in a sense. Have fun!
Cheers!
David