系統(tǒng)要求
建議先升級windows10到20H1/20H2 or higher, 預(yù)設(shè)就會是wsl 2 版本
啟用安裝wsl
wsl2 需要使用Hyper-V了牛,請確認已經(jīng)打開Hyper-V 的功能
控制面板 --> 程序和功能 --> 啟用或關(guān)閉 Windows 功能 --> 勾選 Hyper-V
家庭版沒有Hyper-V框产,先新建一個文本文件殷绍,復(fù)制粘貼一下代碼到文本中博秫,保存為Hyper-V.cmd仔蝌,使用管理員運行,等
重新啟動完成就可以看見Hyper-V了
pushd "%~dp0"
dir /b %SystemRoot%\servicing\Packages\*Hyper-V*.mum >hyper-v.txt
for /f %%i in ('findstr /i . hyper-v.txt 2^>nul') do dism /online /norestart /add-package:"%SystemRoot%\servicing\Packages\%%i"
del hyper-v.txt
Dism /online /enable-feature /featurename:Microsoft-Hyper-V-All /LimitAccess /ALL
以管理員身份執(zhí)行以下 PowerShell 命令
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
重啟電腦呜笑,然后到 控制面板 --> 程序和功能 --> 啟用或關(guān)閉 Windows 功能 --> 勾選 適用于 Linux 的 Windows 子系統(tǒng)和虛擬機平臺
WSL 版本改成WSL2
在 PowerShell 中執(zhí)行以下命令
wsl --set-default-version 2
安裝 Linux 發(fā)行版本
在 windows 10 中啟動 Microsoft Store颓芭,然后在商店中搜索ubuntu横侦,這邊使用是Ubuntu 22.04.3 LTS
安裝后啟動抗斤,設(shè)置好賬號密碼
更換Systemd
很多l(xiāng)inux 應(yīng)用都需要systemd 來管理和啟動服務(wù),WSL2 是windows 下的一個VM,微軟制作了一個init(PID 1)丈咐,快速啟動WSL VM 以及與windows 通信瑞眼,執(zhí)行 systemctl 確認是否有 systemd
ubuntu@DESKTOP:/mnt/$ systemctl
System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down
GitHub 上已經(jīng)有構(gòu)建systemd(PID 1)環(huán)鏡,這里我們使用DamionGans/ubuntu-wsl2-systemd-script棵逊,按照說明操作就可以了
ubuntu@CM-PC-02057:~$ git clone https://github.com/DamionGans/ubuntu-wsl2-systemd-script.git
Cloning into 'ubuntu-wsl2-systemd-script'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 118 (delta 0), reused 2 (delta 0), pack-reused 115
Receiving objects: 100% (118/118), 34.22 KiB | 455.00 KiB/s, done.
Resolving deltas: 100% (57/57), done.
ubuntu@CM-PC-02057:~$ cd ubuntu-wsl2-systemd-script
ubuntu@CM-PC-02057:~/ubuntu-wsl2-systemd-script$ bash ubuntu-wsl2-systemd-script.sh
安裝成功后退出伤疙,重啟ubuntu,使用 wsl -t Ubuntu,Ubuntu 換成你的ubuntu 的名字辆影,使用wsl -l 查看徒像,這里是Ubuntu-20.04,所以執(zhí)行的是 wsl -t Ubuntu-20.04
ubuntu@CM-PC-02057:~/ubuntu-wsl2-systemd-script$ exit
logout
在 PowerShell 中執(zhí)行以下命令
PS C:\WINDOWS\system32> wsl -l
適用于 Linux 的 Windows 子系統(tǒng)分發(fā):
Ubuntu-20.04 (默認)
PS C:\WINDOWS\system32> wsl -t Ubuntu-20.04
PS C:\WINDOWS\system32> wsl -d Ubuntu-20.04
檢查有沒有成功啟動 systemd
ubuntu@DESKTOP:~# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 2452 1608 hvc0 Sl+ 09:57 0:00 /init
root 6 0.0 0.0 2476 132 hvc0 Sl+ 09:57 0:00 plan9 --control-socket 5 --log-level 4 --server-fd 6 --
root 10 0.0 0.0 2472 116 ? S 09:57 0:00 /init
root 44 0.0 0.0 7240 516 ? Ss 09:58 0:00 /usr/bin/unshare --fork --pid --mount-proc bash -c expo
root 46 0.0 0.3 26864 12080 ? Ss 09:58 0:00 /lib/systemd/systemd --unit=basic.target
root 88 0.0 0.3 51672 15500 ? S<s 09:58 0:00 /lib/systemd/systemd-journald
root 105 0.0 0.1 21596 7100 ? Ss 09:58 0:00 /lib/systemd/systemd-udevd
systemd+ 117 0.0 0.2 18552 7928 ? Ss 09:58 0:00 /lib/systemd/systemd-networkd
message+ 254 0.0 0.1 7364 4024 ? Ss 09:58 0:00 /usr/bin/dbus-daemon --system --address=systemd: --nofo
root 334 0.0 0.1 16940 7640 ? Ss 09:58 0:00 /lib/systemd/systemd-logind
root 660 0.0 0.0 2460 816 ? Ss 10:13 0:00 /init
root 661 0.0 0.0 2472 116 ? S 10:13 0:00 /init
root 662 0.0 0.0 2616 596 pts/0 Ss+ 10:13 0:00 sh
root 671 0.0 0.0 2476 120 ? S 10:13 0:00 /init
root 672 0.0 0.6 1247876 24184 pts/1 Ssl+ 10:13 0:00 /mnt/wsl/docker-desktop/docker-desktop-user-distro prox
root 688 0.0 0.0 2476 120 ? S 10:13 0:00 /init
root 689 0.0 1.0 768592 42792 pts/2 Ssl+ 10:13 0:00 docker serve --address unix:///root/.docker/run/docker-
root 723 0.0 0.0 2456 112 ? Ss 10:51 0:00 /init
root 725 0.0 0.0 2472 116 ? R 10:51 0:00 /init
root 728 0.0 0.1 10048 5148 pts/3 Ss 10:51 0:00 -bash
root 766 0.0 0.0 10608 3220 pts/3 R+ 11:11 0:00 ps aux
安裝Docker
有了systemd蛙讥,就可以安裝 docker engine 了锯蛀,按照官方下載安裝就可以了
ubuntu@DESKTOP:~# docker --version
Docker version 24.0.7, build afdd53b
可能會遇到的問題
WSL 2 需要更新其核心元件
Download the Linux kernel update package:https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msiWSL 網(wǎng)絡(luò)玩爛了
可以先shutdown wsl 試試看: wsl --shutdown,wsl --update
不行的話, 就重啟電腦