Ubuntu 20.04 安装 CUDA Toolkit 的三种方式
无论采用哪一种方式,首先都需要更新 Ubuntu 软件源和升级到最新版本的软件包。由于国内从 Ubuntu 官方软件源下载速度比较慢,所以,建议采用国内 Ubuntu 镜像源,比如阿里 Ubuntu 软件源或清华大学 Ubuntu 软件源。具体的配置方式是修改配置文件 /etc/apt/sources.list,将其中的 archive.ubuntu.com 替换为 mirrors.alibaba.com 或 mirrors.tuna.tsinghua.edu.cn 。也可以在图形界面应用 "Software & Update" 中,修改 Ubuntu Software 标签页中的 Download from 后的软件源地址。
配置软件源后,采用如下命令进行软件源的更新和软件包的升级。
sudo apt update
sudo apt upgrade
下面介绍在 Ubuntu 20.04 长期支持版本中,安装 CUDA Tools 的三种方式:
方式一:采用 Ubuntu 软件源中的 CUDA Tools 软件包
这种方式安装简单,但安装的 CUDA Toolkit 版本往往不是最新版本。查询目前可安装的 CUDA Toolkit 版本的命令,如下所示
apt search nvidia-cuda-toolkit
具体安装命令如下:
sudo apt install nvidia-cuda-toolkit
方式二:先采用图形界面安装 CUDA 驱动,再安装从 NVIDIA 官网下载的 CUDA Toolkit 安装包
1)图形界面安装 CUDA 驱动
在所有应用中,选择 “Software & Update” 应用,在标签页 "Additional Drivers" 中选择 “nvidia-driver-450-server”,如下图所示:
选择后,单击 “Apply Changes” 按钮,这样就更新并切换到所选驱动。
快捷键 Ctrl + Alt + T 打开 Terminal ,运行 nvidia-smi 命令以验证切换到 CUDA 驱动是否成功。我尝试过 nvidia-driver-460 这个版本,但没有成功,因此使用稍低的版本 nvidia-driver-450-server 。
2)下载并安装 CUDA Toolkit
本机安装的 CUDA Toolkit 版本为 11.0.3,与上一步安装 CUDA 驱动 450 兼容(可以参考下载文件名的尾缀), 具体下载命令,如下
wget https://developer.download.nvidia.com/compute/cuda/11.0.3/local_installers/cuda_11.0.3_450.51.06_linux.run
安装命令,如下
sudo sh cuda_11.0.3_450.51.06_linux.run
需要注意,安装时,选择不安装 CUDA 驱动,安装记录如下:
===========
= Summary =
===========
Driver: Not Selected
Toolkit: Installed in /usr/local/cuda-11.0/
Samples: Installed in /home/klchang/, but missing recommended libraries
Please make sure that
- PATH includes /usr/local/cuda-11.0/bin
- LD_LIBRARY_PATH includes /usr/local/cuda-11.0/lib64, or, add /usr/local/cuda-11.0/lib64 to /etc/ld.so.conf and run ldconfig as root
To uninstall the CUDA Toolkit, run cuda-uninstaller in /usr/local/cuda-11.0/bin
***WARNING: Incomplete installation! This installation did not install the CUDA Driver. A driver of version at least .00 is required for CUDA 11.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 /var/log/cuda-installer.log
安装结束后,添加环境变量到 ~/.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
保存后退出。
在 Terminal 中,激活环境变量命令为 source ~/.bashrc 。
测试 CUDA Toolkit 。 通过编译自带 Samples并执行, 以验证是否安装成功。具体命令如下所示:
cd /usr/local/cuda/samples/1_Utilities/deviceQuery
sudo make
./deviceQuery
如果安装成功,则输出类似于如下信息:
./deviceQuery Starting...
CUDA Device Query (Runtime API) version (CUDART static linking)
Detected 1 CUDA Capable device(s)
Device 0: "GeForce RTX 2070 with Max-Q Design"
CUDA Driver Version / Runtime Version 11.0 / 11.0
CUDA Capability Major/Minor version number: 7.5
Total amount of global memory: 7982 MBytes (8370061312 bytes)
(36) Multiprocessors, ( 64) CUDA Cores/MP: 2304 CUDA Cores
GPU Max Clock rate: 1125 MHz (1.12 GHz)
Memory Clock rate: 5501 Mhz
Memory Bus Width: 256-bit
L2 Cache Size: 4194304 bytes
Maximum Texture Dimension Size (x,y,z) 1D=(131072), 2D=(131072, 65536), 3D=(16384, 16384, 16384)
Maximum Layered 1D Texture Size, (num) layers 1D=(32768), 2048 layers
Maximum Layered 2D Texture Size, (num) layers 2D=(32768, 32768), 2048 layers
Total amount of constant memory: 65536 bytes
Total amount of shared memory per block: 49152 bytes
Total number of registers available per block: 65536
Warp size: 32
Maximum number of threads per multiprocessor: 1024
Maximum number of threads per block: 1024
Max dimension size of a thread block (x,y,z): (1024, 1024, 64)
Max dimension size of a grid size (x,y,z): (2147483647, 65535, 65535)
Maximum memory pitch: 2147483647 bytes
Texture alignment: 512 bytes
Concurrent copy and kernel execution: Yes with 3 copy engine(s)
Run time limit on kernels: Yes
Integrated GPU sharing Host Memory: No
Support host page-locked memory mapping: Yes
Alignment requirement for Surfaces: Yes
Device has ECC support: Disabled
Device supports Unified Addressing (UVA): Yes
Device supports Managed Memory: Yes
Device supports Compute Preemption: Yes
Supports Cooperative Kernel Launch: Yes
Supports MultiDevice Co-op Kernel Launch: Yes
Device PCI Domain ID / Bus ID / location ID: 0 / 1 / 0
Compute Mode:
< Default (multiple host threads can use ::cudaSetDevice() with device simultaneously) >
deviceQuery, CUDA Driver = CUDART, CUDA Driver Version = 11.0, CUDA Runtime Version = 11.0, NumDevs = 1
Result = PASS
3)下载并安装 cuDNN
从 NVIDIA 官方网址 https://developer.nvidia.com/rdp/cudnn-download 下载 cudnn-11.0-linux-x64-v8.0.5.39.tgz 。
解压压缩包,并把相应的文件,复制到指定目录即可。如下所示:
tar zxvf cudnn-11.0-linux-x64-v8.0.5.39.tgz
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* /usr/local/cuda/lib64/libcudnn*
方式三:CUDA 驱动和 CUDA Toolkit 都采用命令行方式安装
首先,需要卸载原有的 NVIDIA 驱动并禁用自带的驱动 nouveau;然后,重启电脑,使用 lsmod | grep nouveau 命令检查禁用自带驱动是否成功;如果禁用成功,则安装从 NVIDIA 官方地址下载的 CUDA Toolkit。其步骤则与方式二相同,差别在于这次需要安装 CUDA 驱动 。更多内容,参见 How to Install CUDA ToolKit 11.0, and Nvidia Display Driver on Ubuntu 20.04。
问题与解答
问题 1,sudo apt update 时,出现有锁无法更新的情况
$ sudo apt update
Reading package lists... Done
E: Could not get lock /var/lib/apt/lists/lock. It is held by process 1379 (packagekitd)
N: Be aware that removing the lock file is not a solution and may break your system.
E: Unable to lock directory /var/lib/apt/lists/
解决方法:
停用 packagekitd,并禁止开机启动,具体命令如下:
systemctl stop packagekitd
systemcrl disable packagekit.service
参考资料
[1] How to Install cuda on Ubuntu 20.04. https://linuxconfig.org/how-to-install-cuda-on-ubuntu-20-04-focal-fossa-linux
[2] Ubuntu16.04安装NVIDIA驱动、实现GPU加速. https://blog.csdn.net/zhang970187013/article/details/81012845