Skip to content

Instantly share code, notes, and snippets.

@rstacruz
Last active March 17, 2023 09:50
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rstacruz/bdb917af94af2b9f90da7f1e7a033c9a to your computer and use it in GitHub Desktop.
Save rstacruz/bdb917af94af2b9f90da7f1e7a033c9a to your computer and use it in GitHub Desktop.

Make WSL2 faster by removing /mnt/c in the PATH variable

Turning off appendWindowsPath (the feature that adds /mnt/c paths to $PATH) can make WSL faster.

For Ubuntu on WSL

For most distros, it can be done by editing /etc/wsl.conf. These instructions were taken from https://gist.github.com/ilbunilcho/4280bd55a10cefef75e74986b6bff936

$ sudo vi /etc/wsl.conf

[interop]
appendWindowsPath = false

$ exit

For ArchWSL

For ArchWSL, it's "arch.exe config --append-path false".

Why does it work?

Accessing /mnt/c is slow, and WSL adds /mnt/c to your PATH. This makes things that do PATH lookups a bit slow, and removing that feature can speed up a few things. A typical PATH in WSL might look like this:

myusername@wsl2:~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/wsl/lib::/mnt/c/WINDOWS/system32:/mnt/c/WINDOWS:/mnt/c/WINDOWS/System32/Wbem:/mnt/c/WINDOWS/System32/WindowsPowerShell/v1.0/:/mnt/c/WINDOWS/System32/OpenSSH/:/mnt/c/Program Files/dotnet/:/mnt/c/Program Files (x86)/dotnet/:/mnt/c/Users/ricos/scoop/shims:/mnt/c/Users/MYUSERNAME/AppData/Local/Microsoft/WindowsApps:/mnt/c/Users/MYUSERNAME/AppData/Local/Programs/Microsoft VS Code/bin:...

As a result, typing in non-existing commands can take longer time than usual.

$ time asdfghjkl
asdfghjkl: command not found

real  0m0.209s

Typing non-existent commands may not happen too frequently, but this penalty applies to anything that does a PATH lookup. Some shell scripts and configs may rely on this.

time command -v where   # => 0.143s
time which where        # => 0.195s

Workaround: Explorer

Disabling appendWindowsPath means typing “explorer.exe” from the WSL prompt won’t work anymore. Adding shell aliases would be a way around this.

# .bashrc or .zshrc
alias explorer.exe="/mnt/c/Windows/explorer.exe"

# fish (type in a prompt, don't add to a config file)
abbr explorer.exe "/mnt/c/Windows/explorer.exe"

Workaround: VSCode

The same goes for VSCode. An alias can be used here too:

alias code="/mnt/c/Users/YOURUSERNAME/AppData/Local/Programs/Microsoft VS Code/bin/code"

Workaround: other apps

The workarounds for “explorer.exe” and “code” can be done for other apps too. To find the right path, use “where.exe”. For example, to set up an alias for Notepad:

# Find the absolute path for notepad:
/mnt/c/Windows/where.exe notepad
# C:\Windows\System32\notepad.exe
# C:\Windows\notepad.exe

# Then use the first one to write an alias:
alias notepad="/mnt/c/Windows/System32/notepad.exe"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment