Ubuntu19.04環境下Anaconda4.7.10+cuda10.1+cuDNN7.6.2+tensorflow2.0+pytorch1.1安裝經驗總結

安裝的前提是你已經安裝好顯卡驅動,如果沒有安裝好,丟個傳送門關於ubuntu19.04下安裝獨立顯卡驅動

安裝Anaconda4.7.10

先去anaconda官網下載,這裏選擇則64-Bit (x86) Installer

然後在下載目錄打開終端,輸入

bash Anaconda3-2019.07-Linux-x86_64.sh

 之後會出現協議,這裏不用管,一直Enter到結束,之後出現

Do you approve the license terms? [yes|no]

 輸入yes即可,再之後會出現

Anaconda3 will now be installed into this location:
/home/meteorrain/anaconda3
​
  - Press ENTER to confirm the location
  - Press CTRL-C to abort the installation
  - Or specify a different location below
​
[/home/meteorrain/anaconda3] >>>

就是選擇安裝的位置,看個人喜好,我是比較懶,直接默認安裝了,然後.....就裝完了(」゜ロ゜)」
並沒有出現是否添加到path的選項....但是我後來查看確實在路徑中,可能是默認添加了= =,或者說你選擇默認安裝,就會自動添加路徑?....不管了,進行下一步,更新系統文件

source ~/.bashrc

然後輸入如下命令

conda --version

顯示

conda 4.7.10

 則表示安裝成功了~~

 

安裝cuda10.1

不需要g++降級,去cuda官網直接下載相應的版本即可,由於暫時沒有19.04的,所以我下載的18.10,其實也沒多大影響,最近幾代ubuntu的兼容性很好
然後就是補庫(我沒有測試這些庫是否有用,但是爲了省事,就都裝上了~)

sudo apt-get install freeglut3-dev libx11-dev libxmu-dev libxi-dev libgl1-mesa-glx libglu1-mesa libglu1-mesa-dev

然後再輸入

sudo chmod 777 cuda_10.1.168_418.67_linux.run
sudo cuda_10.1.168_418.67_linux.run

安裝程序第一個輸入accept,第二個直接選擇install,然後等待安裝完成即可~再編輯一下這個文件

sudo vi ~/.bashrc

在文件末尾加上這幾行

export CUDA_HOME=/usr/local/cuda
export PATH=$PATH:$CUDA_HOME/bin
export LD_LIBRARY_PATH=/usr/local/cuda-10.1/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}

輸入:wq保存並退出,然後輸入如下命令進行更新

source ~/.bashrc

以下命令是用來測試cuda是否配置成功的

輸入

cat /proc/driver/nvidia/version

輸出

NVRM version: NVIDIA UNIX x86_64 Kernel Module  418.67  Sat Apr  6 03:07:24 CDT 2019
GCC version:  gcc version 8.3.0 (Ubuntu 8.3.0-6ubuntu1)

輸入

nvcc -V

輸出

nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2019 NVIDIA Corporation
Built on Wed_Apr_24_19:10:27_PDT_2019
Cuda compilation tools, release 10.1, V10.1.168

安裝cudnn7.6.2

cudnn的版本一定要根據cuda的版本進行選擇,官網傳送門,下載cudnn還必須註冊賬號,以及填寫一些信息,這些就隨意啦~
網上搜索的安裝cudnn感覺好麻煩,這裏就是下載的問題了,有人建議下載cuDNN Library for Linux,然後安裝過程大概有五條命令的樣子(這裏就不貼出來了),我選擇的是下載deb安裝包,這裏有兩種安裝包, cuDNN Runtime Library for Ubuntu18.04 (Deb)以及cuDNN Developer Library for Ubuntu18.04 (Deb)(由於沒有針對19.04版本的所以下載的18.04版本),請下載Runtime的,因爲Runtime是基本運行庫,Developer是依賴於Runtime針對有興趣開發cuDNN的人的庫,所以我們只需要下載Runtime就可以了,下載完成後輸入命令

sudo dpkg -i libcudnn7_7.6.2.24-1+cuda10.1_amd64.deb

然後等待安裝完成就可以啦~

這裏補充一下,請不要使用如下命令驗證是否安裝成功

