WSL2 and File System Performance

2020-09-20 — 3 mins reading time

Windows Subsystem for Linux v2 (WSL2) is now available and it has some nice new features such as the ability to run Linux docker containers from within your WSL Linux. It was also made available for older Windows 10 installations (1903 and 1909). But it comes with a caveat.

Problem Description

There are a couple of installation guides for WSL2 out there such as this one. Following this guide, I encountered an error during the migration complaining that the hard disk files must not be encrypted or compressed. This seems to be a common issue, but there is a workaround which also worked for me.

Having solved this, I noticed some changes:

  • The auto-completion feature in bash used to be quite slow in WSL1. This changed for the better, it is much faster now.
  • Some operations on many files are much slower than before, most notably git status and also building an Angular application with Node.
  • File system change detection for ng serve or hugo server did not work. This is a blocker for working with both.

For the performance issue, there is also an open ticket and I can confirm, that it is the same issue for me since my development files reside on a Windows partition and are accessed via /mnt.

Workaround

So how to deal with this? I want docker support and the old performance for my development workflow.

Well, until the performance is fixed, you can install Ubuntu twice: in WSL2 and in WSL1. Instead of doing a fresh install, you can simply export and import your distro again. While doing that, you can choose to use a different version of WSL:

C:\>wsl --export Ubuntu-18.04 - | wsl --import Ubuntu-18.04-wsl1 <folder> - --version 1

Replace <folder> with the path where you want your new distro to be located. Notice the last parameter: --version 1 creates this distribution as a WSL1 distribution. With wsl -s Ubuntu-18.04-wsl1 you can set the new distribution as the default distribution.

You can verify the result with wsl -l -v:

C:\>wsl -l -v
  NAME                 STATE           VERSION
* Ubuntu-18.04-wsl1    Stopped         1
  Ubuntu-18.04         Stopped         2

With wsl -d Ubuntu-18.04 you can run an instance of the WSL2 version of your distro.

Within the newly created WSL1 version of the distro, the old performance is back. In addition, the change detection for hugo and ng serve is also working again.

Hopefully, Microsoft will fix the performance of Windows filesystems soon - maintaining two distros is still some work and eats up disk space.

Additional Notes

A problem with the re-imported distro is, that the default user name is not set as expected. This results in being logged in as root. In addition, Windows mounts have the UIDs of the root user and not of your non-root user.

Solutions to this problem are described here. For me, creating the file /etc/wsl.conf with the following content just worked fine:

[user]
default=<username>