Last updated: 2024-11-11 Mon 09:54

WSL2 installation for Windows 10/11
With Windows Terminal

Table of Contents

WSL2

Philosophy

Install WSL2 and Debian for it

Install WSL2 and Debian distribution as per steps in MS Docs.

Note: In step 4 above, instead installing the kernel with wsl.exe --install, consider doing wsl.exe --web-install. This will install from github, and hopefully do away the lag between the store version update. Reason: Fuck Microsoft store and its unambiguous "something happened on our end" -error codes (0x80073CFB). Edit: Looking at AppXDeployment-Server event log for the error code, we can see: AppX Deployment operation failed for package MicrosoftCorporationII.WindowsSubsystemForLinux_2.2.4.0_neutral_~_8wekyb3d8bbwe with error 0x80073CFB. The specific error text for this failure is: Deployment of package MicrosoftCorporationII.WindowsSubsystemForLinux_2.2.4.0_x64__8wekyb3d8bbwe was blocked because the provided package has the same identity as an already-installed package but the contents are different. Increment the version number of the package to be installed, or remove the old package for every user on the system before installing this package. Then… maybe wait. Edit: Waiting doesn't help, the store is fucked up and needs to be reset. Maybe try with these steps.

Try to bypass store completely next time, see pointers here.

Note: If I ever screw my install, I can reset my wsl environment in Win10 as per https://pureinfotech.com/reset-wsl2-linux-distro-windows-10 by doing:

  1. Settings - Apps - Apps & features
  2. Select the distribution - Advanced options
  3. Reset
  4. Then reinstall Debian as per steps in MS Docs.

Get Windows Terminal

To be up-to-date with latest stable, install terminal from Windows Store. Another terminal to try could be Wezterm.

Upgrading WSL2

To upgrade wsl2, launch Powershell as admin. Run:

Edit: Don't run anymore as admin as that will result after UAC-hardening with an error:

Installing: Windows Subsystem for Linux A specified logon session does not exist.

Reason is that with new I'm logged as admin as:

whoami knet\tpyyhtia_t3

vs. old local admin rights I was:

knet\tpyyhtia

So run with regular powershell with knet/tpyyhtia as:

$ .\wsl.exe --update

PS C:\WINDOWS\system32> .\wsl.exe --update
Checking for updates...
Downloading updates...
Installing updates...
This change will take effect on the next full restart of WSL. To force a restart, please run 'wsl --shutdown'.
Kernel version: 5.10.102.1
PS C:\WINDOWS\system32>

Do this after a new release is available at https://github.com/microsoft/WSL2-Linux-Kernel/releases/. New releases can be subscribed with rss at https://github.com/microsoft/WSL/tags?format=atom This will subscribe to linux-msft-wsl packages.

PS C:\Windows\System32> .\wsl.exe --version
WSL version: 2.0.14.0
Kernel version: 5.15.133.1-1
WSLg version: 1.0.59
MSRDC version: 1.2.4677
Direct3D version: 1.611.1-81528511
DXCore version: 10.0.25131.1002-220531-1700.rs-onecore-base2-hyp
Windows version: 10.0.22631.2861
PS C:\Windows\System32> whoami
knet\tpyyhtia
PS C:\Windows\System32>

As per this, the presence of a later version on Github does not mean that it was yet incorporated into the Stable version.

DONE Install aptitude, aptitude configs, get sid repositories, install

apt-listbugs, apt-listchanges, install updates, hstr, bash-completion and curl

$ sudo apt update && sudo apt install --no-install-recommends aptitude wget ca-certificates

$ sudo aptitude update && sudo aptitude safe-upgrade

$ cd /etc/apt/ && sudo wget https://pyyhttu.kapsi.fi/debian/aptitude/apt.conf

$ cd /etc/apt/ && sudo wget https://pyyhttu.kapsi.fi/debian/aptitude/sources.list

$ sudo aptitude update && sudo aptitude safe-upgrade

$ sudo aptitude full-upgrade

$ sudo aptitude install apt-listbugs apt-listchanges apt-transport-https

$ sudo aptitude install bash-completion curl

DONE Get .bashrc, .bash_history and .inputrc

$ cd ~ && wget https://pyyhttu.kapsi.fi/debian/bash/.bashrc ~/.bashrc

$ wget https://pyyhttu.kapsi.fi/debian/bash/.bash_history ~/.bash_history

$ wget https://pyyhttu.kapsi.fi/debian/bash/.inputrc ~/.inputrc

$ wget https://pyyhttu.kapsi.fi/debian/bash/.profile ~/.profile

$ wget https://pyyhttu.kapsi.fi/debian/bash/.hstr_blacklist ~/.hstr_blacklist

