Use Windows Subsystem for Linux (WSL) like a Pro

    Use Windows Subsystem for Linux (WSL) like a Pro So, let’s do a deep dive on Windows Subsystem for Linux (WSL) features, versions, and advanced configuration settings that will allow us to use a really good GNU/Linux environment in our Wind


    So, let’s do a deep dive on Windows Subsystem for Linux (WSL) features, versions, and advanced configuration settings that will allow us to use a really good GNU/Linux environment in our Windows environment.

    Today, having WSL available on your Windows machine is a must-have for any serious engineer working in cross-platform environments. It is a really important tool to have at your disposal for any professional DevOps, Cloud, or Software Engineer.

    In this post, we’ll explore the features, versions, and advanced configuration settings of WSL to set up a real pro developer environment. Whether you’re a Cloud Engineer, DevOps Specialist, or Software Developer, mastering WSL will give you the power and flexibility of a Linux terminal without leaving Windows. Let’s get started.

    What Is WSL?

    Windows Subsystem for Linux (WSL) is a compatibility layer developed by Microsoft that allows users to run a full GNU/Linux distribution directly on Windows. This is a native operating system feature without the need to set up your own virtual machine or dual-boot.

    It was originally launched in Windows 10 to provide developers with a native Linux terminal and the ability to run most command-line tools, utilities, and applications. It’s a really important tool for anyone who:

    • Develops in a Linux-first ecosystem (Node, Python, Ruby, etc.).
    • Works with Docker, Kubernetes, Terraform, or Ansible.
    • Manages cloud environments from Azure, AWS, or GCP.
    • Needs bash, CLI tools like grep/sed/awk, or tools like vim, emacs, tmux.

    WSL 1 vs. WSL 2

    Well, it all started with the WSL 1 version that set the ground for this native support for a Linux environment in Windows. But after some time, Microsoft released the WSL 2 version with some key improvements in the underlying technology.

    The main difference between these two is that the WSL 2 version runs an actual Linux kernel within a managed VM and supports full system call compatibility and performance. WSL 2 runs the Linux distributions as containers within this managed VM, and it uses a subset of the Hyper-V architecture to enable its virtualization.

    On the other hand, WSL 1 is not a real Linux kernel but emulates Linux system calls and maps them to Windows APIs, and its compatibility with Linux tools is not 100%.

    Features comparison

    Here you’ll find the official features comparison:

    So, as you can see, there are some key features here, but I’ll focus next on the ones that will affect your day-to-day tasks if you are an engineer using WSL…

    So let’s start with the key features that we need to understand to use WSL like a Pro.

    Which version to use: 1 or 2?

    As Microsoft recommends, the go-to version to take advantage and make use of GNU/Linux is the WSL 2 version. It has some core improvements and allows you to run a full Linux kernel.

    Performance across OS file systems

    In my experience, one of the key features to select the version is the OS File system performance.

    If you use WSL 2, as mentioned, you’ll use a Managed VM with dynamic memory allocation and each distro will be a Docker container within this managed VM. This comes with an important consideration to keep in mind: the access to the Windows OS files from the VM.

    In WSL 2, this is done through DrvFS. These scenarios occur when you are accessing your Windows files from WSL, most likely via /mnt/c. This has really poor performance if you are constantly using files that are in the Windows file system.

    Microsoft recommends against working across operating systems with your files, unless you have a specific reason for doing so. For the fastest performance speed, we would need to store your files in the WSL file system if you are working in a Linux command line (Ubuntu, OpenSUSE, etc). If we’re working in a Windows command line (PowerShell, Command Prompt), store the files in the Windows file system.

    So, if you are using WSL 2 for high I/O operations you should do it all within the Linux file system.

    Sync files from WSL 2 to Windows OneDrive folder

    If you are like me and you feel safer having your files always synced within your OneDrive folder, you can use the following repo to configure a systemd service to continusly sync your files without affecting the performance:

    rsync-wsl-onedrive/rsync-wsl-onedrive.sh at main · mdiloreto/rsync-wsl-onedrive
    Contribute to mdiloreto/rsync-wsl-onedrive development by creating an account on GitHub.github.com

    Install WSL

    Now that you can install the tool https://learn.microsoft.com/en-us/windows/wsl/install. You have to use the following command:

    Plain text
    wsl --install -d <DistroName>

    You must be running Windows 10 version 2004 and higher (Build 19041 and higher) or Windows 11 to use the commands below. If you are on earlier versions please see the manual install page.

    Setup Windows Terminal

    To take full advantage of WSL, you should use the native Windows Terminal tool. This one was a great discovery for me, and since I started using it with WSL 2, I’ve been having a great experience.

    Windows Terminal can run any application with a command line interface. Its main features include multiple tabs, panes, Unicode and UTF-8 character support, a GPU accelerated text rendering engine, and the ability to create your own themes and customize text, colors, backgrounds, and shortcuts.

    Some features include:

    Setup your terminal settings

    You can configure some key settings to make your Windows Terminal experience better. To do this, go to the configuration section:

    There you’ll find the following settings:

    Some key settings here:

    • Startup > Default Profile: Set your preferred Distro/Shell.
    • Startup > Default Terminal: Set to Windows Terminal.
    • Interaction > Automatically copy selection to clipboard: Enabled.

    In the Appearance and Color Schemes section, you can set up whatever you like the most :)

    • In Profiles > Default, you can also set up other appearance settings like the font face.

    Install the Visual Studio Code Extension

    Remote Development extension pack

    One really useful feature to use VSCode integrated with the WSL 2 deployment is to use the Remote Development extension pack. This pack includes the WSL extension, in addition to the Remote SSH and Dev Containers extensions, enabling you to open any folder in a container, on a remote machine, or in WSL.

    In some distros, you’ll have to run the following commands in order to be able to correctly run VSCode in WSL:

    Bash
    sudo apt-get update
    sudo apt-get install wget ca-certificates

    Then you’ll be able to use open VSCode using the code command on your distro:

    As you can see now your Terminal and Explorer are now connected to the WSL Distro.

    Install zsh theme!

    Now that you have the Terminal setup, you need to make your shell better. This was a key quality-of-life feature for me as a long-time Windows user.

    For this i recommend to:

    • Install Zsh / Oh My Zsh.
    • Find a Theme that you like. You can use the default “robbyrussell” that is actually pretty good.
    • I personally use Spaceship.sh. You can follow along their tutorial to install :)

    IMPORTANT: Install the following Plugins for Zsh

    • Git: git
    • Zsh Autosuggestions: zsh-autosuggestions
    • Zsh Syntax HighLighting: zsh-syntax-highlighting
    • Fuzzy Finder: fzf

    You just have to add them to your Spaceship or .zshrc configuration file, and they'll start making your life easier :)

    Install the Docker Engine integration

    And now for the final section — this one’s interesting (but I think there’s still room for improvement).

    This feature allows us to:

    1. Consume the Docker Engine process in your Windows system distro from your WSL 2 distro.
    2. Allow the WSL 2 distro/container to use Docker Engine from Docker Desktop (Windows App).

    This is REALLY useful if you’re working with Linux containers (which you probably should be). It’s done through the Docker WSL Integration.

    This one is REALLY usefull if you are working with linux containers (which you should be doing). This is done through using the WSL Docker Integration.

    Enable Docker Desktop WSL Integration

    To do this:

    • Enable Docker Desktop’s integration with WSL in settings.
    • Enable WSL integration for your chosen distros.
    ⚠️ This extension has its own problems :)

    I’ve faced issues in the past where the extension failed for some distros. For that, I had to modify the wsl.conf file inside the distro:

    So this extension has his own problems :) i’ve face an issue in the past were the extension failed for the distros. For that i had to modify the wsl.conf file in the distro:

    TOML
    [boot]
    systemd=true
    [network]
    generateResolvConf = false

    And hardcode the DNS servers:

    Alternative of using Docker Desktop WSL integration

    I read somewhere that you could just uninstall Docker Desktop and install Docker Engine directly inside the WSL-managed VM 😮

    I’ve never tried it… Maybe it could be a good thing for you to try and let me know in the comments how it goes.

    If I try it in the near future, I’ll let you know my results!

    Thanks for reading. The learning continues in the next article.