參考
- https://tensorflow.google.cn/install/install_linux
- http://nvidia.com/cuda
- http://developer.nvidia.com/cudnn
說明
- 前提是機器上必須有Nvidia顯卡,不太老就好(古董也沒必要玩這個了吧,費電),在Nvidia官網可以查到顯卡支持情況 https://developer.nvidia.com/cuda-gpus
- 安裝過程中的命令都需要root身份,請使用su root切換或者每次加 sudo,編譯運行測試代碼使用普通用戶就好
踩坑后的提示,怪我眼瞎坑自己,[手動抽臉表情]
- 必須按tensorflow 官網提示的版本安裝 1.9 對應 CUDA 9.0,CUDA 9.0 要下載相應版本的cuDNN
- 如果喜歡折騰,建議使用沒有重要數據的硬盤
- 安裝包最好下載到其他電腦上,使用scp拷貝到安裝機上,重裝了幾遍ubuntu,下一次包就2個G,作為聯通40G所謂無線流量卡用戶,想着還是蛋疼
下載主要安裝文件
- CUDA® 工具包
#http://nvidia.com/cuda
#我選的是16.04的run文件,其他的坑不敢踩了
cuda_9.0.176_384.81_linux.run
- cuDNN 深度神經網絡(DNN)開發環境,需要網站注冊
#http://developer.nvidia.com/cudnn
libcudnn7-dev_7.1.4.18-1+cuda9.0_amd64.deb
libcudnn7_7.1.4.18-1+cuda9.0_amd64.deb
libcudnn7-doc_7.1.4.18-1+cuda9.0_amd64.deb
准備環境
看CUDA自帶的驅動版本,這里是384.81,低於這個版本就要先卸載,>= 跳過
#建議run文件卸載,即你之前下載的Nvidia驅動run文件
chmod +x *.run
./NVIDIA-Linux-x86_64-384.59.run --uninstall
# 不建議采取這種,不知道為什么沒嘗試過 apt-get remove --purge nvidia*
禁用自帶的nouveau驅動,如果你連Nvidia驅動都裝過了,這一步也免了
vi /etc/modprobe.d/blacklist.conf
#加兩行
blacklist nouveau
options nouveau modeset=0 #生效配置 update-initramfs -u #重啟,后分辨率變低了,畢竟沒有顯卡驅動了 reboot #檢查是否生效 lsmod | grep nouveau #如果屏幕沒有輸出則禁用nouveau成功
安裝必要的編譯環境否者自帶網卡驅動安裝不上
apt install gcc g++ make make-guile
針對CUDA 9.0,必須將GCC降級為gcc5,也是安裝CUDA時發現的
apt install gcc-5 g++-5
update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-5 50
update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-5 50
安裝 CUDA® 工具包
一定要根據tensorflow版本安裝對應版本的CUDA 1.9對應9.0,被自己眼瞎害的
chmod +x cuda_9.0.176_384.81_linux.run
sh ./cuda_9.0.176_384.81_linux.run
#會有說明,需要看的自己看,看了幾頁不想看/條款看不懂的 按q鍵
- 如果安裝過程中提示失敗,根據提示查看log排錯
- 安裝成功后的log
Do you accept the previously read EULA?
accept/decline/quit: accept
You are attempting to install on an unsupported configuration. Do you wish to continue?
(y)es/(n)o [ default is no ]: y
#這里384.81表示顯卡驅動版本,如果本機安裝的顯卡驅動版本比它高就不需要安裝
#選no主要是前面踩坑的時候安了CUDA9.2,呵呵
#正常應該是yes
Install NVIDIA Accelerated Graphics Driver for Linux-x86_64 384.81?
(y)es/(n)o/(q)uit: n
Install the CUDA 9.0 Toolkit?
(y)es/(n)o/(q)uit: y
Enter Toolkit Location
[ default is /usr/local/cuda-9.0 ]:
Do you want to install a symbolic link at /usr/local/cuda?
(y)es/(n)o/(q)uit: y
Install the CUDA 9.0 Samples?
(y)es/(n)o/(q)uit: y
Enter CUDA Samples Location
[ default is /root ]:
Installing the CUDA Toolkit in /usr/local/cuda-9.0 ...
Missing recommended library: libGLU.so
Missing recommended library: libX11.so
Missing recommended library: libXi.so
Missing recommended library: libXmu.so
Missing recommended library: libGL.so
Installing the CUDA Samples in /root ...
Copying samples to /root/NVIDIA_CUDA-9.0_Samples now...
Finished copying samples.
===========
= Summary =
===========
Driver: Not Selected
Toolkit: Installed in /usr/local/cuda-9.0
Samples: Installed in /root, but missing recommended libraries
Please make sure that
- PATH includes /usr/local/cuda-9.0/bin
- LD_LIBRARY_PATH includes /usr/local/cuda-9.0/lib64, or, add /usr/local/cuda-9.0/lib64 to /etc/ld.so.conf and run ldconfig as root
To uninstall the CUDA Toolkit, run the uninstall script in /usr/local/cuda-9.0/bin
Please see CUDA_Installation_Guide_Linux.pdf in /usr/local/cuda-9.0/doc/pdf for detailed information on setting up CUDA.
***WARNING: Incomplete installation! This installation did not install the CUDA Driver. A driver of version at least 384.00 is required for CUDA 9.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 /tmp/cuda_install_7657.log
/root/NVIDIA_CUDA-9.0_Samples
設置環境變量
vi /etc/ld.so.conf.d/cuda.conf
#寫入兩行
/usr/local/cuda/lib64
/usr/local/cuda/extras/CUPTI/lib64
vi /etc/profile
#加入兩行
export CUDA_HOME=/usr/local/cuda/bin export PATH=$PATH:$CUDA_HOME
重啟 reboot
測試安裝情況
- 沒有報錯就表示安裝成功
cd /root/NVIDIA_CUDA-9.0_Samples/samples/1_Utilities/deviceQuery
make
./deviceQuery
# Result = PASS 成功 cd ../bandwidthTest make ./bandwidthTest #Result = PASS 成功
cuDNN 安裝
NVIDIA cuDNN is a GPU-accelerated library of primitives for deep neural networks.
#cuDNN v7.1.4 Runtime Library for Ubuntu16.04 (Deb)
dpkg -i libcudnn7_7.1.4.18-1+cuda9.0_amd64.deb
#cuDNN v7.1.4 Developer Library for Ubuntu16.04 (Deb) dpkg -i libcudnn7-dev_7.1.4.18-1+cuda9.0_amd64.deb #cuDNN v7.1.4 Code Samples and User Guide for Ubuntu16.04 (Deb) libcudnn7-doc_7.1.4.18-1+cuda9.0_amd64.deb # 鎖定版本,免得自動更新破壞環境 apt-mark hold libcudnn7 libcudnn7-dev
測試
#Copy the cuDNN sample to a writable path.
$cp -r /usr/src/cudnn_samples_v7/ $HOME #Go to the writable path. $ cd $HOME/cudnn_samples_v7/mnistCUDNN #Compile the mnistCUDNN sample. $make clean && make #Run the mnistCUDNN sample. $ ./mnistCUDNN #If cuDNN is properly installed and running on your Linux system, you will see a message similar to the following: #Test passed!
安裝 tensorflow-gpu 以python3為例
sudo apt-get install python3-pip python3-dev pip3 install tensorflow-gpu
測試安裝
#測試代碼,保存到比如test.py
import tensorflow as tf hello = tf.constant('Hello, TensorFlow!') sess = tf.Session() print(sess.run(hello)) #執行 python3 test.py #第一次有點慢 #沒報錯,有顯卡信息,b'Hello, TensorFlow!',表示成功