Restart Windows Terminal to resource the rc-files.

DONE Install rest of the of the bash toolstack

As per ../debian/bash/bash.html

DONE Get Windows Terminal settings.json

$ cd /mnt/c/Users/TPyyhtia/AppData/Local/Packages/Microsoft.WindowsTerminal_8wekyb3d8bbwe/LocalState/ && wget https://pyyhttu.kapsi.fi/windows/wsl/c/Users/TPyyhtia/AppData/Local/Packages/Microsoft.WindowsTerminal_8wekyb3d8bbwe/LocalState/settings.json

Lastly check what other tools to install.

DONE Copy & paste to work from/to Windows Terminal

Download package from https://sourceforge.net/projects/vcxsrv/

Open powershell and type in:

add-appxpackage -Path ".\Microsoft.WindowsTerminalPreview_1.6.10412.0_8wekyb3d8bbwe.msixbundle"

28:36 – Clipboard sharing between WSL 2 and Windows with VcXsrv

See from this video.

Alternatively, experiment with clipboard support offered by win32yank

See here and here for more inf.

Update [2024-11-11 Mon]: None of the above is needed. Copying/pasting to/from wsl and host works (also with nvim) out of the box with shift-ctrl-c/shift-ctrl-v as well as mouse selection and right click.

NEXT Install nvim and org-mode

As per ..debian/neovim/neovim.html

NEXT Install Debian reference

As per this.

NEXT Get vim with plugins

Once my local config is refactored, consider replacing script with own plugins ('jceb/orgmode' and 'tpope/speeddating.vim' plugins etc.)

