安装WSL2并在WSL2中安装nvidia的cuda toolkit,以便在wsl2中共享使用主机的计算卡。
安装前准备
首先你需要确保你安装有Pascal架构(Geforce 10xx或Tesla P100)及以上的显卡,并安装了公版的最新驱动,推荐使用Game Ready驱动而非Studio驱动。
安装WSL2
仅适用于首次全新安装。
- 安装WSL基本环境随后会要求重启
1
wsl --install
- 切换默认版本为WSL2
1
wsl --set-default-version 2
- 查看可用的发行版我这里选择了Debian,实际使用起来会比ubuntu轻量一点且差异较小。
1
wsl.exe --list --online
随后会自动下载和安装,安装完成后将要求设置用户名密码。1
wsl.exe --install Debian
1
2
3
4
5
6
7
8
9
10正在下载: Debian GNU/Linux
正在安装: Debian GNU/Linux
已成功安装分发。可以通过 “wsl.exe -d Debian” 启动它
正在启动 Debian...
Please create a default UNIX user account. The username does not need to match your Windows username.
For more information visit: https://aka.ms/wslusers
Enter new UNIX username: lxnchan
New password:
Retype new password:
passwd: password updated successfully
安装CUDA Toolkit
参考官方文档。
- 换源,不再赘述。
安装必要的工具1
apt install curl wget gnupg2 -y
- 下载Pin规则以防止从源安装cuda toolkit
1
wget https://developer.download.nvidia.com/compute/cuda/repos/wsl-ubuntu/x86_64/cuda-wsl-ubuntu.pin
- 下载安装包
1
wget https://developer.download.nvidia.com/compute/cuda/12.9.1/local_installers/cuda-repo-wsl-ubuntu-12-9-local_12.9.1-1_amd64.deb
- 下载后安装安装Key
1
dpkg -i cuda-repo-wsl-ubuntu-12-9-local_12.9.1-1_amd64.deb
1
cp /var/cuda-repo-wsl-ubuntu-12-9-local/cuda-CB802CB8-keyring.gpg /usr/share/keyrings/
- 安装Cuda toolkit
1
apt update && apt install -y cuda-toolkit-12-9
- 创建软链接这样就能在wsl2中执行
1
ln -s /usr/lib/wsl/lib/nvidia-smi /usr/bin/nvidia-smi
nvidia-smi
了1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21root@WIN-9H35TRE44W2:~# nvidia-smi
Tue Jul 22 09:47:51 2025
+-----------------------------------------------------------------------------------------+
| NVIDIA-SMI 575.64.01 Driver Version: 576.80 CUDA Version: 12.9 |
|-----------------------------------------+------------------------+----------------------+
| GPU Name Persistence-M | Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|=========================================+========================+======================|
| 0 NVIDIA GeForce RTX 3080 On | 00000000:41:00.0 Off | N/A |
| 30% 22C P8 12W / 320W | 53MiB / 10240MiB | 0% Default |
| | | N/A |
+-----------------------------------------+------------------------+----------------------+
+-----------------------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=========================================================================================|
| 0 N/A N/A 34 G /Xwayland N/A |
+-----------------------------------------------------------------------------------------+