Ubuntu18.04深度學習環境配置

DCooo發表於2020-10-13

作業系統:Ubuntu18.04
顯示卡:RTX2070
注:本教程僅僅是我做筆記方便回憶,不一定適用於所有人

1、cuda安裝

1.1、run方式安裝

  • 開啟網站,連結,獲取包。
  • 禁用 nouveau
    終端中執行: lsmod | grep nouveau,如果有輸出則代表nouveau正在載入。
    因為我早就安裝了NVIDIA的顯示卡驅動,禁用了nouveau,所以沒有輸出。
  • 進入run檔案目錄,執行命令
sudo sh cuda_10.0.89.440.33.01_linux.run
  • 如果詢問顯示管理器仍有開啟,是否繼續安裝,這裡選擇continue;
  • 然後,會列出一個列表要求選擇想要安裝的內容,這裡將第一個驅動安裝的部分回車一下將那個x取消
  • 之後,方向鍵選擇下面的Install進行安裝,最後安裝成功後會有一個提示。
  • 我的提示中有如下提示,不用管他,只要你的nvidia版本高於推薦版本就可以
***WARNING: Incomplete installation! This installation did not install the CUDA Driver. A driver of version at least 440.00 is required for CUDA 10.2 functionality to work
  • 編輯cuda環境變數
gedit ~/.bashrc
  • 新增環境變數
export PATH=/usr/local/cuda-10.2/bin:$PATH 
export LD_LIBRARY_PATH=/usr/local/cuda-10.2/lib64$LD_LIBRARY_PATH
  • 使之生效
source ~/.bashrc
  • 重啟電腦
reboot
  • 測試cuda是否安裝成功
cd /usr/local/cuda-10.2/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"
  CUDA Driver Version / Runtime Version          11.1 / 10.2
  CUDA Capability Major/Minor version number:    7.5
  Total amount of global memory:                 7981 MBytes (8368685056 bytes)
  (36) Multiprocessors, ( 64) CUDA Cores/MP:     2304 CUDA Cores
  GPU Max Clock rate:                            1620 MHz (1.62 GHz)
  Memory Clock rate:                             7001 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 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.1, CUDA Runtime Version = 10.2, NumDevs = 1
Result = PASS

2、cudnn安裝

tar -xvf cudnn-10.2-linux-x64-v8.0.4.30.tgz
  • 拷貝相關庫檔案
sudo cp include/cudnn.h /usr/local/cuda/include/
sudo cp lib64/libcudnn* /usr/local/cuda/lib64/
sudo chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn*

3、cuda的解除安裝

  1. 之前安裝的cuda11.1不能使用Pytorch,於是想解除安裝當前版本的cuda,網上的教程都是執行cuda目錄下的指令碼,但是我發現cuda11.1目錄下面沒有相關的解除安裝指令碼,如果你的cuda目錄下面有指令碼,可以使用該方法:
 cd  /usr/local/cuda-x.x/bin
 sudo ./uninstall_cuda_x.x.pl 
 或
 sudo ./cuda-uninstaller
  1. 如果沒有該指令碼,使用以下方法:
  • 首先
sudo apt-get remove cuda
sudo apt-get autoremove --purge cuda 
sudo apt-get remove cuda*
  • 然後刪除目錄
cd /usr/local/  # 然後切換到CUDA所在目錄
sudo rm -r cuda-x.x
  • 檢視安裝了哪些cuda相關的庫,可以用以下指令
sudo dpkg -l |grep cuda
  • 刪除的包名要根據待刪除的版本而定
sudo dpkg -P cuda-repo-ubuntu1604-9-1-local_9.1.85-1_amd64

4、Pytorch安裝

  • 我首先新建了一個虛擬環境來安裝pytorch。
conda create -n pytorch python=3.8
  • 啟用虛擬環境
conda activate pytorch
  • 進入官網,找到適合的pytorch版本
    在這裡插入圖片描述
  • 安裝
conda install pytorch==1.5.1 torchvision==0.6.1 cudatoolkit=10.2 -c pytorch

相關文章