Skip to content

WSL Config

shamik edited this page Apr 25, 2024 · 6 revisions

WSL Config

There are two options for configuring a WSL system with custom resources i.e. # of CPUs, memory(RAM), Swap,...

  1. .wslconfig
  2. wsl.conf

.wslconfig is used to configure settings globally across all installed distributions running on WSL 2.

On the other hand, wsl.conf is used to to configure settings per-distribution for Linux distros running on WSL 1 or WSL 2.

.wslconfig

  • Stored in the %UserProfile% directory; i.e. C:\Users\<UserName>
    • Neat trick to get to your %UserProfile% is to cd ~ in powershell, which typically will return your home directory/%UserProfile%
    • The final directory path would look like C:\Users\<UserName>\.wslconfig
  • Will work only for WSL 2
  • Create a .wslconfig in windows by opening Notepad and saving the file name as .wslconfig
  • Create a .wslconfig in bash by either typing vim .wslconfig or nano .wslconfig
  • Type the following in the .wslconfig file
[wsl2]
# kernel=C:\\temp\\myCustomKernel
memory=5GB # Limits VM memory in WSL 2 to 5 GB
processors=5 # Makes the WSL 2 VM use two virtual processors

The WSL resources are restricted to 5GB of memory and 5 processors

  • Save the file and close it
  • Stop all instances of WSL by running wsl --shutdown
  • Wait for the 8 second rule for the new configuration settings to appear, if you launch WSL immediately the new configuration settings won't appear
  • Restart WSL

Upon successful configuration one can check the custom resources by typing htop and viewing the # of processors and memory allocation. Below is an image of the same.
WSLConfig

  • A basic example of the .wslconfig file limiting the WSL resources to 5GB of memory and 5 processors is here
  • For a more detailed configurability through .wslconfig refer here

wsl.conf (Retrieved from Microsoft Docs)

  • Stored in the /etc directory of the distribution as a unix file
  • Used to configure settings on a per-distribution basis. Settings configured in this file will only be applied to the specific Linux distribution that contains the directory where this file is stored
  • Can be used for distributions run by either version, WSL 1 or WSL 2
  • Navigate to /etc directory by cd / (goes to the root directory) and then cd /etc
  • The wsl.conf file must be placed here

The above wsl.conf instructions are here

The wsl.conf file gives one the ability to configure a lot of setting and more details can be found here

Commands

Re-installing default SSL certificates

sudo dpkg-reconfigure ca-certificates

Check cpu stat

lscpu

Check for all WSL releases

wsl -l -o

Check for all WSL installed

wsl –l -v

Install wsl from powershell

wsl --install -d <distribution name>

Delete a distribution

wsl --unregister <distribution name>

Always login as root

ubuntu config --default-user root

Change it back to user

ubuntu config --default-user <username>

Switch to root user

su -

Setting a default WSL

wsl -s Ubuntu-20.04

Checking the Ubuntu release

lsb_release –a

Checking WSL memory consumption

free -h

Releasing cache memeory

sudo sh -c "echo 3 > /proc/sys/vm/drop_caches"

If the above doesn't work

  • STEP 1
  • alias drop_cache="sudo sh -c \"echo 3 >'/proc/sys/vm/drop_caches' && swapoff -a && swapon -a && printf '\n%s\n' 'Ram-cache and Swap Cleared'\""
  • STEP 2
  • source ~/.bashrc
  • STEP 3
  • drop_cache

Install Powershell in Ubuntu

sudo apt update 

sudo apt install -y wget apt-transport-https software-properties-common 

wget -q https://packages.microsoft.com/config/ubuntu/$VERSION_ID/packages-microsoft-prod.deb 

sudo dpkg -i packages-microsoft-prod.deb 

rm packages-microsoft-prod.deb 

sudo apt update 

sudo get install -y powershell 

pwsh 

VScode specific commands

VS Code Server for WSL closed unexpectedly

mv ~/.vscode-server ~/.vscode-server-bkup
# Restart VScode in WSL 
rm -rf ~/.vscode-server/extensions/ 
mv ~/.vscode-server-bkup/extensions/ ~/.vscode-server/extensions/