WSL 2 starter guide. Must read!

Once you installed WSL2 you might face with different issues, for example your hot reload server is not working anymore comparing to WSL 1, or your build/compilation become ultra slow.

Why? Because all hot-reload servers are inotify-api based, and you probably still serve project from your windows FS like you did in WSL1. And what? And WSL 2 is a virtual machine with separate virtual hard drive, which is actually now a one file in your windows FS ­čś▓:

Image for a hint

WSL 1 had rootfs with nested folders tree here, in the same folder, but not WSL 2.

So what is windows FS and what is Linux FS now then?

But why you still can access both in terminall? Because WSL interpretator cares about it. In reality you can't even navigate to linux FS via Windows Explorer (at least in early releases).

How to fix hot-reload and inotify is not working in WSL2?

So it is not working because inotify hooks installed by your Node or Python process can't track updates from Windows filesystem. Also access to Windows file system is now slower because it works through virtual machine mounts.

To fix it just recreate or copy your project to WSL filesystem:

cp -R /mnt/c/pro/project_folder ~/project_folder

But how to work then if you can't access filesystem­čĄö?

Answer is VS Code + https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-wsl (WSL Remote Extension)

Hit CTRL + SHIFT + P and type WSL, then select Reopen Folder in WSL:

Image for a hint

Then select your distro, e.g. mine is 18.04 and you are there! (Better to use 20.04 because it has newer package version in repositories by default)

Good news that you can just use Reval in explorer feature and WSL will open special network drive so you could e.g. copy assets or even open files in another editor if you need it:

Image for a hint

How to fix /bin/sh^M: bad interpreter: No such file or directory

If you run some binary which sits both in WSL 2 PATH and in Windows native PATH, you might face bad interpreter issue. For example when I installed nodejs and npm into updated WSL 2, I got:

$ npm
bash: /mnt/c/Program Files/nodejs/npm: /bin/sh^M: bad interpreter: No such file or directory

This happens because Windows PATH had precedence over WSL PATH. Even despite of fact npm located correctly:

$ which npm
/usr/bin/npm

If you want to check by yourself, run:

echo $PATH 

And make sure that /mnt/c/Program Files/ goes before /usr/local/bin in output.

So to fix it I suggest you disable Windows PATH in WSL environment at all, once and forever:

sudo nano /etc/wsl.conf

And add line:

appendWindowsPath=False

Now save file and restart terminal, or just re-run bash to apply change without restarting

How to change default distro in VSCode

Default distro terminology works in your system not in WSL, so just open CMD and check list of your distros:

wslconfig /l

Image for a hint

Now type:

wslconfig /setdefault Ubuntu-20.04

Recheck list of distros now:

Image for a hint

Terminal for WSL 2

You can use terminal from VS Code, but if you don't need it, we recommend to checkout Windows Terminal

Image for a hint

Image for a hint

#wsl
menu 11
Ivan Borshchov profile picture
Oct 27, 2020
by Ivan Borshchov

Best related

Other by Ivan Borshchov