#+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: * WSL2 ** 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]]. *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 https://www.microsoft.com/en-us/p/windows-terminal/9n0dx20hk701?rtc=1&activetab=pivot:overviewtab 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 ** Upgrading 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: #+BEGIN_SRC bash whoami knet\tpyyhtia_t3 #+END_SRC vs. old local admin rights I was: #+BEGIN_SRC knet\tpyyhtia #+END_SRC So run with regular powershell with knet/tpyyhtia as: ~$ .\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~ ~$ sudo aptitude update && sudo aptitude safe-upgrade~ ~$ cd /etc/apt/ && wget https://pyyhttu.kapsi.fi/debian/aptitude/apt.conf~ ~$ cd /etc/apt/ && wget https://pyyhttu.kapsi.fi/debian/aptitude/sources.list~ ~$ sudo aptitude install apt-listbugs apt-listchanges apt-transport-https~ ~$ sudo aptitude update && sudo aptitude safe-upgrade~ ~$ sudo aptitude install bash-completion~ ~$ sudo aptitude update && sudo aptitude install curl~ ** NEXT Install Debian reference As per [[https://news.ycombinator.com/item?id=34664868][this]]. ** DONE Get =.bashrc=, =.bash_history=, =.inputrc= and Solarized =.dircolors= ~$ wget https://pyyhttu.kapsi.fi/debian/bash/.bashr ~/.bashrc~ ~$ wget https://pyyhttu.kapsi.fi/debian/bash/.bash_history ~/.bashhistory~ ~$ wget https://pyyhttu.kapsi.fi/debian/bash/.inputrc ~/.inputrc~ ~$ wget https://pyyhttu.kapsi.fi/debian/bash/.dircolors ~/.dircolors~ ** 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 #+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 ** 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~ *** Alternatively, install mintty terminal emulator Create a shortcut to your Windows %homepath% with: Name: debian Target: =C:\Users\TPyyhtia\AppData\Local\wsltty\bin\mintty.exe --WSL= --configdir="C:\Users\TPyyhtia\AppData\Roaming\wsltty" -~ -= - In mintty settings set locale in =Options= =Locale= as fi_FI and =Character set= ~UTF-8~. Lastly check [[https://nickjanetakis.com/blog/the-tools-i-use][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 [[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. ** 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~ replacement for WSL that opens files and links using Windows apps. ~$ pip3 install --user --upgrade pip~ ~$ 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 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 ~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 [[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: #+BEGIN_SRC bash # 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 #+END_SRC 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. Update: [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. *** 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_EXAMPLE wsl: A localhost proxy configuration was detected but not mirrored into WSL. WSL in NAT mode does not support localhost proxies. #+END_EXAMPLE 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.