Windows Subsystem for Linux (Windows)

0. References

Basic commands for WSL

https://learn.microsoft.com/en-us/windows/wsl/basic-commands

WSL (Ubuntu wiki)

https://wiki.ubuntu.com/WSL

WSLg (Run Linux GUI apps on the Windows Subsystem for Linux)

https://learn.microsoft.com/en-us/windows/wsl/tutorials/gui-apps


1. WSL installation & configuration

Note: Most wsl commands must be executed as a regular user (not Administrator); otherwise you'll get an "Access denied" message.

If you are local administrator, activate UAC (it's enough any level over 'Never notify') to be able to open terminals without Administrator privileges.


1.80. Amazon Linux 2022 (install)

Choose the proper download: AL2022.zip

https://github.com/yosukes-dev/AmazonWSL


1.90. Amazon Linux 2 (install)

Choose the proper download: Amazon2.zip

https://github.com/yosukes-dev/AmazonWSL/tree/2


2. WSL command line

Distributions installed with state and WSL version

wsl -l -v

Remove a distribution from WSL and delete all of the data associated

wsl --unregister <distroName>

Export & Import

Let's 1st install a distro that will later use for exporting and importing it

wsl.exe --install -d Debian

It's possible to see where the distro has been installed (BasePath), eg:

PS> Get-ChildItem "HKCU:\Software\Microsoft\Windows\CurrentVersion\Lxss" -Recurse


Name                           Property

----                           --------

{8d5b10d4-3c52-4bf5-93ab-3ce6e State              : 1

28ea406}                       DistributionName   : Ubuntu

                               Version            : 2

                               BasePath           : C:\Users\mysername\AppData\Local\Packages\CanonicalGroupLimited.Ubuntuon

                               Windows_79rhkp1fndgsc\LocalState

                               Flags              : 15

                               DefaultUid         : 1000

                               PackageFamilyName  : CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc

                               KernelCommandLine  : BOOT_IMAGE=/kernel init=/init

                               DefaultEnvironment : {HOSTTYPE=x86_64, LANG=en_US.UTF-8,

                                                    PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/...

{bdb8fc95-5e65-4efd-aca2-565d0 State             : 1

076f1fd}                       DistributionName  : Debian

                               Version           : 2

                               BasePath          : C:\Users\myusername\AppData\Local\Packages\TheDebianProject.DebianGNULinux

                               _76v4gfsz19hv4\LocalState

                               Flags             : 15

                               DefaultUid        : 1000

                               PackageFamilyName : TheDebianProject.DebianGNULinux_76v4gfsz19hv4

Export the distribution as a .tar file

wsl --export Debian wsl-debian-20230916.tar

or (UNTESTED)

wsl --export --vhd Debian wsl-debian-20230916.vhdx

Unregister the distribution, so that we can import it later

wsl --unregister Debian

Import the .tar into the same BasePath where it was before

wsl --import Debian  C:\Users\myusername\AppData\Local\Packages\TheDebianProject.DebianGNULinux_76v4gfsz19hv4\LocalState   .\wsl-debian-20230916.tar

Import the .tar intro a new BasePath

md D:\wslDistroStorage

md D:\wslDistroStorage\MyDebian

wsl --import Debian D:\WSL_DISTROS\MyDebian   .\wsl-debian-20230916.tar


Alternatively, copy the ext4.vhdx file in the BasePath directory and import it inplace (UNTESTED):

wsl --import-in-place Debian ext4.vhdx

3. Graphical applications

3.1.  WSLg (GUI application support)

At time of writing, WSLg is supported in Windows 11 and there aren't plans to support it in windows 10.


3.2. X server on Windows

Install X servers for Windows and start it before entering the WSL environment: 

VcXsrv - https://sourceforge.net/projects/vcxsrv/

Click desktop icon "XLaunch", click Next, click Next and in the "Extra setting" toggle on "Disable access control"


3.3. WSL2

Windows 11 uses WSLg out of the box (the following configuration, if it exists, must be removed).

Windows 10 needs a running X server. If graphical applications can't connect automatically, paste the following commands to the terminal before starting the applications or set them on every login by adding them to ~/.bashrc:

export DISPLAY=$(awk '/nameserver / {print $2; exit}' /etc/resolv.conf 2>/dev/null):0 # WSL 2

export LIBGL_ALWAYS_INDIRECT=1


4. Windows Terminal

The Windows Terminal is a modern, fast, efficient, powerful, and productive terminal application for users of command-line tools and shells like Command Prompt, PowerShell, and WSL. Its main features include multiple tabs, panes, Unicode and UTF-8 character support, a GPU accelerated text rendering engine, and custom themes, styles, and configurations. 


Install from Microsoft Store:

https://aka.ms/terminal

5. Other WSL related info

5.1. $HOME/.bashrc

# .local/bin

export PATH="$HOME/.local/bin:$PATH"


# INI WSL

export DISPLAY=$(awk '/nameserver / {print $2; exit}' /etc/resolv.conf 2>/dev/null):0 # WSL 2

export LIBGL_ALWAYS_INDIRECT=1

# END WSL


#JDK 11

export JDK_HOME=/usr/lib/jvm/java-1.11.0-openjdk-amd64 && export JAVA_HOME=$JDK_HOME

#JDK 8

#export JDK_HOME=/usr/lib/jvm/java-8-oracle && export JAVA_HOME=$JDK_HOME


5.2. systemd vs SysVinit

Ubuntu under WSL uses, by default, SysVinit instead of systemd. Systemd equivalent sysvinit commands:

 https://linuxhandbook.com/system-has-not-been-booted-with-systemd/

For enabling systemd:

https://ubuntu.com/blog/ubuntu-wsl-enable-systemd


5.2. Eclipse and Spring Boot Suite (STS)

See page 'Eclipse IDE'

https://sites.google.com/site/pawneecity/eclipse-ide


5.3. Visual Studio Code

Install the Visual Studio Code extension:

https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-wsl

And start it from WSL with:

code .


5.4. Docker

https://sites.google.com/site/pawneecity/docker/podman-docker

Note: It requires paid subscription for enterprises with 250 employees or $10 million in annual revenue

https://docs.docker.com/docker-for-windows/wsl/


5.5. PostgreSQL

The idea is having the DBMS in WSL and the UI tools in Windows (eg: DBeaver), as stated at:

https://harshityadav95.medium.com/postgresql-in-windows-subsystem-for-linux-wsl-6dc751ac1ff3


To set the password for postgres, type:

sudo passwd postgres

Close and reopen the terminal.


To start the service, type:

sudo service postgresql start

    

To connect to postgres, type:

sudo -u postgres psql

6. Troubleshooting

6.1. Unable to access ports

If unable to access ports in wsdl from windows, shutting down and restarting wsl might fix it.

jdoe@BC27IB:~$ wsl.exe --shutdown

[process exited with code 1]


6.2. Startup fails w/ code 4294967295

If unable to start WSL on Windows 11 because of error "process exited with code 4294967295" then run on a cmd console as administrator (reboot computer afterwards):

netsh winsock reset

Output of command:

Sucessfully reset the Winsock Catalog.

You must restart the computer in order to complete the reset.

Reference (and other alternative solutions):

https://github.com/microsoft/WSL/issues/5092


6.3. Unable to upgrade systemd [dpkg-deb: error: paste subprocess was killed by signal (Broken pipe)]

Temporary solution is to set systemd=false in /etc/wsl.conf and the run wsl.exe --shutdown

the run sudo apt update or whatever

then enable systemd again


6.4. Error "cannot execute binary file: Exec format error"

Solution is:

sudo nano /usr/lib/binfmt.d/WSLInterop.conf

with content:

:WSLInterop:M::MZ::/init:PF

and execute from a 'cmd' console:

wsl --shutdown