Ubuntu 18.04 安裝 Cuda 8.0+cuDNN v5.1


零、環境

Ubuntu 18.04 顯卡 940MX
安裝Cuda8.0+Cudnn5.1
由於Ubuntu 18.04自帶的gcc版本為7.5.0,下文中使用了建立軟鏈接的方式,使得Cuda使用gcc5.0。

一、安裝合適版本的顯卡驅動

參考下圖,選擇適當版本

來源:https://docs.nvidia.com/cuda/cuda-toolkit-release-notes/index.html

軟件和更新->附加驅動
下圖是默認情況

此時,設置->詳細信息圖形顯示的是我的核心顯卡

這里我修改為418,執行完成后重啟機器,可以看到這兩處的變化

安裝完畢

sudo nvidia-smi

二、安裝 Cuda 8.0

1.下載

https://developer.nvidia.com/Cuda-toolkit-archive

選擇:

雖然Version中沒有18.04,但可選16.04,親測可以正常使用。( 詳見 五、問題與解決

** Base Installer** 和** Patch 2**都需要下載
下載得到這兩個文件

2.預備

安裝依賴

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

接下來的這個步驟,可以避免安裝出現“Can't locate InstallUtils.pm in @INC”的錯誤
Can't locate InstallUtils.pm in @INC問題的參考鏈接:
https://forums.developer.nvidia.com/t/cant-locate-installutils-pm-in-inc/46952/10
https://askubuntu.com/questions/1086160/how-to-install-cuda-8-on-ubuntu-18-04-lts/1087842
參考博客:
https://blog.csdn.net/xiaoyang19910623/article/details/108407959
https://blog.csdn.net/u014529295/article/details/78820414
cd進入文件存放目錄

bash ./cuda*.run --tar mxvf
sudo cp InstallUtils.pm /usr/lib/x86_64-linux-gnu/perl-base
export $PERL5LIB

3.安裝

sudo sh cuda_8.0.61_375.26_linux.run --override

彈出一堆協議申明,可以空格向下瀏覽,不過十分冗長,可以直接按q退出協議瀏覽。

退出協議的閱讀后,輸入accept接受協議,開始安裝

接下來幾個選項需要輸入y/n/enter選擇

Do you accept the previously read EULA?
accept/decline/quit: accept

You are attempting to install on an unsupported configuration. Do you wish to continue?
(y)es/(n)o [ default is no ]: y

Install NVIDIA Accelerated Graphics Driver for Linux-x86_64 375.26?
(y)es/(n)o/(q)uit: n

Install the CUDA 8.0 Toolkit?
(y)es/(n)o/(q)uit: y

Enter Toolkit Location
 [ default is /usr/local/cuda-8.0 ]: 

Do you want to install a symbolic link at /usr/local/cuda?
(y)es/(n)o/(q)uit: y

Install the CUDA 8.0 Samples?
(y)es/(n)o/(q)uit: y

Enter CUDA Samples Location
 [ default is /home/kingdom ]: 

Installing the CUDA Toolkit in /usr/local/cuda-8.0 ...
Installing the CUDA Samples in /home/kingdom ...
Copying samples to /home/kingdom/NVIDIA_CUDA-8.0_Samples now...
Finished copying samples.

===========
= Summary =
===========

Driver:   Not Selected
Toolkit:  Installed in /usr/local/cuda-8.0
Samples:  Installed in /home/kingdom

Please make sure that
 -   PATH includes /usr/local/cuda-8.0/bin
 -   LD_LIBRARY_PATH includes /usr/local/cuda-8.0/lib64, or, add /usr/local/cuda-8.0/lib64 to /etc/ld.so.conf and run ldconfig as root

To uninstall the CUDA Toolkit, run the uninstall script in /usr/local/cuda-8.0/bin

Please see CUDA_Installation_Guide_Linux.pdf in /usr/local/cuda-8.0/doc/pdf for detailed information on setting up CUDA.

***WARNING: Incomplete installation! This installation did not install the CUDA Driver. A driver of version at least 361.00 is required for CUDA 8.0 functionality to work.
To install the driver using this installer, run the following command, replacing <CudaInstaller> with the name of this run file:
    sudo <CudaInstaller>.run -silent -driver

Logfile is /tmp/cuda_install_14643.log

Q&A:

a) 為什么會出現 **You are attempting to install on an unsupported configuration. ** ?

