WSL2 Ubuntu22.04 + 3070 installation cuda11.6 +Pytorch1.13.0 full record

Article directory

    • Install cuda
      • Check the maximum supported cuda version
      • Install according to the official website steps
      • Aftermath after installation is complete
    • Install cudnn
    • Install PyTorch
      • Install miniconda to manage the python environment
      • Install Pytorch in torch_env
    • Result check
      • gpustat

Install cuda

Note in advance: If the WSL distribution version is Ubuntu22.04, an error occurred during my installation process, but it was resolved. Other methods I searched said that using Ubuntu20.04 directly will not cause this problem. If you want, you can choose to reinstall Ubuntu20.04.

View the maximum supported cuda version

First, use the nvidia-smi command to check the maximum cuda version supported by the currently installed driver:

You can see that the maximum supported here is 12.3. However, the previous experiments were all done on 11.6, so cuda11.6 will be installed here.

Install according to the official website steps

Enter the official website of cuda11.6 and select in order: cuda11.6→Linux→x86-64→WSL-Ubuntu→2.0→deb_local

Then the official installation process will appear, such as:

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.6.0/local_installers/cuda-repo-wsl-ubuntu-11-6-local_11.6.0-1_amd64.deb
sudo dpkg -i cuda-repo-wsl-ubuntu-11-6-local_11.6.0-1_amd64.deb
sudo apt-key add /var/cuda-repo-wsl-ubuntu-11-6-local/7fa2af80.pub
sudo apt-get update
sudo apt-get -y install cuda

We can operate the above content in any path. Finally, the cuda-repo-wsl-ubuntu-11-6-local_11.6.0-1_amd64.deb installation package will also be downloaded to this directory. After installation You can choose to delete it. I created a for_cuda directory directly in my home directory to operate.

Just follow the order above. in:
sudo apt-key add /var/cuda-repo-wsl-ubuntu-11-6-local/7fa2af80.pub There will be warnings that can be ignored.
Mainly the last sentence sudo apt-get -y install cuda reported an error:

The following packages have unmet dependencies:
 libcufile-11-6 : Depends: liburcu6 but it is not installable
E: Unable to correct problems, you have held broken packages.

After checking, it seems that it is because Ubuntu22.04 does not have liburcu6 by default. One solution I found is:
Unable to install CUDA on Ubuntu 22.04 WSL2:

# ppa:cloudhan/liburcu6 provides a forward-port of liburcu6 for Ubuntu 22.04.
sudo add-apt-repository ppa:cloudhan/liburcu6
sudo apt update
sudo apt install liburcu6

Follow the above sequence to add the ppa:cloudhan/liburcu6 source first. After updating, you can install liburcu6. (It seems that you need to open a ladder to successfully add the ppa source and update it)

Finally, re-execute sudo apt-get -y install cuda

Aftermath after installation is completed

cuda is installed under /usr/loacl/ by default:

Both cuda and cuda-11 can be understood as shortcuts to cuda-11.6. The files are all in cuda-11.6. Take a look at the directory structure:

There is bin, lib and include (cudnn will be copied to these two later)
Now you can enter the bin and execute ./nvcc -V to check the version:

The installation is successful.

The following is to add environment variables (if you are just using cuda through python, you don’t need to add them):
Add the following content to the home .bashrc file:

export CUDA_HONE=/usr/local/cuda-11.6 # Create new environment variable CUDA_HOME
export PATH=$PATH:$CUDA_HONE/bin # Add the bin directory to the PATH environment variable
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$CUDA_HONE/lib64
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$CUDA_HONE/extras/CUPTI/lib64
# The last two are to add the paths of some libraries to the LD_LIBRARY_PATH environment variable. Please refer to other tutorials.

After that, open the bash terminal and you can directly use the commands under bin. If you are using a zsh terminal, configure it in the corresponding .zshrc file.

Install cudnn

cudnn is just some library used to speed up calculations. Need to download from the official website:
cudnn download
Downloading requires registering an NVIDIA account.

The download here is 8.9.5. After the download is complete, unzip it with the following command:

tar -xvf cudnn-linux-x86_64-8.9.5.30_cuda11-archive.tar.xz

(Replace the compressed package with your own compressed package)

Then go to the decompressed folder and copy the decompressed lib and include contents to the corresponding lib64 and include directories in cuda:

cd cudnn-linux-x86_64-8.9.5.30_cuda11-archive # The directory is also the directory after decompression.
cp ./lib/* /usr/local/cuda-11.6/lib64 # Copy lib
cp ./include/* /usr/local/cuda-11.6/include #Copy header files

* is a wildcard character, indicating “all”

Install Pytorch

It is recommended to use conda to manage the python environment.

Install miniconda to manage python environment

miniconda is more streamlined than anaconda.
Official website: miniconda
Install according to the installation sequence provided:

mkdir -p ~/miniconda3
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda3/miniconda.sh
bash ~/miniconda3/miniconda.sh -b -u -p ~/miniconda3
rm -rf ~/miniconda3/miniconda.sh

After the installation is complete, you need to initialize bash:

~/miniconda3/bin/conda init bash
~/miniconda3/bin/conda init zsh # If you are using zsh, you can use this sentence to initialize zsh.

Afterwards, you can open a bash and you can see that there is (base) in front of it by default. This is the default python environment for conda:

You can create an environment and specify the python version with the following command

conda create --name torch_env python=3.7

Created an environment named torch_env with python 3.7.

You can then switch to different environments through conda activate [environment name], and the front of the command prompt will also change to the corresponding environment name:

Install Pytorch in torch_env

Go to Pytorch official website: pytorch. The latest one is provided by default. Other versions can be installed in install previous versions of PyTorch:

What I installed here is version 1.13.0, just install it according to the commands it provides. Install in the torch_env environment created above.
pip install torch==1.13.0 + cu116 torchvision==0.14.0 + cu116 torchaudio==0.13.0 --extra-index-url https://download.pytorch.org/whl/cu116

The download and installation may report a timeout error. Add --default-timeout=6000 to set the timeout (unit is seconds)

Search again for other errors.

Result check

After the installation is complete, see if it can be used in python

Finish!

gpustat

It’s too ugly to check the usage with nvidia-smi. You can also use gpustat. It is a python module and can be installed directly through pip install gpustat.

Effect after installation is completed:
Run gpustat or gpustat -i to view the gpu status. -i means dynamic viewing.