cat /usr/local/cuda/include/cudnn.h | grep CUDNN_MAJOR -A 2

因爲安裝方式不同,驗證方法也是不一樣的,因爲使用deb文件安裝,根本在/usr/local/cuda/include目錄下根本沒有cudnn.h這個文件,正確的驗證方式在官網上給的清清楚楚, 傳送門,當然,這裏要先安裝一下測試用例

sudo dpkg -i libcudnn7-doc_7.6.2.24-1+cuda10.1_amd64.deb

 我把官網的驗證操作搬運過來

Copy the cuDNN sample to a writable path.(這一步是必須的,因爲如果你直接cd到cudnn_samples_v7裏執行make操作會沒有權限)

$cp -r /usr/src/cudnn_samples_v7/ $HOME

Go to the writable path.

$ cd  $HOME/cudnn_samples_v7/mnistCUDNN

編譯樣例mnistCUDNN

$make clean && make

運行樣例mnistCUDNN 

$ ./mnistCUDNN

如果cuDNN正確的安裝,你將會看到和如下相似的結果:

cudnnGetVersion() : 7602 , CUDNN_VERSION from cudnn.h : 7602 (7.6.2)
Host compiler version : GCC 8.3.0
There are 1 CUDA capable devices on your machine :
device 0 : sms 68  Capabilities 7.5, SmClock 1545.0 Mhz, MemSize (Mb) 10986, MemClock 7000.0 Mhz, Ecc=0, boardGroupID=0
Using device 0

Testing single precision
Loading image data/one_28x28.pgm
Performing forward propagation ...
Testing cudnnGetConvolutionForwardAlgorithm ...
Fastest algorithm is Algo 0
Testing cudnnFindConvolutionForwardAlgorithm ...
^^^^ CUDNN_STATUS_SUCCESS for Algo 0: 0.016480 time requiring 0 memory
^^^^ CUDNN_STATUS_SUCCESS for Algo 2: 0.030816 time requiring 57600 memory
^^^^ CUDNN_STATUS_SUCCESS for Algo 1: 0.042144 time requiring 3464 memory
^^^^ CUDNN_STATUS_SUCCESS for Algo 5: 0.047840 time requiring 203008 memory
^^^^ CUDNN_STATUS_SUCCESS for Algo 7: 0.057280 time requiring 2057744 memory
Resulting weights from Softmax:
0.0000000 0.9999399 0.0000000 0.0000000 0.0000561 0.0000000 0.0000012 0.0000017 0.0000010 0.0000000 
Loading image data/three_28x28.pgm
Performing forward propagation ...
Resulting weights from Softmax:
0.0000000 0.0000000 0.0000000 0.9999288 0.0000000 0.0000711 0.0000000 0.0000000 0.0000000 0.0000000 
Loading image data/five_28x28.pgm
Performing forward propagation ...
Resulting weights from Softmax:
0.0000000 0.0000008 0.0000000 0.0000002 0.0000000 0.9999820 0.0000154 0.0000000 0.0000012 0.0000006 

Result of classification: 1 3 5

Test passed!

Testing half precision (math in single precision)
Loading image data/one_28x28.pgm
Performing forward propagation ...
Testing cudnnGetConvolutionForwardAlgorithm ...
Fastest algorithm is Algo 0
Testing cudnnFindConvolutionForwardAlgorithm ...
^^^^ CUDNN_STATUS_SUCCESS for Algo 0: 0.014784 time requiring 0 memory
^^^^ CUDNN_STATUS_SUCCESS for Algo 2: 0.014976 time requiring 28800 memory
^^^^ CUDNN_STATUS_SUCCESS for Algo 1: 0.034816 time requiring 3464 memory
^^^^ CUDNN_STATUS_SUCCESS for Algo 7: 0.042080 time requiring 2057744 memory
^^^^ CUDNN_STATUS_SUCCESS for Algo 5: 0.044000 time requiring 203008 memory
Resulting weights from Softmax:
0.0000001 1.0000000 0.0000001 0.0000000 0.0000563 0.0000001 0.0000012 0.0000017 0.0000010 0.0000001 
Loading image data/three_28x28.pgm
Performing forward propagation ...
Resulting weights from Softmax:
0.0000000 0.0000000 0.0000000 1.0000000 0.0000000 0.0000714 0.0000000 0.0000000 0.0000000 0.0000000 
Loading image data/five_28x28.pgm
Performing forward propagation ...
Resulting weights from Softmax:
0.0000000 0.0000008 0.0000000 0.0000002 0.0000000 1.0000000 0.0000154 0.0000000 0.0000012 0.0000006 

