WSL - wanglab-neuro/jupyterlab_server GitHub Wiki

Enable WSL2 / Docker

Best practice: https://docs.microsoft.com/en-us/windows/wsl/setup/environment
Commands: https://docs.microsoft.com/en-us/windows/wsl/setup/environment#basic-wsl-commands
See also:
https://towardsdatascience.com/how-to-improve-docker-performance-with-wsl2-3a54402ab0f2
https://www.createit.com/blog/slow-docker-on-windows-wsl2-fast-and-easy-fix-to-improve-performance/
https://levelup.gitconnected.com/docker-desktop-on-wsl2-the-problem-with-mixing-file-systems-a8b5dcd79b22

Install WSL on non-system drive: https://kontext.tech/column/tools/308/how-to-install-windows-subsystem-for-linux-on-a-non-c-drive
Invoke-WebRequest -Uri https://aka.ms/wsl-ubuntu-2004-lts -OutFile Ubuntu.zip -UseBasicParsing

Reset and multiple instances:
https://sungkim11.medium.com/why-you-should-use-multiple-instances-of-same-linux-distro-on-wsl-windows-10-f6f140f8ed88

List and config distros

https://docs.microsoft.com/en-us/windows/wsl/wsl-config

Move distro

wsl -l
wsl --export Ubuntu-20.04 Ubuntu-20.04-LTS.tar
wsl --unregister Ubuntu-20.04
mkdir Ubuntu2004
wsl --import Ubuntu-20.04 .\Ubuntu2004\ .\Ubuntu-20.04-LTS.tar
wsl -s Ubuntu-20.04
wsl -l

https://blog.iany.me/2020/06/move-wsl-to-another-drive/

Step 3. Set the default user for the moved Ubuntu.  

In Step 1, you have created a user for Ubuntu. After export and import, the new instance will use root by default. If you want to continue to use that user, please configure it via registry table.

Find the directory in registry HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Lxss which DistributionName is “Ubuntu”. Set its DefaultUid to decimal 1000 (or hex 3e8).

wsl -d Ubuntu-20.04

Same thing for Docker:

wsl --shutdown
wsl --export docker-desktop-data docker-desktop-data.tar
wsl --unregister docker-desktop-data
mkdir Docker
wsl --import docker-desktop-data J:\Docker\ docker-desktop-data.tar --version 2

Extend WSL disk beyond initial maximum size of 256GB

https://docs.microsoft.com/en-us/windows/wsl/vhd-size
Step 6 may require reboot to work

Manage WSL2 resources and set limits

https://docs.microsoft.com/en-us/windows/wsl/wsl-config https://ryanharrison.co.uk/2021/05/13/wsl2-better-managing-system-resources.html https://itnext.io/wsl2-tips-limit-cpu-memory-when-using-docker-c022535faf6f shutdown WSL: wsl --shutdown
Open config file: notepad "$env:USERPROFILE/.wslconfig"
Add configuration. E.g.:

[wsl2]
memory=30GB   # Limits VM memory in WSL 2 up to 30GB
processors=4 # Makes the WSL 2 VM use four virtual processors
swapfile=J:\\WSL\temp\\wsl-swap.vhdx # Sets swapfile path location, default is %USERPROFILE%\AppData\Local\Temp\swap.vhdx

GPU

