Configuring Cuda and Pytorch under Windows 11/10 WSL2 Ubuntu 20.04

After countless installation failures, I have concluded a correct path. Installing Pytorch in wsl2 is really a physical job, and there are always different errors in the process. Today, I finally successfully connected pytorch and cuda, so I wrote this article to record it.

1. Install WSL2  Ubuntu  20.04

 Windows10/11 three-step installation of wsl2 Ubuntu20.04 (any disk) - Zhihu (zhihu.com)

Second, configure the cuda environment

Configure the cuda environment. The cuda environment of the Ubuntu subsystem under WSL is slightly different from the cuda environment under the Linux system, so the downloaded local installation package or the source of the network installation is different. For the installation of Linux under WSL on the Nvidia official website, the options are as  shown below :

 It is worth noting that the WSL version requirement is WSL2.0. You can choose local installation or network installation. I chose to run the file locally (because the first two methods did not work). The installation source code of the official website is as follows, which is required during installation . Pay attention to the installed cuda version, which is the latest version by default, but the latest cuda version supported on the pytorch official website is 11.3, so you need to choose to install :

wget https://developer.download.nvidia.com/compute/cuda/11.3.0/local_installers/cuda_11.3.0_465.19.01_linux.run
sudo sh cuda_11.3.0_465.19.01_linux.run

Add the cuda file directory to the .bashrc file:

sudo vim ~/.bashrc

 Add the following content to the end of .bashrc:

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

 You can directly find the file modification, the path is \Ubuntu-20.04\home\zzzl (your name)

 Execute the following statement to check whether CUDA is installed successfully:

source ~/.bashrc
#查看cuda版本
nvcc -V
#查看nvidia界面,这个需要在原本的Windows系统上安装nvidia驱动的
nvidia-smi

3. Install pytorch

1. Install pytorch

# 安装pip3
sudo apt install python3-pip
# 更新pip3
python3 -m pip install --upgrade pip

You can install it according to the instructions on the pytorch official website . The latest version is installed by default. If you need a previous pytorch installation version , you can choose to install it according to the cuda version number you installed.

pip3 install torch torchvision torchaudio --pre --extra-index-url https://download.pytorch.org/whl/nightly/cu116

Many problems occurred during this process, basically (host='files.pythonhosted.org', port=443): Read timed out.

So when you encounter a package that is stuck when downloading, you can use Tsinghua Source to download the same package again. After repeated attempts, it will succeed.

pip3 --default-timeout=100 install numpy==1.23.5 -i https://pypi.tuna.tsinghua.edu.cn/simple

2. Check whether pytorch is installed successfully. You can enter the following command in the python3 interface:

import torch
print(torch.version.cuda)
print(torch.__version__)
print(torch.cuda.is_available())

 Whether cuda can be used depends on whether the last column printed is True.

4. Configure VScode to access the WSL interpreter

Just download this plugin

 The effect is probably like this, very convenient

 

Reference article:

[Python] CUDA11.6 three-piece installation of PyTorch_ericdiii's blog-CSDN blog_cuda 11.6

Linux subsystem cuda, pytorch and tensorrt environment construction and VScode connection under Windows system_Shengruxiahua-_-'s blog-CSDN blog

Guess you like

Origin blog.csdn.net/weixin_45897172/article/details/128364804