Ununtu20.10 installation of pytoch (including installation of Anaconda, Nvidia graphics card driver and CUDA) 2021-11-10

1, Installation of Anaconda

Anaconda Download

Click the above link to enter the website, click the corresponding link and jump to the installation package download page

Download the version you need (for example, Anaconda3-2021.05-Linux-x86_64.sh), which is generally several versions lower than the latest version on the official website, but does not affect the use

Click OK

Anaconda installation

Next, find the installation package download location, open it with the terminal and enter it

bash Anaconda3-2021.05-Linux-x86_64.sh #Run the installation file, where the following statement is the name of your corresponding installation package

Then you can install it without brain. You should know all the English inside. When you encounter [ENTER], press ENTER, and when you encounter yes/no, ENTER yes

Finally, after the installation is completed, you will be prompted that the installation is successful. Then close the terminal and reopen the terminal. The shortcut key is Ctrl/Command + Alt + T. you will find that the word (base) appears at the beginning of the terminal statement, which proves that Anaconda installation is successful.

Anaconda verification

conda --version   #Or the following statement
conda -V   #Check the version number of conda. If no error is reported, Anaconda installation is successful

Content reference from: Ubuntu 18.04 installing Anaconda3

2, Installation of Nvidia graphics card driver

Nvidia driver download and installation

Update, global update

sudo apt upgrade   #UPDATE statement
sudo add-apt-repository ppa:graphics-drivers  #Add NVIDA graphics card driver library
ubuntu-drivers devices  #Show installable drivers

Select your favorite driver number for installation. The latest version with the largest version number

sudo apt install nvidia-driver-495  #Installing the 4595 drive

Wait for installation. Generally, the driver installation program will pop up when the installation progress is about 70%. What we have to do is
1. Esc determines the user agreement
2. Remember to set the password and press Enter
3. Repeat the password and press Enter
4. Continue until installation is complete

Nvidia driver activation

Note: read carefully

Restart will pop up the driver installation interface, blue screen, don't panic!!
Control the keyboard and select the second E or something. I can't remember the details. I can't find the follow-up supplement
Then select Continue, yes or something
Until you enter the password interface, enter the just set password. Note that the password is invisible. It doesn't matter if you enter an error. You can re-enter it. Press enter after entering
Finally, select the Reboot option and the system will restart again
At this point, the installation of the graphics card driver is completed

When restarting, you must restart and prepare to activate the graphics card driver. This is actually the command before you pay attention to the above. What you fear is that you are at a loss after restarting. What to do if you are insufficient will lead to an error

shutdown -r now   #Restart command, you can restart manually or enter this code at the terminal

Nvidia drive verification

After restart, open the terminal, enter the following code, and the following information appears to prove that the installation of the graphics card driver is completed

nvidia-smi   #

Content reference from: Simple record: install GPU driver, CUDA and cuDNN

3, CUDA installation

CUDA Download

Enter CUDA official website CUDA official website

Check the CUDA version number in the above NVIDIA SMI information and download the corresponding version, but it is recommended not to download the too new version. Download according to the version number recommended on the official website of pytoch. The version of 11.3 is recommended here

CUDA installation

Then enter the 11.3 version of CUDA's official website to start the selection, and then the download code appears. It is recommended to select deb(network)

Enter the corresponding code under the official website of the terminal in turn

wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/cuda-ubuntu2004.pin
sudo mv cuda-ubuntu2004.pin /etc/apt/preferences.d/cuda-repository-pin-600
sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/7fa2af80.pub
sudo add-apt-repository "deb https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/ /"
sudo apt-get updatesudo apt-get -y install cuda

CUDA verification

Then, add the environment variable and check the CUDA version. The version number indicates that the CUDA installation is successful

export PATH=$PATH:/usr/local/cuda/bin  #Add environment variable
nvcc -V  #Check CUDA version

Basically, the gcc version of ubuntu is higher now. Use the following code to lower the version before proceeding with the next steps

cat /proc/driver/nvidia/version  #View the original gcc version
sudo apt install build-essential
sudo apt -y install gcc-8 g++-8  #Install gcc 8, which can be adjusted according to the actual situation of error reporting.
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 8
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-8 8
sudo update-alternatives --config gcc  #Adjust the default gcc, and install gcc 8 without execution

Install CUDA samples for CUDA calibration

cuda-install-samples-11.x.sh ~  #Install cuda 11.x samples into the ~ directory and replace x with your version number
cd ~/NVIDIA_CUDA-11.x_Samples  #Enter the Sample directory
make  #It takes a little time. If the system version does not match, the gcc version may cause an error
./1_Utilities/deviceQuery/deviceQuery  #Perform inspection procedures

The following results appear, and Result = PASS appears in the last line, which proves that CUDA works normally

Content reference from: Simple record: install GPU driver, CUDA and cuDNN

4, Pytoch installation

Replace the conda mirror source

Replace the conda Tsinghua image source, because the Internet is very slow. Don't ask me how I know. I want to cry without tears

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

Create conda virtual environment

Create conda virtual environment

#Create a virtual environment called python with built-in Python version 3.8. You can decide what name you think of
conda create -n pytorch python=3.8   

Install pytoch!!!

Install pytoch. The following 11.3 is the version number of cuda you downloaded above. Don't make a mistake. You have to download more than 2G. Wait patiently. It's still relatively fast

conda install pytorch torchvision cudatoolkit=11.3

After installation, enter the virtual environment

conda activate pytorch   #You can type whatever name you choose

Run python

python   #Just enter python

Verify whether the pytorch package is successfully installed and whether the graphics card is called

be careful

Pytorch people call him pytorch, but the bag they pour in is torch

import torch

True appears, which proves that the pytorch package is installed successfully and the graphics card is called successfully

Finally, after the installation is completed, it is not easy to write the article. Remember to double-click the third company to pay more attention!

Tags: Python AI Pytorch

Posted on Wed, 10 Nov 2021 18:04:41 -0500 by bogeyman