Skip to content

Install tensorflow gpu 1.6 for Ubuntu 16.04

Theresa edited this page Apr 26, 2018 · 5 revisions

The document is to describe the steps I took to setup tensorflow for GPU-enabled Ubuntu 16.04.

My Environment

My hardware and operating system. Check whether your system meets CUDA's requirements here.

  • Gigabyte GTX 1080
  • Ubuntu 16.04.2 Desktop X86_64
  • Intel i7 7700 CPU + 48G RAM

Native Linux Distribution Support in CUDA 9.1

After the installation, you will have the following packages installed:

  • nvidia driver 390
  • tensorflow-gpu 1.6
  • CUDA Toolkit 9.0
  • cuDNN 7.0
  • Python 3.5
  • miniconda 3

Enable nvidia driver and install libraries

Install driver for GTX 1080

  1. Run the commands below to install the driver. Remove nvidia drivers if it is already installed. Then update the repository to get the latest updates.
    $ sudo apt-get purge nvidia*
    $ sudo apt-get update
    $ sudo apt-cache search nvidia-driver
    nvidia-304 - NVIDIA legacy binary driver - version 304.135
    nvidia-304-updates - Transitional package for nvidia-304
    nvidia-361 - Transitional package for nvidia-367
    nvidia-384 - NVIDIA binary driver - version 384.111
    nvidia-340 - NVIDIA binary driver - version 340.104
    nvidia-367 - Transitional package for nvidia-387
    nvidia-375 - Transitional package for nvidia-390
    nvidia-387 - NVIDIA binary driver - version 387.26
    nvidia-390 - NVIDIA binary driver - version 390.30
    $ sudo apt-get install nvidia-390
    

Note, if you don't see nvidia-390 or latest version from your repo. Add the nvidia driver repo:

$ sudo add-apt-repository ppa:graphics-drivers/ppa
$ sudo apt-get update
  1. Reboot system for the new driver to work. You can check your installation status with the following commands.

    $ nvidia-smi

Install CUDA Toolkit 9.0

  1. Download CUDA 9.0. Choose Linux -> x86_64 -> Ubuntu -> 16.04 -> runfile(local)and download both the install and the patches. Alternatively, you can also download the latest version. Download CUDA toolkit

  2. Run the command in the terminal sudo sh cuda_9.0.176_384.81_linux-run --override

    It is an interactive installation process. Make choices as below:

    Do you accept the previously read EULA? accept/decline/quit: accept
    Install NVIDIA Accelerated Graphics Driver for Linux-x86_64 390.30?
    (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/theresa]:

  3. Install patches as follow:

    $ sudo sh cuda_9.0.176.1_linux.run
    $ sudo sh cuda_9.0.176.2_linux.run
    
  4. Environment setup

  • The PATH variable needs to include /usr/local/cuda-9.0/bin and the the LD_LIBRARY_PATH variable needs to contain /usr/local/cuda-9.0/lib64 on a 64-bit system, or /usr/local/cuda-9.0/lib on a 32-bit system. Add setup below in ~/.bashrc:
    $ export PATH=/usr/local/cuda-9.0/bin${PATH:+:${PATH}}
    $ export LD_LIBRARY_PATH=/usr/local/cuda-9.0/lib64\
                         ${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
    
  • Make it work in the current session: $ source ~/.bashrc
  • You can verify the CUDA version by the command below: $ nvcc --version

Install cuDNN 7.0

In order to install cuDNN 7.0, you have to register in nVidia's official website here and then download the package. Install the packages as follow:

$ tar xvzf cudnn-9.0-linux-x64-v7.tgz
$ sudo cp cuda/include/cudnn.h /usr/local/cuda-9.0/include
$ sudo cp cuda/lib64/libcudnn* /usr/local/cuda-9.0/lib64
$ sudo chmod a+r /usr/local/cuda-9.0/include/cudnn.h /usr/local/cuda-9.0/lib64/libcudnn*

Setup tensorflow 1.6 environment

Install miniconda 3.0

  1. In order to have a isolated working environment, we use miniconda to have a independent tf environment. Download the installation script and install miniconda:
    $ wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh
    $ bash Miniconda3-latest-Linux-x86_64.sh
    
  2. Reload the PATH environment $ source ~/.bashrc

Install Tensorflow

To create a new environment, there are two ways:

  1. Install tensorflow manually in the virtual environment

    • Create a new environment named "tensorflow1.6" and install tensorflow manually
    $ conda create -n tensorflow1.6 
    $ source activate tensorflow1.6
    $ pip install --ignore-installed --upgrade https://pypi.python.org/packages/ba/12/e4266266712053b6e74057c123d10913f422059d4265c82a43e85766aa65/tensorflow-1.6.0-cp35-cp35m-manylinux1_x86_64.whl#md5=7232cb40e04b9205d3cc3ed05d7930ab
    

    You may find tensorflow for different python versions or platforms here . Replace the url with package you want to use.

  2. Alternatively, you can add all the dependencies in one config file (environment.yml) and install the tensorflow in one command

     $ conda env create -f environment.yml
    
    name: tensorflow1.6
    channels:
      - defaults
    dependencies:
      - python=3.5
      - pip:
        - pillow
        - lxml
        - jupyter
        - matplotlib
        - numpy
        - protobuf
        - tensorflow-gpu==1.6
    

    It is extremely slow to download tensorflow if you work behind a proxy. The connection may break at times. You'd better install all the other tensorflow dependencies firstly. Or you would download tensorflow-gpu from the browser and install it offline.

  3. Test the installation

    $ python
    >>> import tensorflow as tf
    >>> hello = tf.constant('Hello, TensorFlow!')
    >>> sess = tf.Session()
    >>> print(sess.run(hello))
    Hello, TensorFlow!
    >>> a = tf.constant(10)
    >>> b = tf.constant(32)
    >>> print(sess.run(a + b))
    42
    >>>
    

    If you get any error when importing tensorflow, it most probably due to the mismatched cuda/cudnn version with tensorflow1.6. Install the right version by reading what the error message means. Good luck!

Reference

  1. https://zhuanlan.zhihu.com/p/25429108
  2. https://github.com/williamFalcon/tensorflow-gpu-install-ubuntu-16.04/blob/master/README.md
  3. http://docs.nvidia.com/cuda/cuda-installation-guide-linux/