WSL tips

May 15, 2023
Last updated: November 1st, 2023

Windows Subsystem for Linux (WSL) is my main programming environment, after giving up Windows in 2015. It is surprisingly good.

Shrink your virtual HD

The virtual hard drive in WSL seems to grow without bound (they grow as needed, but won't release space even if you delete data). After shutting down your WSL instances (i.e. wsl --shutdown), run this in PowerShell (as an administrator):

Optimize-VHD -Path $VHD_PATH -Mode full

where the $VHD_PATH is usually something like C:\Users\$USERNAME\AppData\Local\Packages\$DISTRO\LocalState\ext4.vhdx

There's an alternative method using diskpart described here which I've successfully used before.

Separately, there's a convenient package called wslcompact that will run Powershell scripts for you to automate this process. I've only used it to show info, but most reports seem to indicate it works well.

Use WSL VSCode plugin

I was a long-time holdout on Visual Studio Code (VSCode), but I've completely switched from vim except for on-server editing. The things that made me switch were (a) Pylance, (b) rust-analyzer, and (c) Copilot. rust-analyzer is available inside vim as well, but the integration is less convenient, at least for me (I like being able to hover over variables and see the inferred type, it is extremely helpful).

You can use the WSL extension to open a folder for editing.

If you have VSCode installed, you can use code . from inside WSL to open a folder inside VSCode. This will also reset rust-analyzer if it ends up in a bad state.

Install CUDA

See this post for advice on how to install CUDA.