$ bash <(curl -sS https://pyyhttu.kapsi.fi/windows/wsl/install)

NEXT Clean from script these

Installing zsh-users/zsh-autosuggestions...
Cloning into '/home/pyyhttu/.local/share/zsh-autosuggestions'...
remote: Enumerating objects: 2435, done.
remote: Counting objects: 100% (50/50), done.
remote: Compressing objects: 100% (42/42), done.
remote: Total 2435 (delta 21), reused 21 (delta 7), pack-reused 2385
Receiving objects: 100% (2435/2435), 566.38 KiB | 3.11 MiB/s, done.
Resolving deltas: 100% (1552/1552), done.

Check contents matches with ~/. files, then remove link. Then, delete everything zshell related.

ln -fs "/home/pyyhttu/dotfiles/.zshenv" "/home/pyyhttu/.zshenv"
ln -fs "/home/pyyhttu/dotfiles/.config/zsh/.zshrc" "/home/pyyhttu/.config/zsh/.zshrc"
ln -fs "/home/pyyhttu/dotfiles/.config/zsh/.zprofile" "/home/pyyhttu/.config/zsh/.zprofile"
ln -fs "/home/pyyhttu/dotfiles/.config/zsh/.aliases" "/home/pyyhttu/.config/zsh/.aliases"
ln -fs "/home/pyyhttu/dotfiles/.gitconfig" "/home/pyyhttu/.gitconfig"
ln -fs "/home/pyyhttu/dotfiles/.vimrc" "/home/pyyhttu/.vimrc"
ln -fs "/home/pyyhttu/dotfiles/.vim/spell/en.utf-8.add" "/home/pyyhttu/.vim/spell/en.utf-8.add"
ln -fs "/home/pyyhttu/dotfiles/.tmux.conf" "/home/pyyhttu/.tmux.conf"
ln -fs "/home/pyyhttu/dotfiles/.local/bin/update-zsh-plugins" "/home/pyyhttu/.local/bin/update-zsh-plugins"
ln -fs "/home/pyyhttu/dotfiles/.local/bin/set-theme" "/home/pyyhttu/.local/bin/set-theme"
sudo ln -fs "/home/pyyhttu/dotfiles/etc/wsl.conf" /etc/wsl.conf

DONE Install and configure emacs for vim org-export

DONE Create key-based (passwordless) login ssh-server

If you have a ssh provider, establish a passwordless login using the private-public key-pair. Generate public/private rsa key pair:

$ cd ~/.ssh && ssh-keygen -t rsa

Enter file in which to save the key (/home/user1/.ssh/id_rsa): Enter passphrase: (press enter for no passphrase) Enter same passphrase again: (press enter again)

Copy the public key to the server:

$ scp /home/user1/.ssh/id_rsa.pub user1@remote_server:~/

$ ssh user1@remote_server

$ cat ~/id_rsa.pub >> ~/.ssh/authorized_keys

DONE Install keepassxc-cli and xclip

$ sudo aptitude install keepassxc xclip

Operate like so:

$ keepassxc-cli clip /mnt/c/Users/TPyyhtia/Nextcloud/Documents/Database.kdbx 'Asus Login' 0

DONE Install xdg-open-wsl

xdg-open-wsl is an xdg-open replacement for WSL that opens files and links using Windows apps.

First, install dependencies:

$ sudo aptitude install python3 python3-pip

Then install with:

$ pip3 install --user --upgrade pip

If you get an error This environment is externally managed

Then also do:

$ sudo mv /usr/lib/python3.11/EXTERNALLY-MANAGED /usr/lib/python3.11/EXTERNALLY-MANAGED.old

Then install with:

$ pip install --user git+https://github.com/cpbotha/xdg-open-wsl.git

After above, show-on-osm works under WSL2.

DONE Install docker environment inside WSL2

As per video here.

If running Debian Sid, the Debian convenience-script may fail with E: The repository 'https://download.docker.com/linux/debian bookworm Release' does not have a Release file..

Work this around by editing /etc/os-release and /etc/debian_version and replacing bookworm with bullseye, or whatever the supported release is. Edit these back to what they were after $ sudo sh get-docker.sh has finished succesfully.

DONE Backing up and restoring WSL instance

In Powershell do:

$ wsl --list

$ wsl --shutdown debian

$ wsl --export Debian debian.tar

To restore WSL instance from a backup, in Powershell do:

$ wsl --export Debian C:\Users\TPyyhtia\Debian debian.tar

Troubleshooting

DONE Define .wslconfig to avoid memory leakage / high CPU usage

If not defined, wsl2 will consume memory because of wsl2 bug 6982. This becomes evident when in Task Manager (press ctrl-shift-esc) you can see Vmmem consuming all your memory and peaking CPU. Also Windows Terminal becomes unresponsive, eventually crashing. If that happens, resolve the situation like this.

To avoid that in the first place, define at c:\Users\your-username\.wslconfig the maximum amount of memory allowed for wsl2:

# Settings apply across all Linux distros running on WSL 2
[wsl2]

# Make sure you have this amount available to use!
# I put mine off my SSD, but this is optional.
memory=6GB # How much memory to assign to the WSL2 VM.

#processors=4 # How many processors to assign to the WSL2 VM. Default is "The same number of processors on Windows", thus commented out: https://github.com/microsoft/WSL/discussions/5943

#swapFile=C:\Users\TPyyhtia\wsl2-swap.vhdx

# Another option is to disable swap but if the VM goes out of memory, the
# OOM killer might start killing important processes or the VM may crash.
#
# Uncomment the line below this to use no swap file at all in the WSL 2 VM.
swap=0

# Apparently also needed to contain vmemm high cpu and mem, as per: https://www.tecklyfe.com/fix-vmmem-high-cpu-usage-in-windows/
guiApplications=false

# "wsl: A localhost proxy configuration was detected but not mirrored into WSL. WSL in NAT mode does not support localhost proxies."
# https://github.com/microsoft/WSL/issues/10753#issuecomment-1814839310

[experimental]
autoMemoryReclaim=gradual  # gradual  | dropcache | disabled
networkingMode=mirrored
dnsTunneling=true
firewall=true
autoProxy=true

See Nick Janetakis's video for more info from timestamp 25:14 onward.

Update: [2023-02-28 Tue] This is fixed in Windows 11 22H2. Confirmed after upgraded to Win11 and the issue is no longer witnessed.

DONE Error code: Wsl/Service/0x80040326

This started abruptly one morning. Github issue 9867 addressed the fix to update WSL in PowerShell with:

PS wsl --update which resolved this. Also updated in Windows terminal settings for Debian Starting directory: Use parent process directory.

DONE Error code: wsl: Havaittiin localhost-välityspalvelimen määritys, mutta sitä ei peilattu WSL:ään. WSL NAT-tilassa ei tue localhost-välityspalvelimia.

Error came up everytime I opened wsl terminal. Also, the error started to spam Windows notification center. Also, the error was in Finnish, making it impossible to google for.

Fixes were:

  1. To fix WSL errors into English, change in Windows Regional format to "English (Finland)". Also change format for "Short date" for more readable format "5 Apr 2017".

This way error is now reproduced in English, which we can google for:

wsl: A localhost proxy configuration was detected but not mirrored into WSL. WSL in NAT mode does not support localhost proxies.
  1. Fix the error by editing .wslconfig, add:
[experimental]
autoMemoryReclaim=gradual  # gradual  | dropcache | disabled
networkingMode=mirrored
dnsTunneling=true
firewall=true
autoProxy=true

Run Powershell and issue:

wsl --shutdown

Then restart wsl-terminal. No more proxy error.

Tuomas Pyyhtiä / CC BY-SA NVim 0.9.5 (nvim-orgmode 0.3.4)