千万不要在 WSL2 中安装 NVIDIA 相关驱动!

1.安装 Windows 驱动

在 Windows 上安装 NVIDIA GeForce Game Ready 或 NVIDIA RTX Quadro Windows 11 显示驱动,可以用 NVIDIA App 安装。

2.安装 WSL 2

  1. 打开常用的 Windows 终端/命令提示符/Powershell,安装 WSL: wsl.exe –install
  2. 更新到最新的 WSL 内核: wsl.exe –update

确认版本是 WSL2。

3.安装 Ubuntu

首先,查看可用的 WSL 发行版:

wsl --list --online

安装 Ubuntu-24.04

wsl --install -d Ubuntu-24.04

4.安装 CUDA

按显卡算力选择合适的 CUDA 版本。

CUDA Toolkit 11.8 Downloads | NVIDIA Developer

CUDA 11.8 本地 deb 安装命令如下:

wget https://developer.download.nvidia.com/compute/cuda/repos/wsl-ubuntu/x86_64/cuda-wsl-ubuntu.pin

sudo mv cuda-wsl-ubuntu.pin /etc/apt/preferences.d/cuda-repository-pin-600

wget https://developer.download.nvidia.com/compute/cuda/11.8.0/local_installers/cuda-repo-wsl-ubuntu-11-8-local_11.8.0-1_amd64.deb

sudo dpkg -i cuda-repo-wsl-ubuntu-11-8-local_11.8.0-1_amd64.deb

sudo cp /var/cuda-repo-wsl-ubuntu-11-8-local/cuda-*-keyring.gpg /usr/share/keyrings/

sudo apt-get update

sudo apt-get -y install cuda

Ubuntu-24.04 安装 CUDA 时可能会报错:

Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
 nsight-systems-2023.3.3 : Depends: libtinfo5 but it is not installable
E: Unable to correct problems, you have held broken pack

解决方案:

  1. 打开镜像源文件并修改。
sudo vim /etc/apt/sources.list.d/ubuntu.sources
  1. 向 sources 文件中添加如下内容:
Types: deb
URIs: http://old-releases.ubuntu.com/ubuntu/
Suites: lunar
Components: universe
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg

3.更新

sudo apt-get update
  1. 重新安装 CUDA
 sudo apt install cuda -y

修改环境变量:

vim ~/.bashrc  	# 进入配置文件

# 在配置文件最下面添加下列代码,下载的是 11.8 就写 11.8

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda-11.8/lib64
export PATH=$PATH:/usr/local/cuda-11.8/bin
export CUDA_HOME=$CUDA_HOME:/usr/local/cuda-11.8
export PATH=/usr/local/cuda/bin:$PATH


source ~/.bashrc 	# 应用配置文件

5.检查安装

nvidia-smi # 检查驱动

样例输出如下:

Fri May  9 10:05:39 2025
+-----------------------------------------------------------------------------------------+
| NVIDIA-SMI 575.51.02              Driver Version: 576.02         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 4070 Ti     On  |   00000000:01:00.0  On |                  N/A |
|  0%   39C    P5             15W /  285W |    3051MiB /  12282MiB |      3%      Default |
|                                         |                        |                  N/A |
+-----------------------------------------+------------------------+----------------------+

+-----------------------------------------------------------------------------------------+
| Processes:                                                                              |
|  GPU   GI   CI              PID   Type   Process name                        GPU Memory |
|        ID   ID                                                               Usage      |
|=========================================================================================|
|  No running processes found                                                             |
+-----------------------------------------------------------------------------------------+
# 检查 CUDA
nvcc -V

样例输出如下:

nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2022 NVIDIA Corporation
Built on Wed_Sep_21_10:33:58_PDT_2022
Cuda compilation tools, release 11.8, V11.8.89
Build cuda_11.8.r11.8/compiler.31833905_0