Result of classification: 1 3 5

Test passed!

至此,cuDNN成功安裝~

安裝Tensorflow2.0

emmmm,裝這個是最簡單的,如果上面裝的都和我一樣的話,下面的也請跟我裝的一樣,alpha0版本我測試過似乎有問題= =

pip3 install tensorflow-gpu==2.0.0-beta1

進入python3環境,然後輸入

import tensorflow as tf

這裏有可能會出現這個警告

/home/meteorrain/.local/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:516: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint8 = np.dtype([("qint8", np.int8, 1)])
/home/meteorrain/.local/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:517: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_quint8 = np.dtype([("quint8", np.uint8, 1)])
/home/meteorrain/.local/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:518: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint16 = np.dtype([("qint16", np.int16, 1)])
/home/meteorrain/.local/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:519: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_quint16 = np.dtype([("quint16", np.uint16, 1)])
/home/meteorrain/.local/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:520: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint32 = np.dtype([("qint32", np.int32, 1)])
/home/meteorrain/.local/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:525: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  np_resource = np.dtype([("resource", np.ubyte, 1)])
/home/meteorrain/.local/lib/python3.7/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:541: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint8 = np.dtype([("qint8", np.int8, 1)])
/home/meteorrain/.local/lib/python3.7/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:542: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_quint8 = np.dtype([("quint8", np.uint8, 1)])
/home/meteorrain/.local/lib/python3.7/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:543: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint16 = np.dtype([("qint16", np.int16, 1)])
/home/meteorrain/.local/lib/python3.7/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:544: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_quint16 = np.dtype([("quint16", np.uint16, 1)])
/home/meteorrain/.local/lib/python3.7/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:545: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint32 = np.dtype([("qint32", np.int32, 1)])
/home/meteorrain/.local/lib/python3.7/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:550: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  np_resource = np.dtype([("resource", np.ubyte, 1)])

可以無視掉,因爲官網的實例中也有,而且這個只是warning不是error,只是告訴你,這樣寫不太合適,究其原因是numpy版本的問題,如果想不顯示,就將numpy版本降級,網上有方法不再贅述,這裏我選擇放着不管(官方都不怕,咱們怕啥(๑•́ ₃•̀๑))

然後輸入

tf.__version__

如果輸出

'2.0.0-beta1'

就表示,安裝成功了~

安裝Pytorch

首先在pytorch官網選擇自己的環境,python的版本,cuda的版本,以及安裝的方式啥的,然後官網會自動生成一個安裝命令,將其複製到終端執行即可,不過我安裝的時候沒有cuda10.1的選項,於是乎,我自己手動修改了其中的版本號,輸入瞭如下命令:

conda install pytorch torchvision cudatoolkit=10.1 -c pytorch

當然也是沒有問題的,最後測試以下是否安裝成功,在python3環境下輸入

import torch

沒有報錯的話就是安裝成功啦~

這裏附上一個安裝虛擬環境的帖子,這裏是創建的python虛擬環境,
實際上可以直接創建anaconda的虛擬環境,命令如下

conda create --name exp python=3.7.3

exp是自己定義的環境名稱,後面的3.7.3是python的版本,然後會安裝一些庫,安裝完成後,輸入

source activate exp

即可進入虛擬環境進行操作,下面是一些簡單的Anaconda指令

創建環境  conda create -n environment_name python=3
刪除環境  conda remove -n environment_name --all
激活環境  source activate environment_name  /conda activate environment_name
退出環境  conda deactivate

安裝包           conda install package
查看已安裝包      conda list
更新包           conda update package
移除包           conda remove package

覺得文章還不錯有幫助的童鞋還請點個贊支持一下~( •̀∀•́ )

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章