How to build WSL2 Linux kernel
Yes, there is an official article, but it does not explain everything, especially the issues we may face during the process. Here I’ve the described the full process with the solutions to the common problems.
- In order to avoid errors caused by the case-sensitive paths collisions, enable case-sensitivity for the directory which will contain the WSL2-Linux-Kernel repository. Execute the following command in command line tool:
fsutil.exe file SetCaseSensitiveInfo "C:\path\to\directory\" enable
- Clone the WSL2-Linux-Kernel GitHub repository to the directory where you have enables case-sensitivity. In order to speed up the process, you can clone only the last commit:
git clone --depth 1 https://github.com/microsoft/WSL2-Linux-Kernel.git
- Install the dependencies as described in the official documentation. In order to avoid errors like
pahole (pahole) is not available, install dwarves. Execute the following command in Ubuntu:
sudo apt install build-essential flex bison libssl-dev libelf-dev dwarves
- Build the kernel as described in the official documentation. Execute the following command in Ubuntu:
cd WSL2-Linux-Kernel make KCONFIG_CONFIG=Microsoft/config-wsl
- Create a .wslconfig file in your user’s home directory. Put the configuration as described in the official documentation
Now you can upgrade from WSL1 to WSL2 like described in the official documentation. In command line tool:
wsl --set-version <distro name> 2
WSL files in Windows:
Reduce RAM usage by WSL2
WSL2 can consume huge amounts of RAM and not releasing it, even if it does not execute any task. In order to limit memory consumption by WSL2:
- create a file with name
.wslconfigin your user’s home directory
- add the following content to the
[wsl2] memory=10Gb swap=0
memory=10Gb means that 10Gb of RAM are assigned to the WSL2 VM. WSL2 will not use more than that.
swap=0 means that the swap file is not used.
Reference: WSL 2 Settings
With the setting above, WSL2 can still use almost 10Gb of RAM with no active tasks, but never more than 10Gb.
If you run some RAM consuming tasks in WSL2, for example, you test some functionality in the environment created by a set of docker containers, you may experience Out of memory errors, if you use all 10Gb of memory allocated to WSL2.
Resolve git clone .git/config.lock failed: Operation not permitted, fatal: could not set ‘core.filemode’ to ‘false’
The problem is caused by the fact that git is not allowed to use chmod.
As a workaround, execute the
git clone command with
sudo git clone ...
In order to permanenetly solve the issue, add the following lines to the file
[automount] options = "metadata"
and restart wsl.
Resolve Windows Docker Desktop failed to start error
Open a terminal (
cmd is fine) and execute a command:
wsl -l -v
In case you see this line in the output:
docker-desktop Uninstalling 2
execute the command:
wsl -t docker-desktop
In case the response is
There is no distribution with the supplied name., execute the command:
wsl --unregister docker-desktop
Check by executingthe command:
wsl -l -v
If the docker-desktop is no longer in the list, try to launch Docker Desktop. It should work now. There is no need in launching it as administrator.