WSL Command

Windows Subsystem Linux (WSL)

Windows PowerShell 常用 WSL(2) command

目前 Windows 10 supports Ubuntu 18.04 LTS 和 Ubuntu 20.04 LTS

Install and Uninstall WSL (from Windows)

[@hucHowInstall2022] and [@hucHowUninstall2022]

Check WSL status (from Windows)

1
2
3
4
5
PS> wsl -l -v
PS> wsl --list --verbose 
  NAME            STATE           VERSION
* Ubuntu-18.04    Running         2
  Ubuntu-20.04    Running         2

可以看到 WSL distribution name (U18 or U20) 和 WSL version number (WSL1 or WSL2).

  • 目前正在執行 (running) 的 U18 WSL2 and U20 WSL2

  • 兩個 WSL 都是 WSL2 (version 2)

  • Default active 是 U18 WSL2, 因爲前面有 *.

    什麽是 default active? 就是在 “PS> bash” 從 PowerShell 切到 U18 WSL2, 但是仍然在同一個 directory.

Run/Stop WSL (from Windows)

Running: 簡單的方法是直接從 Windows Start Menu 打開 Ubuntu18 or Ubuntu20 icon,就可以把 Stopped WSL2 喚醒。

Stopped: 關掉 Ubuntu windows 不會馬上 Running -> Stopped. 但是等 30 秒就會 Stopped.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
PS> wsl -l -v 
  NAME            STATE           VERSION
* Ubuntu-20.04    Running         2
  Ubuntu-18.04    Stopped         2
  
# 此時打開 Windows Start Menu 的 Ubuntu18
PS> wsl -l -v 
  NAME            STATE           VERSION
* Ubuntu-20.04    Running         2
  Ubuntu-18.04    Running         2
      
# 此時關掉 Windows Start Menu 的 Ubuntu18
PS> wsl -l -v 
  NAME            STATE           VERSION
* Ubuntu-20.04    Running         2
  Ubuntu-18.04    Running         2

# 等了 30 seconds
PS> wsl -l -v 
  NAME            STATE           VERSION
* Ubuntu-20.04    Running         2
  Ubuntu-18.04    Stopped         2
從 PowerShell 開關 WSL 的方法:

Running -> Stopped

1
PS> wsl --terminate <Distro>

全部 stopped

1
PS> wsl --shutdown

Stopped -> Running

1
PS> wsl --distribution <Distro>

Stopped -> Running the default WSL: wsl (or bash)

1
PS> wsl

PowerShell 和 Ubuntu WSL bash 切換 (Windows <-> Ubuntu)

1
2
3
4
5
6
PS> wsl (or bash)

/mnt/c/Users$ exit
logout

PS> wsl
  • (Windows) PowerShell 切到 (Default Ubuntu) bash: wsl (or bash)
  • (Ubuntu) bash 回到 PowerShell: exit
  • Caveat1: bash (or wsl) 會切到 default WSL.
  • Caveat2: 切換 PowerShell and bash 都是在同一個 directory. 對於需要用 Linux command 非常方便。

Set a Default Linux Distribution (from Windows)

1
2
PS> wsl -l -v
PS> wsl --setdefault <Distro>

實例如下:

1
2
3
4
5
6
7
8
9
10
PS> C:\Users\allen\OneDrivewsl -l -v
  NAME            STATE           VERSION
* Ubuntu-18.04    Running         2
  Ubuntu-20.04    Running         2

PS> wsl --setdefault Ubuntu-20.04
PS> wsl -l -v
NAME            STATE           VERSION
* Ubuntu-20.04    Running         2
  Ubuntu-18.04    Running         2

Switch between WSL1 and WSL2 (from Windows)

一般我們都用 WSL2:WSL2 是 WSL1 的更新版本,支持所有的 linux system call, 又快了 20%. 沒有什麽理由用 WSL1. Don’t do it!

1
2
PS> wsl -l -v
PS> wsl --set-version [Distro] [Version]

Ubuntu (WSL2) bash 常用 command

確認目前是在哪一個 WSL2 (from Ubuntu)

Ubuntu 20.04:

1
2
3
4
5
6
7
$ lsb_release -a

No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.5 LTS
Release:        20.04>
Codename:       focal

Ubuntu 18.04:

1
2
3
4
5
6
7
$ lsb_release -a

No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 18.04.6 LTS
Release:        18.04
Codename:       bionic

How To Upgrade Existing WSL/WSL2 Ubuntu 18.04 to 20.04

How To Upgrade Existing WSL/WSL2 Ubuntu 18.04 to 20.04 - NEXTOFWINDOWS.COM

password is axxxxxxz

1
2
3
sudo apt update
sudo apt list --upgradable
sudo apt upgrade

Then clean up package source and remove any unused packages.

U18/U20

1
2
3
4
5
$ sudo apt --purge autoremove
Reading package lists... Done
Building dependency tree
Reading state information... Done
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

Auto Upgrade to New Packages (Not recommend)

It’s important to install this update manager core package this will trick the system to think there is a new LTS available and allow you to do an in place upgrade.

1
2
3
sudo apt install update-manager-core
sudo do-release-upgrade
sudo do-release-upgrade -d
1
2
    $ conda install scikit-learn-intelex
    $ python -m sklearnex my_application.py

Install CUDA for AI

Reference: [@dkHowInstall2022]

  1. 更新win系统 to 2022H2

  2. Download Nvidia’s Windows driver (510.60.02) and CUDA (11.6) for graph card and support WSL! Download NVIDIA, GeForce, Quadro, and Tesla Drivers

  3. Install WSL2

  4. Check if graph card is OK in WSL2: graphic driver (510.60.02) and CUDA (11.6)

    upgrade to driver 516.94, CUDA 11.7

1
$ nvidia-smi

image-20221107215455615

image-20230210230754445

  1. Install Anaconda (2022/10, Python 3.9)
1
2
$ wget https://repo.anaconda.com/archive/Anaconda3-2022.10-Linux-x86_64.sh
$ bash Anaconda3-2022.10-Linux-x86_64.sh

Clone environment jax and install jax. use the CPU version since not for serious computing!

1
2
3
4
(base) $ conda create -n jax --clone base
(base) $ conda activate jax
(jax) $ pip install --upgrade pip
(jax) $ pip install --upgrade "jax[cpu]"

Clone environment torch and install pytorch (use GPU!)

1
2
3
(base) $ conda create -n torch --clone base
(base) $ conda activate torch
(torch) $ conda install pytorch torchvision torchaudio pytorch-cuda=11.6 -c pytorch -c nvidia

Install cudnn (TBD! Nvidia sucks!)

  • Go to nvidia download website: [cuDNN Archive NVIDIA Developer](https://developer.nvidia.com/rdp/cudnn-archive).

image-20221107225432309

Try jax gpu on jax_gpu virtual environment.

PC -> use WSL -> jax cpu (OK), jax gpu (TBD)

Mac -> M1 version jax (NOK!) use miniforge3 (OK)

Compact the Ubuntu VM!

First find the location of the disk:

  1. PowerShell > diskpart
  2. DISKPART> Select vdisk file=c:\Users\allen\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu20.04onWindows_79rhkp1fndgsc\LocalState\ext4.vhdx
  3. DISKPART> compact vdisk
  4. DISKPART> Select vdisk file=c:\Users\allen\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu18.04onWindows_79rhkp1fndgsc\LocalState\ext4.vhdx
  5. DISKPART> compact vdisk