DEV Community

loading...

How to make WSL2 even faster, with fast git

themartes_ profile image Matej Bunček 🤔 ・3 min read

So Windows 2004 update is here, and I was really excited about this update. Because it brings WSL2 as a production-ready product to our daily drivers. With all the excitement I downloaded it, and when I did cd into my project I quickly realized that something is wrong. Git performance was really, really slow.

So I started searching around web and I discovered this issue and it seems like this issue in only appearing in the /mnt directory. Sadly the only workaround I found was to move your projects into the WSL filesystem. But I knew I want something more pleasing. So I created a Symlink for it. Now, this took me a great 6 hours, to figure out all the things you need to do, so if you're stuck on the same issue, I hope my guide will help.

1. Create a script 📖

You need to create a batch script that will take care of the symlink. Unfortunately, you can't do a symlink from WSL to windows, which is my plan. Create a symlink from $HOME/Code to your User Directory. So you can start up CMD/PowerShell/Bash/WSL and cd right into code directory.

Open up notepad and paste the following lines in here. Make sure you'll change you usernames

@echo off
START /B "" pushd \\wsl$\<Your WSL Version>\
rmdir C:\Users\<Your Windows Username>\Code /s /q
mklink /d C:\Users\<Your Windows Username>\Code Z:\home\<Your WSL Username>\Code
Enter fullscreen mode Exit fullscreen mode

But how should I get my WSL version?

Well, it's fairly easy. Just open up your WSL, type cd $HOME and then explorer.exe .. In the location bar type cmd like so
Cmd in adress bar
This will open WSL Filesystem in Windows explorer. Now click on the adress bar again and you should see something like this
Alt Text
The part that we need is that Ubuntu-20.04 in my case but that could be fedora or alpine or Ubuntu in your case.

What this script does?

Basically it will just mount your WSL as a network drive, and then it will create Windows Symlink, which will work in both Windows and WSL. Unlike the WSL symlinks which work only in WSL.

When we have our script done go to File > Save as, and then choose a location where you know that your script will be safe, so you don't accidentally delete it, and name it mount_wsl.bat.

2. Create a schedules task with highest privileges ⏱

Now when we have our script we need to run it every time we boot into our machine. We also need to do that with the highest privileges because you need to be an admin to make a symlink from network drive to your base drive.

So go ahead and search for Task Scheduler. On the left sidebar you will see Task Scheduler Library. Click on it and on the right sidebar click on Create Task....

Now make sure you'll give a name to your task, then write a little description and Check "Run with highest privileges". Also make sure you'll change Configure for: To Windows10

Win10 Scheduler

Next click on Triggers tab and add new trigger. This will look fairly simple, Just make sure it's like this, and click OK.

Triggers tab

Next click on Actions add New and Select the .bat script we created earlier.

3. Profit 🤑

Now when we restart our computer the network drive will be available in "This PC" and Code directory symlink to WSL will be in your User Directory. With significantly faster I/O Operations and git. Enjoy ✌

Discussion (7)

pic
Editor guide
Collapse
obriat profile image
O'Briat • Edited

I try to link directly the \\wsl$ and it seems to work, disk performance are back to normal and I doesn't need a startup script.

C:\>mklink /d C:\FOO \\wsl$\debian\BAR
$ cd /BAR/
dd if=/dev/zero of=./testfile bs=1M count=1000 
# x4 to x6 faster than /c/ (or /mnt/c/ if you don't use "root = /" option in wsl.conf)

[Edit]: after navigating through these folders, Explorer switchs path from C:\... to \\wsl$\... and I cannot open the new path with my IDE (phpstorm)

Collapse
nabeel profile image
Nabeel Molham

Hi Matej,

Thanks for the idea, the thing is I don't follow now do I access the windows files now from WSL? they doesn't show up under the /home/{username/ dir

Collapse
adsoncicilioti profile image
Adson Silva Cicilioti

"Z:\home<Your WSL Username>\Code" could it be "Z:\Code"?
Coincidentally I have a Z: disk and I keep my Projects in a directory at the root. I didn't quite understand why "home \ wsl_username".

Collapse
ilanvivanco profile image
Ilán Vivanco

If I run this as soon as the session is logged on, it results in an error. So you can have it delay by 15 min at logon. That worked for me.

Collapse
kmc79 profile image
kmc79

Why not run git from wsl?

Collapse
themartes_ profile image
Matej Bunček 🤔 Author

You're using git in WSL, the problem is with mounted windows. When windows is under /mnt git is extremely slow, and for me personally and projects what I'm developing in work it's really a bummer. This repository is more than 10 years old, and simple git status is taking about 5 seconds. With this "hack" the git is running really smooth. Basically git something which is running on top of your filesystem, so it makes sense that it could be slower on mounted filesystems.

Collapse
ilanvivanco profile image
Ilán Vivanco

Extremely helpful. Just what I was looking for, thanks!