#+TITLE: WSL2 installation for Windows 10/11 #+SUBTITLE: With Windows Terminal #+DESCRIPTION: Notes on how to setup working linux terminal emulator in Windows 10 #+KEYWORDS: windows 10, wsl, wsl2 # Export underscore as underscore instead of highlight in HTML: #+OPTIONS: ^:nil num:nil # Include usable macros from https://github.com/fniessen/org-macros: #+INCLUDE: ../org-mode/macros/org-macros.setup #+INFOJS_OPT: view:showall ltoc:nil path:https://iki.fi/~pyyhttu/debian/org-mode/js/org-info.js #+HTML_HEAD: * Philosophy Largely as per https://github.com/nickjj/dotfiles * Install WSL2 and Debian for it Install WSL2 and Debian distribution as per steps in [[https://docs.microsoft.com/en-us/windows/wsl/install-manual][MS Docs]]. Here's [[https://www.debian.org/doc/manuals/debian-reference/index.en.html][Debian reference]]. #+begin_note *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 [[https://answers.microsoft.com/en-us/windows/forum/all/code-error-0x80073cfb/25de0471-0f47-4269-9a35-95f36074fa4f][these steps]]. #+end_note Try to bypass store completely next time, see pointers [[https://superuser.com/questions/1731365/updating-wsl-2-without-microsoft-store][here]]. #+begin_note *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. #+end_note * Get Windows Terminal To be up-to-date with latest stable, install terminal from [[https://www.microsoft.com/en-us/p/windows-terminal/9n0dx20hk701?rtc=1&activetab=pivot:overviewtab][Windows Store]]. Another terminal to try could be [[https://www.reddit.com/r/neovim/comments/wmr9bs/what_terminal_emulator_do_you_use/][Wezterm.]] * Execute WSL2 DNS fix https://github.com/microsoft/WSL/issues/5420#issuecomment-646479747 * Get =resolv.conf=, =wsl.conf= and =.bashrc= For display server to work correctly (=$DISPLAY=), it requires DNS to resolve correctly. WSL writes at its install time =/etc/resolv.conf= with your current DNS-provider. If you install WSL while e.g. in your corporate network, then you may get your corporate's nameserver and DNS. ~$ cd /etc~ ~$ sudo wget https://pyyhttu.kapsi.fi/windows/wsl/etc/wsl.conf~ ~$ sudo wget https://pyyhttu.kapsi.fi/windows/wsl/etc/resolv.conf~ Setting correct DNS nameserver also ensures setting display server and thus =$DISPLAY= correctly so that we can later use in Neovim ~xclip~ as the [[https://github.com/neovim/neovim/blob/master/runtime/doc/provider.txt#L192C1-L193C31][clipboard provider]]. Clipboard support can be checked in ~nvim~ with =:checkhealth= and should return: =- OK Clipboard tool found: xclip= =$DISPLAY= is defined in ~.bashrc~: +~$ cd ~ && wget https://pyyhttu.kapsi.fi/debian/bash/.bashrc ~/.bashrc~+ *[2025-01-02 Thu]*: =export DISPLAY= is [[https://superuser.com/questions/1580610/how-to-run-gui-apps-with-windows-subsystem-for-linux/1758743#1758743][no longer needed]] as running graphical applications is taken care by wslg. * Execute setcap ping fix If I do ~$ ping www.google.com~, I get: #+begin_src bash ~$ ping www.google.com ping: socktype: SOCK_RAW ping: socket: Operation not permitted ping: => missing cap_net_raw+p capability or setuid? pyyhttu@5FS3T44:~$ ls -l /bin/ping ping ping4 ping6 pyyhttu@5FS3T44:~$ ls -l /bin/ping -rwxr-xr-x 1 root root 90568 Nov 27 2022 /bin/ping pyyhttu@5FS3T44:~$ getcap /bin/ping pyyhttu@5FS3T44:~$ sudo setcap cap_net_raw+p /bin/ping [sudo] password for pyyhttu: pyyhttu@5FS3T44:~$ getcap /bin/ping /bin/ping cap_net_raw=p pyyhttu@5FS3T44:~$ ping www.google.com PING www.google.com (216.58.210.132) 56(84) bytes of data. 64 bytes from mad06s09-in-f4.1e100.net (216.58.210.132): icmp_seq=1 ttl=58 time=15.8 ms #+end_src Fix that by setting setcap ping fix: https://github.com/microsoft/WSL/issues/5109#issuecomment-616320311 * Upgrading WSL2 To upgrade wsl2, launch Powershell and run: ~$ .\wsl.exe --update~ #+BEGIN_SRC bash 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> #+END_SRC 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. #+BEGIN_SRC bash 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> #+END_SRC #+BEGIN_note As per [[https://superuser.com/questions/1709437/how-can-i-update-the-kernel-in-wsl2-kernel-to-latest-release][this]], the presence of a later version on Github does not mean that it was yet incorporated into the Stable version. #+END_note * 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= Open windows terminal first from Store, then go to Settings and 'Open JSON file' so that Appdata folder is created. Then navigate to created AppData folder and get the config file: ~$ 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 [[https://nickjanetakis.com/blog/the-tools-i-use][what other tools to install]]. * DONE Install Firacode Nerd Font for the Terminal Install =FiraCodeNerdFont-Regular.ttf=. * DONE Copy & paste to work from/to Windows Terminal We'll want to set =$DISPLAY= so that we can use for neovim either ~xclip~ or ~xsel~ as the [[https://github.com/neovim/neovim/blob/master/runtime/doc/provider.txt#L192C1-L193C31][the clipboard provider]]. 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 [[https://nickjanetakis.com/blog/a-linux-dev-environment-on-windows-with-wsl-2-docker-desktop-and-more][this video]]. ** Alternatively, experiment with clipboard support offered by =win32yank= See [[https://www.reddit.com/r/neovim/comments/v8grlq/system_clipboard_for_neovim_in_wsl/][here]] and [[https://www.reddit.com/r/neovim/comments/vxdjyb/neovim_in_wsl2_cant_copypaste_tofrom_system/][here]] for more info. [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 {{{kbd(shift-ctrl-c)}}}/{{{kbd(shift-ctrl-v)}}} as well as mouse selection and right click. * DONE Install nvim and org-mode As per =..debian/neovim/neovim.html=. * REJECTED 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)~ *[2025-03-07 Fri]*: Rejected this. Neovim with nvim-orgmode has worked well enough. See [[../neovim/neovim.html][neovim/neovim.html]]. ** REJECTED Clean from script these #+BEGIN_SRC 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. #+END_SRC Check contents matches with ~/. files, then remove link. Then, delete everything zshell related. #+BEGIN_SRC 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 #+END_SRC *[2025-03-07 Fri]*: Rejected this. Neovim with nvim-orgmode has worked well enough. See [[../neovim/neovim.html][neovim/neovim.html]]. * DONE Install and configure emacs for vim org-export As per: https://pyyhttu.kapsi.fi/debian/emacs/emacs.org * 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~ [[https://www.reddit.com/r/bashonubuntuonwindows/comments/fj0jpf/xdgopenwsl_a_wslspecific_xdgopen_replacement_to/][~xdg-open-wsl~]] is an ~xdg-open~ (provided by =xdg-utils=) 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, [[https://scribblesandsnaps.wordpress.com/2017/01/20/show-geotagged-photos-on-openstreetmap/][~show-on-osm~]] works under WSL2. * DONE Install [[https://github.com/4U6U57/wsl-open][~wsl-open~]] To open in WSL any file/link there with native windows executable. Install by doing: ~$ curl -o ~/.local/bin/wsl-open https://raw.githubusercontent.com/4U6U57/wsl-open/master/wsl-open.sh~ ~$ sudo chmod +x ~/.local/bin/wsl-open~ ** DONE Install docker environment inside WSL2 As per video [[https://nickjanetakis.com/blog/install-docker-in-wsl-2-without-docker-desktop][here]]. #+begin_note If running Debian Sid, the [[https://docs.docker.com/engine/install/debian/#install-using-the-convenience-script][Debian convenience-script]] may fail with =E: The repository 'https://download.docker.com/linux/debian bookworm Release' does not have a Release file.=. #+end_note Work this around by editing =/etc/os-release= and =/etc/debian_version= and replacing there =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~ * DONE Define =.wslconfig= to avoid memory leakage / high CPU usage If not defined, wsl2 will consume memory because of wsl2 [[https://github.com/microsoft/WSL/issues/6982][bug 6982]]. This becomes evident when in Task Manager (press {{{kbd(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 [[https://www.minitool.com/news/vmmem-high-memory.html][this.]] To avoid that in the first place, define at =C:\Users\your-username\.wslconfig= the maximum amount of memory allowed for wsl2. See Nick Janetakis's [[https://nickjanetakis.com/blog/a-linux-dev-environment-on-windows-with-wsl-2-docker-desktop-and-more][video]] for more info from timestamp 25:14 onward. *[2023-02-28 Tue]*: This is fixed in [[https://github.com/microsoft/WSL/issues/6982#issuecomment-1446218436][Windows 11 22H2.]] Confirmed after upgraded to Win11 and the issue is no longer witnessed. *[2025-01-02 Thu]*: Also, set in =.wlsconfig= now =guiApplications= back to =true=. * DONE Error code: Wsl/Service/0x80040326 This started abruptly one morning. [[https://github.com/microsoft/WSL/issues/9867][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: #+begin_src bash wsl: A localhost proxy configuration was detected but not mirrored into WSL. WSL in NAT mode does not support localhost proxies. #+end_src 2. [[https://github.com/microsoft/WSL/issues/10753#issuecomment-1814839310][Fix the error]] by editing =.wslconfig=, add: #+BEGIN_SRC bash [experimental] autoMemoryReclaim=gradual # gradual | dropcache | disabled networkingMode=mirrored dnsTunneling=true firewall=true autoProxy=true #+END_SRC Run Powershell and issue: ~wsl --shutdown~ Then restart wsl-terminal. No more proxy error.