Ubuntu 18.04 64位安裝tensorflow-gpu


第一步(可直接跳到第二步):安裝nvidia顯卡驅動

linux用戶可以通過官方ppa解決安裝GPU驅動的問題。使用如下命令添加Graphic Drivers PPA:

1
sudo add - apt - repository ppa:graphics - drivers / ppa 

然后更新源:

1
sudo apt - get update 

然后去navidia官網查看最新的驅動版本號:navidia官網:http://www.geforce.cn/drivers
比如說驅動的最新版本號為396,則執行如下指令:

1
sudo apt - get install nvidia - 396

最后安裝openGL支持:

 

1
sudo apt - get install mesa - common - dev 

第二步:安裝cuda-10.0(中間會默認安裝顯卡驅動)

如果直接執行:$  sudo apt-get install -y cuda  會報錯。正確使用方法為。
官方網站下載:CUDA Toolkit 10.0。下載網址為:https://developer.nvidia.com/cuda-downloads
linux,86_64,Ubuntu,18.04,deb[local]
然后執行如下指令:
1
2
3
$ sudo dpkg - i cuda-repo-ubuntu1804-10-0-local-10.0.130-410.48_1.0-1_amd64.deb
$ sudo apt - get update 
$ sudo apt - get install cuda
這時出現如下錯誤:

使用的是Ubuntu 18.04 LTS 64位,安裝cuda10.0,采用本地deb包安裝方式,這是安裝的環境的主要信息。

對於該問題,搜索到的多是將Ubuntu中的一些有問題的更新或者源去掉來解決的,但是對於這個問題無效。而且Google上很少有這個問題的搜索結果,折騰了一兩個小時后才找到一個方案,就是使用aptitude。這個比apt處理依賴問題要更加智能,采用該方案安裝cuda如下:

如果沒有aptitude則需要先安裝aptitude: sudo apt-get install aptitude

然后進行安裝cuda:

沖突寫了兩三屏幕,然后給出了解決方案:

選擇接受,等待

終於完成了,嘗試了下nvidia-smi

 

如果nvidia-smi失敗,則試着重啟,應該就OK了。

 

這時在/usr/local目錄下產生一個cuda安裝的路徑叫"cuda-10.0",並添加cuda到環境變量:

 

1
sudo vim / etc / profile 
添加內容:
1
2
3
export CUDA_HOME = / usr / local / cuda -10 .0 
export PATH = $CUDA_HOME / bin :$PATH   
export LD_LIBRARY_PATH = $CUDA_HOME / lib64:$LD_LIBRARY_PATH
使環境變量生效
1
source / etc / profile
檢驗是否安裝成功:查看GPU運行的進程
1
$ nvidia - smi  <a style = "font-size: 2em; background-color: #ffffff; font-family: 'PingFang SC', 'Helvetica Neue', Helvetica, Arial, sans-serif;" name = "t3" >< / a>

第三步:降低gcc版本到5.0以下

查看gcc當前使用版本:
1
$ gcc - - help
1
$ gcc - - version #查看gcc版本號
最后一行為 <file:///usr/share/doc/gcc-4.8/README.Bugs>. 使用的ubuntu14.04使用的是4.8版本 所以不用降低gcc版本

否則執行如下指令:
1
2
3
4
5
6
7
8
9
sudo apt - get install g + + - 4.9 
sudo update - alternatives - - install / usr / bin / gcc gcc / usr / bin / gcc - 4.9 20 
sudo update - alternatives - - install / usr / bin / gcc gcc / usr / bin / gcc - 5 10 
sudo update - alternatives - - install / usr / bin / g + + g + + / usr / bin / g + + - 4.9 20 
sudo update - alternatives - - install / usr / bin / g + + g + + / usr / bin / g + + - 5 10 
sudo update - alternatives - - install / usr / bin / cc cc / usr / bin / gcc 30 
sudo update - alternatives - - set cc / usr / bin / gcc 
sudo update - alternatives - - install / usr / bin / c + + c + + / usr / bin / g + + 30 
sudo update - alternatives - - set c + + / usr / bin / g + + 

第四步:下載 cuDNN V5+ 庫文件並添加到cuda-8.0庫

解壓並將內容copy到/usr/local/cuda-10.0/include和lib64目錄中:

