Photo by Gabriel Heinzer on Unsplash
使用 WSL 可以讓你在 Windows 執行 Linux-based 的程式
WSL2 版本
若 windows 10 升級到 20H1/20H2 或 更高版本, 預設就會是 WSL2 的版本
啟用 Hyper-V
到 控制台 或搜尋 programs,點選 Add or remove programs

然後點選右方的 Programs and Features (程式和功能)

點選 Turn Windows features on or of (開啟或關Windows功能)

將 Hyper-V 中的選項都勾選

啟用 WSL
以 系統管理員身分 執行 PowerShell,然後執行下列 2 個指令啟用 WSL
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
執行完成後要重新啟動 Windows 去啟動 WSL
確認 WSL 啟用狀況
回到原本的 Windows Features 中確認,下列兩項是否都開啟了
- Virtual Machine Platform (虛擬機器平台)
- Windows Subsystem for Linux (Windows 子系統 Linux 版)

變更 WSL 版本
確認將 WSL 版本設定為 WSL2
wsl --set-default-version 2
在 Microsoft Store 下載 Ubuntu
Hint: 可以下載任何你想要在 Windows 執行的 Linux 版本,但我這裡用
Ubuntu 20.04 LTS版本做舉例
打開 Microsoft Store 搜尋 Ubuntu 然後下載安裝 Ubuntu

下載安裝完成後開啟 Ubuntu,然後設定好你在 Ubuntu 的 帳號密碼,建立完成後確認可以進到 Linux 系統,然後就可以關閉這個視窗了

使用 wsl 列出可以執行的 Linux 版本
執行 wsl -l 可以列出所有目前可以操作的子系統 Linux 版本
wsl -l
我目前只有安裝一個版本,所以你只會看到一個,若你有安多個不同版本的 Linux,這裡就會看到很多個版本

使用 wsl 進入指定的 Linux
wsl -d
Linux 系統名稱
wsl -d Ubuntu-20.04
使用 wsl -d 就可以進入到指定的 Linux 系統
然後你會看到在 Linux 中的所有檔案都是與 Windows 同步的掛載到 /mnt 路徑中
所以你就可以使用 Windows 軟體環境 快速的編輯你要執行的程式檔案,然後在 Linux 環境 中執行這些程式了

wsl 指令
| 指令 | 說明 |
|---|---|
wsl -h |
列出所有 wsl 指令 |
wsl -l |
列出所有目前可以操作的子系統 Linux 版本 |
wsl -d |
進入到指定 Linux 系統 |
Reference
- 什麼是Windows 子系統 Linux 版 | Microsoft Learn
- 使用WSL2在Windows下快速打造Linux開發環境(含Docker) - iT 邦幫忙::一起幫忙解決難題,拯救 IT 人的一天
Donate KJ 贊助作者喝咖啡
如果這篇文章對你有幫助的話,可以透過下面支付方式贊助作者喝咖啡,如果有什麼建議或想說的話可以贊助並留言給我
If this article has been helpful to you, you can support the author by treating them to a coffee through the payment options below. If you have any suggestions or comments, feel free to sponsor and leave a message for me!
| 方式 Method | 贊助 Donate |
| PayPal | https://paypal.me/kejyun |
| 綠界 ECPay | https://p.ecpay.com.tw/AC218F1 |
| 歐付寶 OPay | https://payment.opay.tw/Broadcaster/Donate/BD2BD896029F2155041C8C8FAED3A6F8 |