Detailed tutorial on installing pytorch + python3.8+GPU/CPU version under anaconda

1. Install anaconda

Anaconda包括Conda、Python以及一大堆安装好的工具包,比如:numpy、pandas等
Miniconda包括Conda、Python
conda是一个开源的包、环境管理器,可以用于在同一个机器上安装不同版本的软件包及其依赖,并能够在不同的环境之间切换

Students who have not installed Anaconda can refer to the following installation link:
https://blog.csdn.net/qq_45281807/article/details/112442577

It is classified according to the installed CPU version and GPU version. Generally, it is recommended to use the CPU version for running programs, which is more convenient to install.

2. Install the CPU version of Pytorch

1. Open Anaconda Prompt, as shown below:

Anaconda Prompt

2. Use Tsinghua mirror source website

Notice! If the download fails after switching the image, switch the default source first, and then modify another conda source that can be used (be sure to restore the default first, and then change another one!!!), so we switch back to the default source first :

conda config --remove-key channels

(1) Switch to the domestic mirror source (otherwise the download speed may be too slow)
and enter the following 4 lines of code respectively:

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --set show_channel_urls yes
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/

After configuration, if you need to use the Tsinghua image in the future, you don't need to configure it again.

3. Create a Pytorch environment

Create environment pytorch, use Python version 3.8, where pytorch is the name of the virtual environment, which can be changed:

conda create -n pytorch_38 python=3.8

After that, a prompt will pop up during the loading process, enter y to install.
Check whether the environment is installed successfully

conda info --envs

envs
You can see that there are four environments, of which pytorch is the environment just created.

4. Activate the pytorch environment just created

activate pytorch_38

activate

Activate the pytorch environment you just created.

5. Install Pytorch

According to your own installation version, look for the installation command code on the Pytorch official website:
Pytorch official website: https://pytorch.org/
Pytorch official website

The version shown in the picture is a copy:

conda install pytorch torchvision torchaudio cpuonly -c pytorch

Paste the copied code into the command line format, a prompt will pop up, enter y to complete the installation, and "done" will be displayed.

6. Test whether the installation is successful

Enter python, enter python

python

Enter import torch to test whether pytorch is installed successfully. If no error is reported, the installation is successful.
insert image description here

3. Install the GPU version (the computer has a graphics card)

1. Check whether the CUDA software driver is installed

(1) Use the shortcut key WIN+R, enter cmd
cmd
(2) Enter the command, nvidia-smi
nvidia-smi
(3) If the version number of CUDA is displayed, you can start the third step operation, otherwise, refer to the second chapter below, configuration cuda driver software:

https://blog.csdn.net/qq_45281807/article/details/121294644

2. Open Anaconda Prompt, as shown below:

Anaconda Prompt

3. Use Tsinghua mirror source website

Notice! If the download fails after switching the image, switch the default source first, and then modify another conda source that can be used (be sure to restore the default first, and then change another one!!!), so we switch back to the default source first :

conda config --remove-key channels

(1) Switch to the domestic mirror source (otherwise the download speed may be too slow)
and enter the following 4 lines of code respectively:

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
conda config --add channels https://mirrors.bfsu.edu.cn/anaconda/pkgs/main/

conda config --add channels https://mirrors.bfsu.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.bfsu.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels https://mirrors.bfsu.edu.cn/anaconda/cloud/bioconda/

After configuration, if you need to use the Tsinghua image in the future, you don't need to configure it again.

4. Create a Pytorch environment

Create environment pytorch, use Python version 3.8, where pytorch is the name of the virtual environment, which can be changed:

conda create -n pytorch_38 python=3.8

After that, a prompt will pop up during the loading process, enter y to install.
Check whether the environment is installed successfully

conda info --envs

envs
You can see that there are four environments, of which pytorch is the environment just created.

5. Activate the pytorch environment just created

activate pytorch_38

activate

Activate the pytorch environment you just created.

6. Install Pytorch

According to your own installation version, look for the installation command code on the Pytorch official website:
Pytorch official website: https://pytorch.org/
Pytorch official website
Enter the command:

conda install pytorch torchvision torchaudio cudatoolkit=11

Then after a while, it will be displayed whether to continue, enter y, press Enter, and you can install it! !

7. Check whether CUDA is available:

Enter the following commands one by one. If the output is True, the installation is successful.

python
import torch
print(torch.cuda.is_available())

insert image description here
Return False Solution
If you install according to the above steps, there is generally no problem. If False appears, you can try the following steps to troubleshoot: 1. First determine whether the graphics card supports CUDA, https://www.geforce.com/hardware/technology/cuda/supported-gpus 2. Make sure the driver is updated to the latest (make sure the driver Support each other with cuda version)

Reference article:

https://blog.csdn.net/qq_45281807/article/details/112442423
https://blog.csdn.net/qq_45281807/article/details/121294644

Guess you like

Origin blog.csdn.net/up_and_down__/article/details/125739528