無論采用哪一種方式,首先都需要更新 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