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 |