Windows Subsystem for Linux (WSL)

Windows Subsystem for Linux Revisited

Published 2018-08-20. Last modified 2024-10-25.
Time to read: 3 minutes.

This page is part of the posts collection, categorized under Git, Ubuntu, WSL.

I’ve been using Windows Subsystem for Linux (WSL) headless since it was first released with Windows 10 version 1607 in August 2016. The April 2018 release of Windows 10 (version 1803) significantly improved WSL.

It is possible to work with Ubuntu graphically on a vanilla Windows machine. No special drivers are required. No special Linux or Ubuntu support is required from the computer vendor.

This is my setup for running an X client like xeyes or IntelliJ IDEA from WSL or WSL2, accessed via a Windows X server like VcXsrv. These scripts assume Ubuntu 18.04 was installed under WSL.

Desktop showing Windows Subsystem for Linux (WSL)

Configuration

This is my WSL configuration file, in my Windows home directory (%systemdrive%%homepath%\.wslconfig). See Advanced settings configuration in WSL for more information.

%systemdrive%%homepath%\.wslconfig
# See https://docs.microsoft.com/en-us/windows/wsl/wsl-config#configure-global-options-with-wslconfig
# See https://github.com/microsoft/WSL/issues/4166
[wsl2]
memory=8GB
swap=60GB
#processor=4
localhostForwarding=true

[experimental]
autoMemoryReclaim=drop
sparseVhd=true

2024-02-20 Update

The WSL v2.1.3 prerelease sets the the default memory reclamation mode to dropcache. The following is now a default setting:
%systemdrive%%homepath%\.wslconfig
wsl2.autoMemoryReclaim=drop
The memory will be freed using dropcache after 10 minutes of inactivity. To upgrade WSL using bash, cmd or powershell:
bash, cmd or powershell
wsl.exe --update --pre-release
Wait a few minutes before restarting a terminal session. It is not necessary to reboot after upgrading.

Upgrading from Ubuntu 19.11 to Ubuntu 20.04

Update Aug 17, 2020: Here are my notes on upgrading WSL and WSL2 from Ubuntu 19.11 to Ubuntu 20.04.

Display whether you have WSL1 or WSL2 by typing wsl --list --verbose into a PowerShell prompt, like this:

PowerShell or Cmd
PS C:\Users\mslin>wsl --list --verbose
  NAME   STATE     VERSION
* Ubuntu Running   1 

The above shows that a machine has WSL1 installed.

Ignore this step if you have WSL2.
The default Ubuntu sleep command does not work on WSL1. sleep must work properly in order for the upgrade to succeed. To fix the problem, replace /bin/sleep with an equivalent Python 3 program prior to running do-release-upgrade. This will avoid the problem sleep: cannot read realtime clock: Invalid argument described in this askubuntu posting. To do this, type:

Shell
$ sudo mv /bin/sleep{,-} # renames sleep to sleep-
$ sudo vi /bin/sleep # I use vi, but you can use any editor with sudo

Enter the code for /bin/sleep shown below using your favorite editor:

/bin/sleep
#!/usr/bin/env python3
import sys
import time

time.sleep(int(sys.argv[1]))

Save the file. Now make your replacement for sleep executable:

Shell
$ sudo chmod a+x /bin/sleep

Ensure that all previous upgrades have been applied:

Shell
$ sudo apt upgrade

Verify that the last line of /etc/update-manager/release-upgrades is Prompt=normal. If not, edit with your favorite editor:

Shell
$ sudo vi /etc/update-manager/release-upgrades

Run the upgrade. Because Ubuntu rolls out its upgrade to users gradually, you might have to try this several times over a few days. Some of my machines update earlier than others, even though they are in the same building. You may find instructions elsewhere that advocate "Force direct upgrade by using the -d switch". This is incorrect. There is no such thing as a direct upgrade, instead the -d switch installs a development version, which means you will forever after install a whole lot of prerelease software. Don't do that unless you have a good reason. This is what most users should type:

Shell
$ sudo do-release-upgrade

The installation will die after a while with the following error:

Error message
dpkg: error processing package libc6:amd64 (--configure):
installed libc6:amd64 package post-installation script subprocess returned error exit status 1
Errors were encountered while processing:
libc6:amd64

To fix the problem (from https://stackoverflow.com/a/61697214/553865), edit /var/lib/dpkg/info/libc6:amd64.postinst and comment out this line near the top of the file:

lib/dpkg/info/libc6:amd64.postinst
set -e

so it looks like this:

lib/dpkg/info/libc6:amd64.postinst
# set -e

Now type:

Shell
$ sudo apt -f install

To conclude the upgrade, type:

Shell
$ sudo apt upgrade --fix-missing
$ sudo apt upgrade
$ sudo apt-get dist-upgrade -y
$ sudo apt autoremove
$ sudo apt autoclean

Run Associated Windows Program

To run the Windows program associated with a file, preface the file with cmd.exe /C start.

For example, to run the default Windows video viewer for .mkv files, type:

Shell
$ cmd.exe /C start my_file.mkv

I defined a bash alias to make this easier:

~/.bash_aliases
alias run='cmd.exe /C start'

Reload the bash aliases after editing ~/.bash_aliases:

Shell
$ source ~/.bash_aliases

Now use the run alias to run the default Windows video viewer for .mkv files like this:

Shell
$ run my_file.mkv
* indicates a required field.

Please select the following to receive Mike Slinn’s newsletter:

You can unsubscribe at any time by clicking the link in the footer of emails.

Mike Slinn uses Mailchimp as his marketing platform. By clicking below to subscribe, you acknowledge that your information will be transferred to Mailchimp for processing. Learn more about Mailchimp’s privacy practices.