cudann-10.0是目前為止比較穩定的版本在更新tensorflow后(1.4.1- 指令: pip install --upgrade tensorflow-gpu 即可更新tensorflow)

在官網下載對應版本的*.tgz文件。

指令如下:

1
2
3
4
sudo tar xvzf cudnn-10.0-linux-x64-v7.4.2.24.tgz
sudo cp cuda / include / cudnn.h / usr / local / cuda -10 .0 / include 
sudo cp cuda / lib64 / libcudnn * / usr / local / cuda -10 .0 / lib64 
sudo chmod a + r / usr / local / cuda -10 .0 / include / cudnn.h / usr / local / cuda -10 .0 / lib64 / libcudnn *   #分配包的權限 

第五步:安裝tensorflow

(1)Anaconda安裝tensorflow

下載:Anaconda2-4.3.1-Linux-x86_64.sh(https://repo.continuum.io/archive/)參考網址:https://www.cnblogs.com/willnote/p/6746499.html

1
bash Anaconda2 - 4.3 . 1 - Linux - x86_64.sh

Anaconda倉庫鏡像

官方下載更新工具包的速度很慢,所以繼續添加清華大學 TUNA提供的Anaconda倉庫鏡像,在終端或cmd中輸入如下命令進行添加

1
2
$ conda config - - add channels https: / / mirrors.tuna.tsinghua.edu.cn / anaconda / pkgs / free /
$ conda config - - set show_channel_urls yes

備注:如果出現conda命令未找到,查看:https://www.cnblogs.com/chamie/p/10009193.html

Tensorflow安裝

在終端或cmd中輸入以下命令搜索當前可用的tensorflow版本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
(可以略掉)$ anaconda search - t conda tensorflow
 
Using Anaconda API: https: / / api.anaconda.org
Run 'anaconda show <USER/PACKAGE>' to get more details:
Packages:
      Name                      |  Version | Package Types   | Platforms     
      - - - - - - - - - - - - - - - - - - - - - - - - - |   - - - - - - | - - - - - - - - - - - - - - - | - - - - - - - - - - - - - - -
      HCC / tensorflow            |    1.0 . 0 | conda           | linux - 64      
      HCC / tensorflow - cpucompat  |    1.0 . 0 | conda           | linux - 64      
      HCC / tensorflow - fma        |    1.0 . 0 | conda           | linux - 64      
      SentientPrime / tensorflow  |    0.6 . 0 | conda           | osx - 64        
                                           : TensorFlow helps the tensors flow
      acellera / tensorflow - cuda  |   0.12 . 1 | conda           | linux - 64      
      anaconda / tensorflow       |    1.0 . 1 | conda           | linux - 64      
      anaconda / tensorflow - gpu   |    1.0 . 1 | conda           | linux - 64      
      conda - forge / tensorflow    |    1.0 . 0 | conda           | linux - 64 , win - 64 , osx - 64
                                           : TensorFlow helps the tensors flow
      creditx / tensorflow        |    0.9 . 0 | conda           | linux - 64      
                                           : TensorFlow helps the tensors flow
      derickl / tensorflow        |   0.12 . 1 | conda           | osx - 64        
      dhirschfeld / tensorflow    | 0.12 . 0rc0 | conda           | win - 64        
      dseuss / tensorflow         |          | conda           | osx - 64        
      guyanhua / tensorflow       |    1.0 . 0 | conda           | linux - 64      
      ijstokes / tensorflow       | 2017.03 . 03.1349 | conda, ipynb    | linux - 64      
      jjh_cio_testing / tensorflow |    1.0 . 1 | conda           | linux - 64      
      jjh_cio_testing / tensorflow - gpu |    1.0 . 1 | conda           | linux - 64      
      jjh_ppc64le / tensorflow    |    1.0 . 1 | conda           | linux - ppc64le 
      jjh_ppc64le / tensorflow - gpu |    1.0 . 1 | conda           | linux - ppc64le 
      jjhelmus / tensorflow       | 0.12 . 0rc0 | conda, pypi     | linux - 64 , osx - 64
                                           : TensorFlow helps the tensors flow
      jjhelmus / tensorflow - gpu   |    1.0 . 1 | conda           | linux - 64      
      kevin - keraudren / tensorflow |    0.9 . 0 | conda           | linux - 64      
      lcls - rhel7 / tensorflow     |   0.12 . 1 | conda           | linux - 64      
      marta - sd / tensorflow       |    1.0 . 1 | conda           | linux - 64      
                                           : TensorFlow helps the tensors flow
      memex / tensorflow          |    0.5 . 0 | conda           | linux - 64 , osx - 64
                                           : TensorFlow helps the tensors flow
      mhworth / tensorflow        |    0.7 . 1 | conda           | osx - 64        
                                           : TensorFlow helps the tensors flow
      miovision / tensorflow      | 0.10 . 0.gpu | conda           | linux - 64 , osx - 64
      msarahan / tensorflow       | 1.0 . 0rc2 | conda           | linux - 64      
      mutirri / tensorflow        | 0.10 . 0rc0 | conda           | linux - 64      
      mwojcikowski / tensorflow   |    1.0 . 1 | conda           | linux - 64      
      rdonnelly / tensorflow      |    0.9 . 0 | conda           | linux - 64      
      rdonnellyr / r - tensorflow   |    0.4 . 0 | conda           | osx - 64        
      test_org_002 / tensorflow   | 0.10 . 0rc0 | conda           |               
Found 32 packages

選擇一個較新的CPU或GPU版本,如jjh_cio_testing/tensorflow-gpu的1.0.1版本,輸入如下命令查詢安裝命令

1
2
3
4
5
6
7
8
9
10
11
12
(可以略掉)$ anaconda show jjh_cio_testing / tensorflow - gpu
 
Using Anaconda API: https: / / api.anaconda.org
Name:    tensorflow - gpu
Summary:
Access:  public
Package Types:  conda
Versions:
    + 1.0 . 1
 
To install this package with conda run:
      conda install - - channel https: / / conda.anaconda.org / jjh_cio_testing tensorflow - gpu

使用最后一行的提示命令進行安裝

1
2
3
4
5
6
7
8
9
10
11
12
$ conda install - - channel https: / / conda.anaconda.org / jjh_cio_testing tensorflow - gpu = = 1.3 . 0
 
Fetching package metadata .............
Solving package specifications: .
 
Package plan for installation in environment / home / will / anaconda2:
 
The following packages will be SUPERSEDED by a higher - priority channel:
 
     tensorflow - gpu: 1.0 . 1 - py27_4 https: / / mirrors.tuna.tsinghua.edu.cn / anaconda / pkgs / free - - > 1.0 . 1 - py27_4 jjh_cio_testing
 
Proceed ([y] / n)?

conda會自動檢測安裝此版本的Tensorflow所依賴的庫,如果你的Anaconda缺少這些依賴庫,會提示你安裝。因為我之前已經安裝過了,所以這里只提示我安裝Tensorflow。輸入y並回車之后等待安裝結束即可

  • 可以選擇次高版本的Tensorflow安裝,因為最新版本可能清華 TUNA的倉庫鏡像庫沒有及時更新,而官方更新連接總是失敗,我最開始選擇了jjhelmus/tensorflow-gpu的1.0.1版本,其他依賴 庫清華 TUNA的倉庫鏡像有資源,而到最后jjhelmus/tensorflow-gpu版本的Tensorflow安裝包總是下載不下來,嘗試20多次之后 換了一個1.0.0的版本,終於順利安裝成功

進入python,輸入

1
import tensorflow as tf 

如果沒有報錯說明安裝成功。

(2)PIP安裝tensorflow

安裝完CUDA 8 和 cuDNN 5后, 在終端輸入 sudo apt-get install libcupti-dev(參考:https://www.cnblogs.com/zengcv/p/6564517.html)

Ubuntu14.04默認安裝的Python2.7.6

先安裝Python庫

1
sudo apt - get install python - pip python - dev 

安裝tensorflow:

      (1)在線安裝 

                sudo pip install https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-1.0.1-cp27-none-linux_x86_64.whl 

   (2)下載安裝(由於Ubuntu系統下,網上比較慢,可以在windows下載。推薦這種安裝方法) 

               sudo pip install tensorflow_gpu-1.0.1-cp27-none-linux_x86_64.whl 

    (下載地址:https://pypi.org/project/tensorflow-gpu/1.0.1/#files)

參考文獻:https://www.cnblogs.com/chamie/p/8876271.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM