主要参考:
https://github.com/williamFalcon/tensorflow-gpu-install-ubuntu-16.04
但原文章中有一些错已改正。
我使用的版本以及下载的相关软件:
ubuntu 16.04
cuda 9.0
cudnn 7.1
python 3.6/python 2.7
miniconda
版本的匹配很重要,版本对不上就会出错,安装过程中也能看到相互依赖的版本。
1.Nouveau drivers
在安装英伟达的驱动之前需要需要卸载ubuntu自带的开源英伟达驱动nouveau.
Create a file at /etc/modprobe.d/blacklist-nouveau.conf with the following content
sudo vi /etc/modprobe.d/blacklist-nouveau.conf
blacklist nouveau
options nouveau modeset=0
Regenerate the kernel initramfs
sudo update-initramfs -u
sudo reboot
On reboot, verify that noveau drivers are not loaded
lsmod | grep nouveau
If nouveau driver(s) are still loaded do not proceed and troubleshoot why it's still loaded.
2.Installation dependency package
sudo apt-get update
sudo apt-get install openjdk-8-jdk git build-essential swig libcurl3-dev curl
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python3.6 #可以不安装
sudo apt-get install python-dev python3-dev python-pip python3-pip
sudo apt-get install python-numpy python3-numpy python-virtualenv python-wheel
3.install CUDA Toolkit
cuda下载网址:
https://developer.nvidia.com/cuda-toolkit-archive
install cuda-repo-ubuntu1604-9-0-local_9.0.176-1_amd64.deb
sudo dpkg -i cuda-repo-ubuntu1604-9-0-local_9.0.176-1_amd64.deb
sudo apt-get update sudo apt-key add /var/cuda-repo-9-0-local/7fa2af80.pub
sudo apt-get install cuda-9-0
sudo reboot
check nvidia driver install
nvidia-smi
# you should see a list of gpus printed
# if not, the previous steps failed.
4.install cudnn
下载网址:
https://developer.nvidia.com/rdp/cudnn-archive
install cudnn-9.0-linux-x64-v7.1.tgz
sudo tar -xzvf cudnn-9.0-linux-x64-v7.0.x.tgz #这里需要自己改一下版本号
sudo cp cuda/include/cudnn.h /usr/local/cuda/include
sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64
sudo chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn*
Add these lines to end of ~/.bashrc
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/cuda/lib64:/usr/local/cuda/extras/CUPTI/lib64"
export CUDA_HOME=/usr/local/cuda
export PATH="$PATH:/usr/local/cuda/bin"
Reload bashrc
source ~/.bashrc
5.Install miniconda
下载网址:
https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/
install Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh
# press s to skip terms
# Do you approve the license terms? [yes|no]
# yes
# Miniconda3 will now be installed into this location:
# accept the location
# Do you wish the installer to prepend the Miniconda3 install location
# to PATH in your /home/ghost/.bashrc ? [yes|no]
# yes
Reload bashrc
source ~/.bashrc
使用miniconda 的基本操作:
https://www.jianshu.com/p/d2e15200ee9b
install miniconda 指定python 版本 以及需要安装的包(根据自己的需要选择)
conda create -n tensorflow_py27 python=2.7 NumPy tqdm
# press y a few times
Activate env
source activate tensorflow_py27
Install tensorflow1.5.0 with GPU support for python 2.7
其他版本的下载网址:需要注意python 版本要与下载的tensorflow对应上
http://mirrors.nju.edu.cn/tensorflow/linux/gpu/
pip install --ignore-installed –upgrade tensorflow_gpu-1.5.0-cp27-none-linux_x86_64.whl
6.test tensorflow
# start python shell
python
# run test script
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
# when you run sess, you should see a bunch of lines with the word gpu in them (if install worked)
# otherwise, not running on gpu
sess = tf.Session()
print(sess.run(hello))