1、装显卡驱动
1.1检测显卡型号和对应推荐安装驱动
lu@computer:~/project$ ubuntu-drivers devices
WARNING:root:_pkg_get_support nvidia-driver-390: package has invalid Support Legacyheader, cannot determine support level
== /sys/devices/pci0000:00/0000:00:1c.4/0000:04:00.0 ==
modalias : pci:v000010DEd00001347sv00001043sd00001A6Dbc03sc02i00
vendor : NVIDIA Corporation
model : GM108M [GeForce 940M]
driver : nvidia-driver-465 - third-party non-free
driver : nvidia-driver-418-server - distro non-free
driver : nvidia-driver-450-server - distro non-free
driver : nvidia-driver-390 - distro non-free
driver : nvidia-driver-460 - third-party non-free recommended
driver : nvidia-driver-460-server - distro non-free
driver : xserver-xorg-video-nouveau - distro free builtin
lu@computer:~/project$
1.2如果有安装过旧的驱动,需要先删除旧的驱动:
sudo apt-get purge nvidia*
1.3禁用自带的驱动,通过如下命令创建一个文件:
sudo vim /etc/modprobe.d/blacklist-nouveau.conf
在文件内添加如下内容:
blacklist nouveau
options nouveau modeset=0
更新一下:
sudo update-initramfs -u
1.4安装nvidia驱动
sudo add-apt-repository ppa:graphics-drivers/ppa
sudo apt-get update
sudo apt-get install nvidia-driver-418-server
sudo apt-get install mesa-common-dev
sudo apt-get install freeglut3-dev
注意:
1.具体驱动名称是“nvidia-driver-xxx”还是“nvidia-xxx”,需要根据上面“ubuntu-drivers devices”输出结果,由上图可见在ubuntu20.04 上是“nvidia-driver-xxx”
2.cuda10.0对应的驱动是410以上的版本,故这里安装418版本
1.5确保BIOS中Secure Boot设置为disabled,重启检查驱动是否安装成功
nvidia-smi
注意:
1.也可以执行下面指令自动安装所推荐的驱动,但这种方式没法选择驱动版本
sudo ubuntu-drivers autoinstall
2、安装CUDA
链接:https://developer.nvidia.com/cuda-toolkit-archive
2.1CUDA推荐下载.run可以根据提示安装
执行如下命令:
sudo bash cuda_10.0.130_410.48_linux.run
压住回车键,直到服务条款显示到100%。接着按下面的步骤选择:
accept
n(不要安装driver)
y
y
y
2.2安装完成后,设置环境变量
打开主目录下的 .bashrc文件添加如下路径,例如我的.bashrc文件在/home/lu/下。
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda-10.0/lib64
export PATH=$PATH:/usr/local/cuda-10.0/bin
export CUDA_HOME=$CUDA_HOME:/usr/local/cuda-10.0
终端运行:source ~/.bashrc
检查:nvcc --version
3.cudnn的安装
3.1下载安装文件
按需求下载cudnn的安装文件:https://developer.nvidia.com/rdp/cudnn-archive
3.2安装cudnn
解压下载的文件,可以看到cuda文件夹,在当前目录打开终端,执行如下命令:
sudo cp cuda/include/cudnn* /usr/local/cuda/include/
sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64/
sudo chmod a+r /usr/local/cuda/include/cudnn*
sudo chmod a+r /usr/local/cuda/lib64/libcudnn*
3.3查看cudnn版本
在终端输入
cat /usr/local/cuda/include/cudnn.h | grep CUDNN_MAJOR -A 2