CUDA 安装
安装准备
- GPU计算力
> lspci | grep -i nvidia
01:00.0 VGA compatible controller: NVIDIA Corporation GP106 [GeForce GTX 1060 3GB] (rev a1)
01:00.1 Audio device: NVIDIA Corporation GP106 High Definition Audio Controller (rev a1)
- 系统信息
> uname -m && cat /etc/*release
x86_64
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=16.04
DISTRIB_CODENAME=xenial
DISTRIB_DESCRIPTION="Ubuntu 16.04.6 LTS"
NAME="Ubuntu"
VERSION="16.04.6 LTS (Xenial Xerus)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 16.04.6 LTS"
VERSION_ID="16.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
VERSION_CODENAME=xenial
UBUNTU_CODENAME=xenial
- GCC 版本
> gcc --version
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609
- Kernel 版本
> uname -r
4.15.0-72-generic
下载链接
离线安装包:
-
https://developer.download.nvidia.com/compute/cuda/repos/
-
https://developer.download.nvidia.com/compute/machine-learning/repos/
cuda 安装
禁用集成显卡
-
在 /etc/modprobe.d 目录中创建文件 blacklist-nouveau.conf,并输入以下内容:
blacklist nouveau options nouveau modeset=0
-
更新系统
sudo update-initramfs -u
-
验证
lsmod | grep nouveau # 若无内容输出,则表示禁用成功。
安装 nvidia 显卡驱动
-
重启电脑,不要进入登陆界面,直接按 Ctrl + Alt + F1 进入命令行模式,登陆账户;
-
关闭图形化界面;
sudo service lightdm stop sudo apt-get remove nvidia-* # 卸载掉原有nvidia驱动
-
cd 到下载好的 nvidia 安装驱动文件所在目录;
-
安装
sudo chmod 777 NVIDIA-Linux-x86_64-396.18.run # 禁用 opengl, 否则会出现循环登陆的问题 sudo ./NVIDIA-Linux-x86_64-396.18.run -no-x-check -no-nouveau-check -no-opengl-files #-no-x-check:安装驱动时关闭X服务 #-no-nouveau-check:安装驱动时禁用nouveau #-no-opengl-files:只安装驱动文件,不安装OpenGL文件
安装过程
The distribution-provided pre-install script failed! Are you sure you want to continue? yes Would you like to register the kernel module souces with DKMS? This will allow DKMS to automatically build a new module, if you install a different kernel later? No Nvidia's 32-bit compatibility libraries? No Would you like to run the nvidia-xconfigutility to automatically update your x configuration so that the NVIDIA x driver will be used when you restart x? Any pre-existing x confile will be backed up. Yes
-
挂载 nvidia 驱动
modprobe nvidia
-
验证
nvidia-smi
cuda安装
-
重启电脑,不要进入登陆界面,直接按 Ctrl + Alt + F1 进入命令行模式,登陆账户;
-
关闭图形化界面;
sudo service lightdm stop
-
cd 到下载好的 cuda runfile 文件所在目录;
-
安装
sudo sh cuda_8.0.61_375.26_linux.run
安装过程:
Do accept the previously read EULA? accept/decline/quit: accept Install NVIDIA Accelerated Graphics Driver for Linux-x86_64 xxx.xx? (y)es/(n)o/(q)uit: n # 已单独安装了最新的显卡驱动 Install the CUDA xx Toolkit? (y)es/(n)o/(q)uit: y Enter Toolkit Location [ default is /usr/local/cuda-xx ]: # 直接回车 Do you want to install a symbolic link at /usr/local/cuda? (y)es/(n)o/(q)uit: y Install the CUDA xx Samples? (y)es/(n)o/(q)uit: y Enter CUDA Samples Location [ default is /home/xxx ]: # 直接回车 Installing the CUDA Toolkit in /usr/local/cuda-xx ... # 等待安装完成
-
重启图形化界面;
sudo service lightdm restart # 会自动跳转到图形化界面,登录账户
-
验证
cd ~/NVIDIA_CUDA-XX_Samples make -j8 cd bin/x86_64/linux/release ./bandwidthTest ./deviceQuery
-
添加本地环境变量
vim ~/.bashrc # 末尾添加 export PATH=/home/michael/local/cuda_8.0.61/bin:$PATH export LD_LIBRARY_PATH=/home/michael/local/cuda_8.0.61/lib64$LD_LIBRARY_PATH
-
更新环境变量
source ~/.bashrc
cudnn安装
-
nvidia 官网下载与 cuda 版本对应的 cudnn.
-
解压,将解压文件夹中, include 目录下的文件拷贝或移动到 cuda 安装目录的头文件夹中.
sudo mv cuda/include/* /usr/local/cuda-10.0/include/
将解压文件中,lib 或 lib64 目录下的文件拷贝或移动到 cuda 安装目录的库文件夹中.
sudo mv cuda/lib64/* /usr/local/cuda-10.0/lib64/
这里 cudnn 放入到 cuda 的包含目录里面了,也可以不放进 cuda 里,放在外面之后要改环境变量。
版本查询
# cuda 版本
cat /usr/local/cuda/version.txt
# cudnn 版本
cat /usr/local/cuda/include/cudnn.h | grep CUDNN_MAJOR -A 2