WSL2安装cuda

千万不要在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 发行版:

1
wsl --list --online

安装 Ubuntu-24.04

1
wsl --install -d Ubuntu-24.04

4.安装cuda

看显卡算力安装适合的cuda版本

CUDA Toolkit 11.8 Downloads | NVIDIA Developer

11.8本地下载deb命令如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
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报错

1
2
3
4
5
6
7
8
9
10
11
12
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.打开镜像源文件,并修改。

1
sudo vim /etc/apt/sources.list.d/ubuntu.sources

2.向sources文件中添加如下内容:

1
2
3
4
5
Types: deb
URIs: http://old-releases.ubuntu.com/ubuntu/
Suites: lunar
Components: universe
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg

3.更新

1
sudo apt-get update

4.重新安装cuda

1
sudo apt install cuda -y

修改配置环境:

1
2
3
4
5
6
7
8
9
10
11
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.检查安装

1
nvidia-smi #检查驱动

样例输出如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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 |
+-----------------------------------------------------------------------------------------+
1
2
#检查cuda
nvcc -V

样例输出如下:

1
2
3
4
5
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