在 WSL Ubuntu 下安装 cuda和cudnn 的方法

system environment

  • windows 11 23H2
  • wsl ubuntu 2204

install cuda 12.3

download url: https://developer.nvidia.com/cuda-downloads

1
2
3
4
wget https://developer.download.nvidia.com/compute/cuda/repos/wsl-ubuntu/x86_64/cuda-keyring_1.1-1_all.deb
sudo dpkg -i cuda-keyring_1.1-1_all.deb
sudo apt-get update
sudo apt-get -y install cuda-toolkit-12-3

install cudnn

download url: https://developer.nvidia.com/cudnn-downloads

1
2
3
4
5
6
wget https://developer.download.nvidia.com/compute/cudnn/9.0.0/local_installers/cudnn-local-repo-ubuntu2204-9.0.0_1.0-1_amd64.deb
sudo dpkg -i cudnn-local-repo-ubuntu2204-9.0.0_1.0-1_amd64.deb
# 替换下面命令里的星号位具体的值
sudo cp /var/cudnn-local-repo-ubuntu2204-9.0.0/cudnn-*-keyring.gpg /usr/share/keyrings/
sudo apt-get update
sudo apt-get -y install cudnn

add path

1
2
3
4
5
6
7
cuda_version=$(ls /usr/local/ | grep -oP "cuda-\d+\.\d+" | tail -1)
echo "CUDA Version: $cuda_version"
cuda_bin_path="/usr/local/$cuda_version/bin"
cuda_lib_path="/usr/local/$cuda_version/lib64"
echo "export PATH=\"$cuda_bin_path:\$PATH\"" >> ~/.bashrc
echo "export LD_LIBRARY_PATH=\"$cuda_lib_path:\$LD_LIBRARY_PATH\"" >> ~/.bashrc
source ~/.bashrc

verify nvidia driver

1
nvidia-smi

verify cuda

1
nvcc -V
updatedupdated2024-02-292024-02-29