环境介绍

系统:Ubuntu 18.04.1
显卡:GTX 1050 Ti
其他:Python 3.6 + Tensorflow 1.10.1

更新NVIDIA驱动

  1. 更新

    1
    2
    sudo apt-get update
    sudo apt-get upgrade
  2. 选择software & updates->Additional Drivers,选择对应的NVIDIA驱动. 通过https://www.nvidia.cn/Download/index.aspx可以查看不同版本显卡对应的驱动

Python及Virtualenv

1
sudo apt-get install python3-pip python3-dev python3-virtualenv

降低gcc版本

因为Ubuntu18的gcc是7,而CUDA 9.0 要求的gcc版本的5.x或6.x.

执行以下命令进行降级

1
2
3
sudo apt install gcc-6
sudo apt install g++-6
gcc --version

安装CUDA 9.0

下载对应版本安装

1
2
3
4
5
sudo bash cuda_9.0.176_384.81_linux.run --override
sudo bash cuda_9.0.176.1_linux.run --override
sudo bash cuda_9.0.176.2_linux.run
sudo bash cuda_9.0.176.3_linux.run
sudo bash cuda_9.0.176.4_linux.run

注意安装过程中 Install NVIDIA Accelerated Graphics Driver 选择n,因为前面安装的才是相匹配的驱动。

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
Description

The NVIDIA CUDA Toolkit provides command-line and graphical
tools for building, debugging and optimizing the performance
Do you accept the previously read EULA?
accept/decline/quit: accept

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 /home/cqc ]:

Installing the CUDA Toolkit in /usr/local/cuda-9.0 ...

安装完毕后进入cd /etc目录执行如下命令:

1
2
sudo cp -rf ld.so.conf ld.so.conf.bak
sudo vi f ld.so.conf.bak

添加CUDA 9.0 到库文件LD_LIBRARY_PATH:

1
include /usr/local/cuda-9.0/lib64

利用降级的gcc进行如下设置

1
2
sudo ln -s /usr/bin/gcc-6 /usr/local/cuda/bin/gcc
sudo ln -s /usr/bin/g++-6 /usr/local/cuda/bin/g++

安装cuDNN 7.0

解压安装包并复制到CUDA Toolkit的目录

1
2
3
4
tar -xzvf  cudnn-9.0-linux-x64-v7.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*

将cuda的一些必要路径添加到系统环境变量里.

1
vi ~/.bashrc

需要添加一下内容.

1
2
export PATH=${PATH}:/usr/local/cuda-9.0/bin
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}/usr/local/cuda-9.0/lib64:/usr/local/cuda-9.0/lib:/usr/local/cuda/extras/CUPTI/lib64

执行以下命令使之生效

1
source ~/.profile

安装bazel

下载并安装.

1
sudo bash ./bazel-0.7.0-installer-linux-x86_64.sh

安装成功的标志.

安装Tensorflow-GPU

  1. 确保pip大于8.

    1
    pip3 install --upgrade pip3
  2. 创建Python虚拟环境.

    1
    virtualenv --system-site-packages -p python3 ~/tensorflow

    ~/tensorflow是自己选择的位置并创建的目录.

  3. 激活虚拟环境.

    1
    source ~/tensorflow/bin/activate
  4. 安装Tensorflow.

    1
    pip3 install --upgrade tensorflow-gpu
  5. 验证.

    1
    2
    3
    4
    import tensorflow as tf
    hello = tf.constant('Hello, TensorFlow!')
    sess = tf.Session()
    print(sess.run(hello))

VS Code进行开发

  1. 下载并安装VS Code
  2. 安装Python扩展插件(Python extension for Visual Studio Code)
  3. 选择Python执行环境
    ctrl + shift + p,输入Python:Select Interpreter,选择你的Python安装目录
  4. 在VS Code的Terminal中激活Virtualenv环境中的Tensorflow。

    1
    source ~/tensorflow/bin/activate
  5. 运行Tensorflow的Python程序

安装keras

使用pip进行安装.

1
pip3 install keras

使用Jupyter

安装Jupyter.

1
sudo pip3 install jupyter

运行jupyter.

1
jupyter notebook