ubuntu20.04安装nvidia显卡驱动/CUDA/cuDNN
- 1 安装显卡驱动
-
- 1.1 禁止secure boot
- 1.2 安装闭源驱动
- 1.3 重启查看驱动
- 2 安装CUDA
-
- 2.1 下载
- 2.2 安装CUDA
- 2.3 配置环境
- 2.4 查看CUDA
- 3 安装cuDNN
-
- 3.1 下载
- 3.2 安装cuDNN
- 3.3 查看cuDNN
- 4 安装gpu版Pytorch
1 安装显卡驱动
1.1 禁止secure boot
这一步很重要,如果bios中为enable,驱动将安装失败。
windows每次开机有可能自动开启secure boot,所以安装前要确认为禁用状态。
1.2 安装闭源驱动
- 图形模式安装
- 命令行模式安装
1 2 3 4 5 6 7 8 9 10 11 12 13 | zjy@zjy-HP-ENVY-Laptop-13-ad0xx:~$ ubuntu-drivers devices == /sys/devices/pci0000:00/0000:00:1c.0/0000:01:00.0 == modalias : pci:v000010DEd00001D12sv0000103Csd0000834Cbc03sc02i00 vendor : NVIDIA Corporation model : GP108M [GeForce MX150] driver : nvidia-driver-390 - distro non-free driver : nvidia-driver-418-server - distro non-free driver : nvidia-driver-450-server - distro non-free driver : nvidia-driver-450 - distro non-free driver : nvidia-driver-455 - distro non-free recommended driver : nvidia-driver-440-server - distro non-free driver : xserver-xorg-video-nouveau - distro free builtin |
这里选择 recommended,即:
1 | sudo apt install nvidia-driver-455 |
1.3 重启查看驱动
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | zjy@zjy-HP-ENVY-Laptop-13-ad0xx:~/桌面$ nvidia-smi Sun Nov 29 16:12:30 2020 +-----------------------------------------------------------------------------+ | NVIDIA-SMI 455.38 Driver Version: 455.38 CUDA Version: 11.1 | |-------------------------------+----------------------+----------------------+ | GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC | | Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. | | | | MIG M. | |===============================+======================+======================| | 0 GeForce MX150 Off | 00000000:01:00.0 Off | N/A | | N/A 58C P0 N/A / N/A | 263MiB / 2002MiB | 3% Default | | | | N/A | +-------------------------------+----------------------+----------------------+ +-----------------------------------------------------------------------------+ | Processes: | | GPU GI CI PID Type Process name GPU Memory | | ID ID Usage | |=============================================================================| | 0 N/A N/A 1337 G /usr/lib/xorg/Xorg 125MiB | | 0 N/A N/A 1508 G /usr/bin/gnome-shell 137MiB | +-----------------------------------------------------------------------------+ |
如上图所示,显卡安装成功。
如果不放心可以用下面的指令查看:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | zjy@zjy-HP-ENVY-Laptop-13-ad0xx:~/桌面$ neofetch .-/+oossssoo+/-. zjy@zjy-HP-ENVY-Laptop-13-ad0xx `:+ssssssssssssssssss+:` ------------------------------- -+ssssssssssssssssssyyssss+- OS: Ubuntu 20.04.1 LTS x86_64 .ossssssssssssssssssdMMMNysssso. Host: HP ENVY Laptop 13-ad0xx /ssssssssssshdmmNNmmyNMMMMhssssss/ Kernel: 5.4.0-54-generic +ssssssssshmydMMMMMMMNddddyssssssss+ Uptime: 1 min /sssssssshNMMMyhhyyyyhmNMMMNhssssssss/ Packages: 1741 (dpkg), 6 (snap) .ssssssssdMMMNhsssssssssshNMMMdssssssss. Shell: bash 5.0.17 +sssshhhyNMMNyssssssssssssyNMMMysssssss+ Resolution: 2560x1440 ossyNMMMNyMMhsssssssssssssshmmmhssssssso DE: GNOME ossyNMMMNyMMhsssssssssssssshmmmhssssssso WM: Mutter +sssshhhyNMMNyssssssssssssyNMMMysssssss+ WM Theme: Adwaita .ssssssssdMMMNhsssssssssshNMMMdssssssss. Theme: Yaru [GTK2/3] /sssssssshNMMMyhhyyyyhdNMMMNhssssssss/ Icons: Yaru [GTK2/3] +sssssssssdmydMMMMMMMMddddyssssssss+ Terminal: gnome-terminal /ssssssssssshdmNNNNmyNMMMMhssssss/ CPU: Intel i5-7200U (4) @ 3.100GHz .ossssssssssssssssssdMMMNysssso. GPU: Intel HD Graphics 620 -+sssssssssssssssssyyyssss+- GPU: NVIDIA GeForce MX150 `:+ssssssssssssssssss+:` Memory: 840MiB / 7715MiB .-/+oossssoo+/-. |
正确识别到了核显和独显。
2 安装CUDA
2.1 下载
下载地址:https://developer.nvidia.com/zh-cn/cuda-downloads
安装指南:https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html
2.2 安装CUDA
执行:
1 | sudo sh cuda_11.1.0_455.23.05_linux.run |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | x End User License Agreement x x - x x NVIDIA Software License Agreement and CUDA Supplement to x x Software License Agreement. x x x x Preface x x - x x The Software License Agreement in Chapter 1 and the Supplement x x in Chapter 2 contain license terms and conditions that govern x x the use of NVIDIA software. By accepting this agreement, you x x agree to comply with all the terms and conditions applicable x x to the product(s) included herein. x x x x NVIDIA Driver x x x Do you accept the above EULA? (accept/decline/quit): |
输入accept,回车。
1 2 3 4 5 6 7 8 9 | │ CUDA Installer │ - [ ] Driver │ [ ] 455.23.05 │ + [X] CUDA Toolkit 11.1 │ [X] CUDA Samples 11.1 │ [X] CUDA Demo Suite 11.1 │ [X] CUDA Documentation 11.1 │ Options │ Install |
在对应选项上按空格(space)取消勾选
安装完成后显示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | =========== = Summary = =========== Driver: Not Selected Toolkit: Installed in /usr/local/cuda-11.1/ Samples: Installed in /home/zjy/, but missing recommended libraries Please make sure that - PATH includes /usr/local/cuda-11.1/bin - LD_LIBRARY_PATH includes /usr/local/cuda-11.1/lib64, or, add /usr/local/cuda-11.1/lib64 to /etc/ld.so.conf and run ldconfig as root To uninstall the CUDA Toolkit, run cuda-uninstaller in /usr/local/cuda-11.1/bin ***WARNING: Incomplete installation! This installation did not install the CUDA Driver. A driver of version at least .00 is required for CUDA 11.1 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 /var/log/cuda-installer.log |
2.3 配置环境
装完成后,需要配置环境变量,编辑
1 | source ~/.bashrc |
在末尾添加:
1 2 3 | export CUDA_HOME=/usr/local/cuda export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${CUDA_HOME}/lib64 export PATH=${CUDA_HOME}/bin:${PATH} |
添加后更新环境变量:
1 | source ~/.bashrc |
cuda安装目录为
参考:Ubuntu18.04 LTS 使用CUDA11.1编译TensoFlow-GPU版本
2.4 查看CUDA
1 2 3 4 5 6 7 | zjy@zjy-HP-ENVY-Laptop-13-ad0xx:~/下载$ nvcc -V nvcc: NVIDIA (R) Cuda compiler driver Copyright (c) 2005-2020 NVIDIA Corporation Built on Tue_Sep_15_19:10:02_PDT_2020 Cuda compilation tools, release 11.1, V11.1.74 Build cuda_11.1.TC455_06.29069683_0 |
3 安装cuDNN
3.1 下载
下载地址:https://developer.nvidia.com/rdp/cudnn-download
这里选择
安装指南:https://docs.nvidia.com/deeplearning/cudnn/install-guide/index.html
3.2 安装cuDNN
-
Navigate to your directory containing the cuDNN tar file.
-
Unzip the cuDNN package.
1tar -xzvf cudnn-x.x-linux-x64-v8.x.x.x.tgz -
Copy the following files into the CUDA Toolkit directory.
1
2
3sudo 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 /usr/local/cuda/lib64/libcudnn*
3.3 查看cuDNN
1 2 3 4 5 6 7 8 9 | zjy@zjy-HP-ENVY-Laptop-13-ad0xx:~$ cat /usr/local/cuda/include/cudnn_version.h | grep CUDNN_MAJOR -A 2 #define CUDNN_MAJOR 8 #define CUDNN_MINOR 0 #define CUDNN_PATCHLEVEL 5 -- #define CUDNN_VERSION (CUDNN_MAJOR * 1000 + CUDNN_MINOR * 100 + CUDNN_PATCHLEVEL) #endif /* CUDNN_VERSION_H */ |
安装成功。
4 安装gpu版Pytorch
选择对应版本的pytorch:https://pytorch.org/
在新环境中安装,并测试:
1 2 3 4 5 6 7 8 | (pytorch_gpu) zjy@zjy-HP-ENVY-Laptop-13-ad0xx:~$ python Python 3.7.9 (default, Aug 31 2020, 12:42:55) [GCC 7.3.0] :: Anaconda, Inc. on linux Type "help", "copyright", "credits" or "license" for more information. >>> import torch >>> torch.cuda.is_available() True #成功 |