這個問題請看文末 五、問題與解決 中的 原因 ,對這里安裝沒有影響。

b) 關於 WARNING: Incomplete installation! This installation did not install the CUDA Driver

之前我們已經手動安裝了高版本的顯卡驅動,這里可以無視。故上面 Install NVIDIA Accelerated Graphics Driver for Linux-x86_64 375.26? 的時候我們選擇的是 n

4.配置環境變量

vim ~/.bashrc

末尾處添加

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

source ~/.bashrc使環境變量生效

5.驗證安裝

nvcc -V

到這里,cuda的安裝就完成了,但是可能會在后續使用中出現gcc版本的問題。
建議執行 解決方法 , 通過添加軟鏈接的方式使得cuda改用gcc5.0。

三、安裝cuDNN v5.1

1.下載

https://developer.nvidia.com/rdp/cudnn-archive

這里下載需要登錄你的Nvidia賬號,如果沒有就注冊一個。注冊過程有可能出現訪問緩慢的情況。
下載也可能失敗。如果始終無法下載可以在windows下使用迅雷下載。
不過,windows下下載此文件,文件格式將會是solitairetheme8。

使用前請重命名,修改為.tgz。

若在ubuntu下下載,將會直接是tgz格式。

2.解壓

tar -zxvf cudnn-8.0-linux-x64-v5.1.tgz

解壓將會在當前目錄產生一個cuda文件夾

3.配置

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*

4.驗證安裝

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

可以看到我們的cuDNN版本

這步僅能查看版本,不能驗證cuDNN已可用。

如何驗證請看 五、問題與解決

四、驗證

cd進入安裝cuda產生的Samples目錄

kingdom@kingdom-ThinkPad-T470p:~$ cd NVIDIA_CUDA-8.0_Samples/
kingdom@kingdom-ThinkPad-T470p:~/NVIDIA_CUDA-8.0_Samples$ cd 1_Utilities/
kingdom@kingdom-ThinkPad-T470p:~/NVIDIA_CUDA-8.0_Samples/1_Utilities$ cd deviceQuery
kingdom@kingdom-ThinkPad-T470p:~/NVIDIA_CUDA-8.0_Samples/1_Utilities/deviceQuery$ make
kingdom@kingdom-ThinkPad-T470p:~/NVIDIA_CUDA-8.0_Samples/1_Utilities/deviceQuery$ ./deviceQuery 

五、問題與解決

問題

問題出現在驗證cuDNN安裝上。這個問題與前文cuda安裝出現的一問原因相同。故放在一起來講。

如何驗證cuDNN安裝成功呢?
如果使用deb形式安裝cuDNN時,會附帶下載一份Sample,因為前文我用tgz格式安裝了,所以補充下載下圖的deb包。

得到這個文件

右鍵提取到此處解壓,得到的文件


右鍵提取到此處,得到data文件夾
其結構

其中的 cudnn_samples_v5 就是我們需要的

可以把它復制一份到我們指定的位置
這里我把它復制到了我的主目錄下
cd進入/cudnn_samples_v5/mnistCUDNN

make clean && make

編譯,遇到問題

error: #error -- unsupported GNU version! gcc versions later than 5 are not supported!

原因

Ubuntu 18.04自帶的gcc版本與cuda8.0要求的不同

參考官方給出的對照表

來源:https://docs.nvidia.com/cuda/archive/8.0/cuda-installation-guide-linux/index.html

解決方法

改變cuda使用的gcc版本

安裝低版本gcc和g++

sudo apt-get install gcc-5
sudo apt-get install g++-5

然后建立軟鏈接

sudo ln -s /usr/bin/gcc-5 /usr/local/cuda/bin/gcc
sudo ln -s /usr/bin/g++-5 /usr/local/cuda/bin/g++

這樣就可以正常編譯sample了

運行
./mnistCUDNN

出現 Test passed! 表示cuDNN安裝成功。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM