Install CUDA cuDNN and corresponding pytorch on Ubuntu20.04

1. The relationship between graphics card driver, CUDA, cuDNN and cuda version of pytorch

NVIDIA's graphics card can only be used if there is a driver. The driver of the graphics card and the CUDA driver are not the same thing. For me who have never used a graphics card, I got confused at the beginning. CUDA is a budget platform for parallel computing on graphics cards. cuDNN specializes in accelerating the calculation of deep learning algorithms on graphics cards. Personal understanding is equivalent to an accelerator. The cuda version of pytorch is equivalent to being able to call the graphics card well in the pytorch framework to process the algorithm.
This article is used to configure environment records for the first time. Some places may not be written clearly enough, if there is an error, continue to update later.
cuda: Compute Unified Device Architecture, is a general-purpose parallel computing architecture introduced by NVIDIA, which enables GPUs to solve complex computing problems.

cudnn: It is a GPU accelerated library for deep neural networks launched by NVIDIA. It emphasizes performance, ease of use and low memory overhead.

The relationship between cuda and cudnn: cudnn is a GPU acceleration library specially developed for deep neural networks based on the cuda architecture. cuda can be understood as a large business district, but this business district is empty and has not yet been renovated. cudnn can be understood as a room after decoration, for example, the first floor of the basement is specially decorated for amusement (deep neural network) into a large amusement factory.

2. NVIDIA graphics card driver installation

Notice! The ubuntu system installation generally comes with its own driver. Before installing the NVIDIA driver, it is necessary to disable the system’s own graphics card driver nouveau: you can first check the lsmod | grep nouveauenabling status of the nouveau driver through the command. If there is output, it means that the nouveau driver is working. If there is no content output, it means that it is already working. Disabled nouveau.
Picture:
My computer has output, indicating that nouveau is started, and the following disables nouveau:

  • Open the terminal and enter the following command in the terminal

    sudo gedit /etc/modprobe.d/blacklist.conf
    

    After that, the blacklist.conf file pops up:
    Picture:

  • Add these two lines at the end of the blacklist.conf file and save:

    blacklist nouveau
    options nouveau modeset=0
    
  • Then type in the terminal

    sudo update-initramfs -u  #应用更改
    
  • After restarting, the nouveau graphics card driver that comes with ubuntu20.04 is disabled, and then we can safely install the NVIDIA driver we want

3. Install the graphics card driver

Installing the NVIDIA driver on ubuntu20.04 is easy, just open System Settings -> Software and Updates -> Additional Drivers -> Select NVIDIA Driver -> Apply Changes (I chose the 510 version of the driver here). This interface will automatically display the recommended NVIDIA graphics driver based on the GPU on the computer.
insert image description here
Wait for it to complete.

Preparations before installing cuda and cudnn

Install a series of versions of the query

Go to the official website of pytorch to check which CUDA versions can be supported, preferably the corresponding version, and then check the CUDA version supported by cudnn on the official website of cudnn,

4. Install CUDA and test

4.1 Download and installation

After the NVIDIA (NVIDIA) graphics card driver installation is complete, restart the computer, enter nvidia-smi in the terminal and output the result as shown in the figure below, indicating that the installation is successful. The red box on the left in the figure below indicates that the graphics card driver version (my version 510) has just been installed, and the red box on the right shows that the highest version of CUDA supported by this version of the graphics card driver is 11.6 (note that this does not indicate that CUDA is installed, but the highest version of CUDA supported by the graphics card driver just installed)
insert image description here
After knowing the CUDA version you want to install, go to the CUDA download page on NVIDIA official website (click on the blue font) to find the corresponding CUDA version, such as As shown in the picture, I chose the version of CUDA 11.6.
insert image description here
After clicking in, the interface as shown in the figure below will appear, select 123→ in turn 4. Then three installation methods pop up. According to the installation experience, the runfile (local) method is recommended here 5, that is, select the option in the red circle in the figure below. This is because the CUDA installation process requires a lot of dependent library files. Although the CUDA run file is larger than the other two installation methods, it contains all the dependent library files, so it is relatively easy to install successfully.
insert image description here
Before installing CUDA11.0, you need to install some interdependent library files first, and run the following command in the terminal:

sudo apt-get install freeglut3-dev build-essential libx11-dev libxmu-dev libxi-dev libgl1-mesa-glx libglu1-mesa libglu1-mesa-dev

Then return to the above interface, and copy the two commands in the red box below to the terminal in turn. (Explain weget: download the installation package to the directory where the terminal runs, so just run sudo directly below.)
insert image description here
After running the above command, the following interface will pop up, press the ⬇ button, select and Continuepress Enter, then enter accept, and
insert image description herethen , as shown in the figure below, press Enterthe button in the pop-up interface Driver和450.51.05to cancel the installation (if you don’t cancel it here, the driver installed above will be overwritten, and there will be problems), then click and Installwait...
insert image description here

4.2 Configure CUDA environment variables

After the CUDA installation is complete, you need to configure the variable environment to use it normally. First type in the terminal

sudo gedit ~/.bashrc

cuda-11.6Open the .bashrc file as shown in the figure below, and then add the following CUDA environment variable configuration information ( version information, different versions need to be changed to your own version number) at the end of the .bashrc file as shown in the figure below, and then save the file.

export PATH=/usr/local/cuda-11.6/bin${
    
    PATH:+:${
    
    PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda-11.6/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}

insert image description here

4.3 CUDA test

Finally, enter in the terminal source ~/.bashrcor restart the terminal to make it take effect. At this time, we can enter nvcc -V in the terminal to view the CUDA installation information, as shown in the figure below, so far CUDA installation is successful.
Among them, LD_LIBARARY_PATH is the dynamic library search path.
insert image description here
There is a tutorial on the official website for adding environment variables above, and you can also refer to it. I haven't found where to find the update so far.

5. Install cuDNN

First, enter the cuDNN official website
URL: https://developer.nvidia.com/rdp/cudnn-archive
Register an nvidia account first, and then log in with the account to download. . .
insert image description here
Select the corresponding version of cuDNN, because my CUDA is version 11.6, so I choose the one in the red box above, using the 11.x version. Click to open.
insert image description here
I chose the one in the red box here, and I feel that the process is relatively simple, and I have not tried other ones so far. The red box below is my download directory. The general download directory is the folder named 'Download' under the main directory. Because the installation package is a compressed file ending in xz, use it first xz -d cudnn-linux-x86_64-8.4.0.27_cuda11.6-archive.tar.xz, then use to tar xf cudnn-linux-x86_64-8.4.0.27_cuda11.6-archive.tardecompress it, and after it is compressed, it will be in the second red box.
insert image description here
The next step is to install: here is the official document

  • Right-click to open the terminal and enter the terminal.
  • Run commands sequentially
    sudo cp cudnn-*-archive/include/cudnn*.h /usr/local/cuda/include 
    sudo cp -P cudnn-*-archive/lib/libcudnn* /usr/local/cuda/lib64 
    sudo chmod a+r /usr/local/cuda/include/cudnn*.h /usr/local/cuda/lib64/libcudnn*
    
  • Copy all .h header files to /usr/local/cuda, done.

cudnn test

Enter the following command in the terminal:

cat /usr/local/cuda-11.6/include/cudnn_version.h | grep CUDNN_MAJOR -A 2

Output the information in the red box is success.
insert image description here
Stupid method: You can also go directly to the directory to see the cudnn_version.h file, and you can click on the content in the red box to see it. There will also be information output in the terminal on the right.
insert image description here

Five install pytorch

The first is to install pycharm and anaconda:
-----First, install pycharm: refer to link
-----Install anaconda: refer to link
official website to find the appropriate pycharm version: Create an environment and install directly with the command line.

Guess you like

Origin blog.csdn.net/baobao135/article/details/126915105