1. 程式人生 > 實用技巧 >ubuntu18.04上安裝cuda

ubuntu18.04上安裝cuda

下面是從NVIDIA官網搬運過來的。

2.Pre-installation Actions

Some actions must be taken before the CUDA Toolkit and Driver can be installed on Linux:
  • Verify the system has a CUDA-capable GPU.
  • Verify the system is running a supported version of Linux.
  • Verify the system has gcc installed.
  • Verify the system has the correct kernel headers and development packages installed.
  • Download the NVIDIA CUDA Toolkit.
  • Handle conflicting installation methods.
Note: You can override the install-time prerequisite checks by running the installer with the -override flag. Remember that the prerequisites will still be required to use the NVIDIA CUDA Toolkit.

2.1.Verify You Have a CUDA-Capable GPU

To verify that your GPU is CUDA-capable, go to your distribution's equivalent of System Properties, or, from the command line, enter:

$ lspci | grep -i nvidia

If you do not see any settings, update the PCI hardware database that Linux maintains by enteringupdate-pciids(generally found in/sbin) at the command line and rerun the previouslspci

command.

If your graphics card is from NVIDIA and it is listed in http://developer.nvidia.com/cuda-gpus, your GPU is CUDA-capable.

The Release Notes for the CUDA Toolkit also contain a list of supported products.

2.2.Verify You Have a Supported Version of Linux

The CUDA Development Tools are only supported on some specific distributions of Linux. These are listed in the CUDA Toolkit release notes.

To determine which distribution and release number you're running, type the following at the command line:

$ uname -m && cat /etc/*release

You should see output similar to the following, modified for your particular system:

x86_64
Red Hat Enterprise Linux Workstation release 6.0 (Santiago)

Thex86_64line indicates you are running on a 64-bit system. The remainder gives information about your distribution.

2.3.Verify the System Has gcc Installed

Thegcccompiler is required for development using the CUDA Toolkit. It is not required for running CUDA applications. It is generally installed as part of the Linux installation, and in most cases the version of gcc installed with a supported version of Linux will work correctly.

To verify the version of gcc installed on your system, type the following on the command line:

$ gcc --version

If an error message displays, you need to install the development tools from your Linux distribution or obtain a version ofgccand its accompanying toolchain from the Web.

2.4.Verify the System has the Correct Kernel Headers and Development Packages Installed

The CUDA Driver requires that the kernel headers and development packages for the running version of the kernel be installed at the time of the driver installation, as well whenever the driver is rebuilt. For example, if your system is running kernel version 3.17.4-301, the 3.17.4-301 kernel headers and development packages must also be installed.

While the Runfile installation performs no package validation, the RPM and Deb installations of the driver will make an attempt to install the kernel header and development packages if no version of these packages is currently installed. However, it will install the latest version of these packages, which may or may not match the version of the kernel your system is using. Therefore, it is best to manually ensure the correct version of the kernel headers and development packages are installed prior to installing the CUDA Drivers, as well as whenever you change the kernel version.

The version of the kernel your system is running can be found by running the following command:
$ uname -r
This is the version of the kernel headers and development packages that must be installed prior to installing the CUDA Drivers. This command will be used multiple times below to specify the version of the packages to install. Note that below are the common-case scenarios for kernel usage. More advanced cases, such as custom kernel branches, should ensure that their kernel headers and sources match the kernel build they are running. Note: If you perform a system update which changes the version of the linux kernel being used, make sure to rerun the commands below to ensure you have the correct kernel headers and kernel development packages installed. Otherwise, the CUDA Driver will fail to work with the new kernel. Note: If you perform a system update which changes the version of the linux kernel being used, make sure to rerun the commands below to ensure you have the correct kernel headers and kernel development packages installed. Otherwise, the CUDA Driver will fail to work with the new kernel.

RHEL/CentOS

The kernel headers and development packages for the currently running kernel can be installed with:
$ sudo yum install kernel-devel-$(uname -r) kernel-headers-$(uname -r)

Fedora

The kernel headers and development packages for the currently running kernel can be installed with:
$ sudo dnf install kernel-devel-$(uname -r) kernel-headers-$(uname -r)

OpenSUSE/SLES

Use the output of theunamecommand to determine the running kernel's version and variant:
$ uname -r
3.16.6-2-default
In this example, the version is3.16.6-2and the variant isdefault. The kernel headers and development packages can then be installed with the following command, replacing<variant>and<version>with the variant and version discovered from the previousunamecommand:
$ sudo zypper install kernel-<variant>-devel=<version>

Ubuntu

The kernel headers and development packages for the currently running kernel can be installed with:
$ sudo apt-get install linux-headers-$(uname -r)

2.5.Choose an Installation Method

The CUDA Toolkit can be installed using either of two different installation mechanisms: distribution-specific packages (RPM and Deb packages), or a distribution-independent package (runfile packages). The distribution-independent package has the advantage of working across a wider set of Linux distributions, but does not update the distribution's native package management system. The distribution-specific packages interface with the distribution's native package management system. It is recommended to use the distribution-specific packages, where possible.

Note: Standalone installers are not provided for architectures other than the x86_64 release. For both native as well as cross development, the toolkit must be installed using the distribution-specific installer. See the CUDA Cross-Platform Installation section for more details. cuda歷史版本下載:https://developer.nvidia.com/cuda-toolkit-archive 最終使用的是 .run檔案安裝的,沒有降級gcc,安裝的cuda10.2,因為之前安裝的nvidia驅動版本夠用 440.95.01。 執行 sudo sh *.run後會輸出宣告資訊,掃一遍,後面提示下輸入:accept 接著就進入安裝介面:

上圖右邊[ ]中的x是將游標移動到那一行後按Enter鍵實現的。

然後就可以執行安裝了。

安裝完成,比期待的時間快多了。

安裝完後,在.bashrc檔案末尾新增環境變數

sudo vim ~/.bashrc (沒有安裝vim 可以使用gedit:sudo gedit ~/.bashrc)

export CUDA_HOME=/usr/local/cuda 
export PATH=$PATH:$CUDA_HOME/bin 
export LD_LIBRARY_PATH=/usr/local/cuda-10.0/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
儲存退出後,輸入以下命名

source ~/.bashrc
測試cuda:
cd /usr/local/cuda/samples/1_Utilities/deviceQuery 
sudo make
./deviceQuery
能正常執行的話顯示的是你的顯示卡資訊,說明CUDA安裝成功

安裝CUDNN:

下載的是cuDNN Library for Linux

進入解壓後的cudnn目錄 應該能看到cuda資料夾
sudo cp cuda/include/cudnn.h /usr/local/cuda/include/ 
sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64/ 
sudo chmod a+r /usr/local/cuda/include/cudnn.h 
sudo chmod a+r /usr/local/cuda/lib64/libcudnn*

檢視cudnn版本

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

輸出資訊如下:
$ cat /usr/local/cuda/include/cudnn.h | grep CUDNN_MAJOR -A 2#define CUDNN_MAJOR 7
#define CUDNN_MINOR 6
#define CUDNN_PATCHLEVEL 5
--
#define CUDNN_VERSION (CUDNN_MAJOR * 1000 + CUDNN_MINOR * 100 + CUDNN_PATCHLEVEL)

#include "driver_types.h"

從上到下輸出7 6 5 即表示cudnn7.6.5

參考:https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#pre-installation-actions

參考:https://blog.csdn.net/zhiman_zhong/article/details/91491780

參考:https://blog.csdn.net/weixin_41424027/article/details/90708926?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param#CUDA_80