https://docs.docker.com/desktop/windows/wsl/ -> GPU
See full doc here: https://docs.nvidia.com/cuda/wsl-user-guide/index.html
If using Windows 10, register it with the Insider program and build version 20145 or higher (21H2 seems to work, though: https://docs.microsoft.com/en-us/windows/whats-new/whats-new-windows-10-version-21h2#gpu-compute-support-for-the-windows-subsystem-for-linux)

Normally, CUDA toolkit for Linux will have the CUDA driver for NVIDIA GPU packaged with it. On WSL2, the CUDA driver used is part of the Windows driver installed on the system and therefore care must be taken to not install this Linux driver as it will clobber your installation.

Removing previous install. Depending on install method:
sudo apt-get --purge remove cuda-11.5 (and/or any variant)
then sudo apt autoremove
Same for Cuda Toolkit:
sudo apt-get remove nvidia-cuda-toolkit sudo apt-get remove --auto-remove nvidia-cuda-toolkit sudo apt-get purge nvidia-cuda-toolkit Or
sudo /usr/local/cuda-11.5/bin/cuda-uninstaller
sudo /usr/bin/nvidia-uninstall
Check uninstall: nvcc --version

Install gcc if not installed: sudo apt install gcc

Install CUDA toolkit that matches the driver version.
If Matlab is in the picture, check GPU support: https://www.mathworks.com/help/parallel-computing/gpu-support-by-release.html
https://developer.nvidia.com/cuda-downloads?target_os=Linux&target_arch=x86_64&Distribution=WSL-Ubuntu&target_version=2.0
Check which WSL distro is used : uname -m && cat /etc/*release

  1. set up the CUDA network repository
    apt-key adv --fetch-keys http://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/7fa2af80.pub
    sudo sh -c 'echo "deb http://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64 /" > /etc/apt/sources.list.d/cuda.list'
    sudo apt-get update
    
    
  2. install CUDA
    sudo apt-get install -y cuda-toolkit-11-0

At this point, nvcc --version in WSL should still return empty, as WSL uses the Windows driver

Running Existing GPU Accelerated Containers on WSL 2:
https://docs.nvidia.com/cuda/wsl-user-guide/index.html#ch05-running-containers
Install NVIDIA Container Toolkit:

distribution=$(. /etc/os-release;echo $ID$VERSION_ID)
curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | sudo apt-key add -
curl -s -L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.list | sudo tee /etc/apt/sources.list.d/nvidia-docker.list
sudo apt-get update
sudo apt-get install -y nvidia-docker2   

Test nvidia-smi with the latest official CUDA image:
docker run --rm --gpus all nvidia/cuda:10.1-base nvidia-smi
(just to make sure, compare ouput to nvidia-smi)

Remote connection test:
docker run -it --gpus all -p 8888:8888 tensorflow/tensorflow:latest-gpu-py3-jupyter
Open a container terminal and install tensorflow docs

apt update
apt install git
pip install git+https://github.com/tensorflow/docs

Open the 8888 port on Windows firewall
Then connect remotely to the notebook, e.g.: http://:8888
Try !nvidia-smi

Backups

File Sync with Bash and Rsync (not version controlled, so not proper backup)
https://nickjanetakis.com/blog/automatic-offline-file-backups-with-bash-and-rsync
Incremental backups with rsnapshot
https://ubuntu.com/server/docs/tools-rsnapshot
https://manpages.ubuntu.com/manpages/focal/man1/rsnapshot.1.html
https://rsnapshot.org/rsnapshot/docs/docbook/rest.html#backup-script
Some WSL specific info, esp regarding starting CRON: https://scottiestech.info/2018/08/07/run-cron-jobs-in-windows-subsystem-for-linux/.
Best solution is to have windows start Cron on WSL at startup: https://www.howtogeek.com/746532/how-to-launch-cron-automatically-in-wsl-on-windows-10-and-11/.
Also, just in case, check that Cron is running:
service --status-all. If not: service cron start

Create a cron.log file to contain just the CRON entries that show up in syslog:
https://askubuntu.com/questions/56683/where-is-the-cron-crontab-log
Open the file /etc/rsyslog.d/50-default.conf
Find the line that starts with: #cron.*
Uncomment that line, save the file, and restart rsyslog: sudo service rsyslog restart
You should now see a cron log file here: /var/log/cron.log

CRON reference: https://cronitor.io/cron-reference

rsnapshot CRON command didn't work from crontab -e (user crontab), but worked from /etc/cron.d/rsnapshot

Run some tests, add commands to /etc/cron.d/rsnapshot:

*Test 1*  
* *        * * *       root    /usr/bin/rsnapshot -c /usr/local/bin/backup_scripts/test_snapshot.conf daily
*Test 2*  
32 15-16            * * *           root    /usr/bin/rsnapshot -c /usr/local/bin/backup_scripts/rsnapshot.jupyter.conf daily  
*Test 3*  
*/5 *          * * *           root    /usr/bin/rsnapshot -c /usr/  

Add a display

As of now, WSL2 isn't designed to run graphical applications. Which can be a problem to install GUI-required apps like Matlab.

The simplest solution to that is to upgrade to WSLg, which requires subscribing to the Windows insider program to access recent Windows builds (W11+). But may not be available.
https://insider.windows.com/en-us/getting-started

Otherwise use SSH to remote access WSL with X forwarding.
Install a desktop:
lxde https://www.makeuseof.com/tag/linux-desktop-windows-subsystem/
gnome https://gist.github.com/Ta180m/e1471413f62e3ed94e72001d42e77e22

Then add to bashrc

export DISPLAY=:0
export LIBGL_ALWAYS_INDIRECT=1

See https://askubuntu.com/questions/976744/open-a-folder-in-file-manager-in-win10-wsl-while-in-ubuntu-command-line
https://www.how2shout.com/linux/install-gui-file-manager-nautilus-on-wsl/

Used export DISPLAY=$(grep nameserver /etc/resolv.conf | awk '{print $2}'):0.0 instead of export DISPLAY=localhost:0.0. But the line above (export DISPLAY=:0) might be fine too.

Open file manager, navigate to unzipped folder, then double click on install file.v Since this is not done as root, can't install in usr directory. Use home instead.
Untested work-around: https://www.how2shout.com/linux/open-ubuntu-file-manager-as-root-user/

Install GUI apps

https://bestofcpp.com/repo/microsoft-wslg#install-and-run-gui-apps

Install Anaconda

https://linuxize.com/post/how-to-install-anaconda-on-ubuntu-20-04/

Install Matlab

https://www.how2shout.com/linux/how-to-install-matlab-in-ubuntu-20-04/

Mount ISO File:
Create the mount point directory: sudo mkdir /mnt/matlab_iso
Mount the ISO file: sudo mount -o loop /tmp/R2021b/R2021b_Linux.iso /mnt/matlab_iso
Verify it, run: mount OR df -H OR ls -l /mnt/iso/
Unmount the ISO file using: sudo umount /mnt/iso/

Copy files to another directory, change permissions
cp -r matlab_iso/ /tmp/R2021b/
find /tmp/R2021b/ -type d -exec chmod 755 {} ;

install in home directory then make simlink
sudo ln -s /home/wanglab/MATLAB/R2021b/bin/matlab /usr/local/bin/matlab

Issues

Network down (eth0) issue https://github.com/microsoft/WSL/issues/5336#issuecomment-907776406
Powershell as admin

wsl -l -v
wsl --setdefault <distro version> (e.g. Ubuntu-20.04)
wsl --set-version Ubuntu-20.04 1
start wsl and install net-tools
   sudo apt-get update
   sudo apt install net-tools

then switch back to WSL2
wsl --set-version Ubuntu-20.04 1

Also, don't set swapfile location (https://github.com/microsoft/WSL/issues/5336#issuecomment-911297983)

Finally, https://github.com/microsoft/WSL/issues/5336#issuecomment-984995551

# Fix network issues
# Delete auto-generated files
[root@PC-NAME user]# rm /etc/resolv.conf || true
[root@PC-NAME user]# rm /etc/wsl.conf || true

# Enable changing /etc/resolv.conf
# Enable extended attributes on Windows drives
[root@PC-NAME user]# cat <<EOF > /etc/wsl.conf
[network]
generateResolvConf = false

[automount]
enabled = true
options = "metadata"
mountFsTab = false
EOF

# Use google nameservers for DNS resolution
[root@PC-NAME user]# cat <<EOF > /etc/resolv.conf
nameserver 8.8.8.8
nameserver 8.8.4.4
EOF

Exit Linux WSL (<- issue fixed after that point)

cmd as admin:
wsl --shutdown
netsh winsock reset
netsh int ip reset all
netsh winhttp reset proxy
ipconfig /flushdns

Windows Search > Network Reset

Line endings messed up because file opened from Windows
This makes bash scripts fail.
For example, get ERROR: config_version 1.2 - config_version not recognized! when running test snapshot sudo rsnapshot -c ~/images/repos/jupyterlab_server/scripts/rsnapshot.jupyter.conf -t daily Fix with sed -i -e 's/\r$//' rsnapshot.jupyter.conf
Similar issue with .sh scripts.

Fix download speeds
install speedtest: sudo apt install speedtest-cli
https://townsyio.medium.com/wsl2-how-to-fix-download-speed-3edb0c348e29
Same as running Disable-NetAdapterLso -Name "vEthernet (WSL) in Powershell.

⚠️ **GitHub.com Fallback** ⚠️