Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save shengchiliu/3ee8bff8a17b28881360c8f16f8d85b3 to your computer and use it in GitHub Desktop.
Save shengchiliu/3ee8bff8a17b28881360c8f16f8d85b3 to your computer and use it in GitHub Desktop.
CUDA9.1 & cuDNN7.1 & TensorFlow1.7 on Ubuntu17.10

Install CUDA9.1 & cuDNN7.1 & TensorFlow1.7 on Ubuntu17.10

This instruction is about installing CUDA 9.1 and cuDNN 7.1 libraries on Ubuntu 17.10 in order to use the GPUs to accelerate computations in PyTorch, TensorFlow or other DL frameworks.

1. Use Integrated Graphics

  • Set Integrated Graphics Device (IGD) as the primary display in BIOS
  • Connect your monitor to IGD port

2. Download CUDA and cuDNN

  • Download CUDA here
  • Download cuDNN here

3. Stop GUI

# Login tty console (Ctrl + Alt + F3)
$ sudo service gdm stop

4. Install CUDA

# Under the Download directory
$ sudo dpkg -i cuda-repo-ubuntu1704-9-1-local_9.1.85-1_amd64.deb
$ sudo apt-key add /var/cuda-repo-9-1-local/7fa2af80.pub
$ sudo apt update
$ sudo apt install cuda
$ sudo reboot

5. Install cuDNN

# Under the Download directory
$ tar xvzf cudnn-9.1-linux-x64-v7.1.tgz
$ sudo cp cuda/include/cudnn.h /usr/local/cuda-9.1/include/
$ sudo cp cuda/lib64/libcudnn* /usr/local/cuda-9.1/lib64/
$ sudo chmod a+r /usr/local/cuda-9.1/include/cudnn.h /usr/local/cuda-9.1/lib64/libcudnn*

6. Set Environment Variables in .bashrc File

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda-9.1/lib64/:/usr/local/cuda-9.1/extras/CUPTI/lib64/:/usr/lib/nvidia-YOUR_DRIVER_VERSION
export CUDA_HOME=/usr/local/cuda-9.1
export PATH=$PATH:/usr/local/cuda-9.1/bin

Find your nvidia driver version here /usr/lib/nvidia-387

7. Check CUDA, GPU and Driver Version

$ nvcc -V       # if it shows the CUDA information, meaning the installation succeeded 
$ nvidia-smi    # if it shows "nvidia-smi command not found", then you need to renew your nvidia driver

* Renew Nvidia Driver

sudo apt purge nvidia-*
sudo add-apt-repository ppa:graphics-drivers/ppa
sudo apt update
sudo apt install nvidia-390     # choose a version suitable for your GPU

8. Install TensorFlow 1.7 from Sources

8.1 Install gcc 6, Basel, and Dependencies
# Install dependencies
$ sudo apt-get install libcupti-dev g++ freeglut3-dev build-essential libx11-dev libxmu-dev libxi-dev libglu1-mesa libglu1-mesa-dev

# Install gcc 6 for CUDA 9.1
$ sudo apt install gcc-6
$ sudo apt install g++-6

$ sudo ln -s /usr/bin/gcc-6 /usr/local/cuda-9.1/bin/gcc
$ sudo ln -s /usr/bin/g++-6 /usr/local/cuda-9.1/bin/g++

# Install basel using binary installer
$ cd ~/Downloads
$ wget https://github.com/bazelbuild/bazel/releases/download/0.11.0/bazel-0.11.0-installer-linux-x86_64.sh
$ sudo bash ./bazel-0.11.0-installer-linux-x86_64.sh
8.2 Configure TensorFlow from Source
# Download TensorFlow
$ cd ~/Downloads
$ wget https://github.com/tensorflow/tensorflow/archive/v1.7.0.zip
$ unzip v1.7.0.zip
$ cd tensorflow-1.7.0

Note:

  1. If you wanna install tensorflow in a virtual environment, you mightneed to activate the virtual environment now.
  2. In the configuration, careful about the thing like the location of python, gcc version, CUDA support, CUDA and cuDNN version, and the others depend on your need.
# Configure TensorFlow
$ ./configure
Please specify the location of python. [Default is /usr/bin/YOUR_PYTHON_DIRECTORY]:
(If it's the right location, just press Enter)

Do you wish to build TensorFlow with jemalloc as malloc support? [Y/n]: Y
Do you wish to build TensorFlow with Google Cloud Platform support? [Y/n]: N
Do you wish to build TensorFlow with Hadoop File System support? [Y/n]: N
Do you wish to build TensorFlow with Amazon S3 File System support? [Y/n]: N
Do you wish to build TensorFlow with Apache Kafka Platform support? [y/N]: N
Do you wish to build TensorFlow with XLA JIT support? [y/N]: N
Do you wish to build TensorFlow with GDR support? [y/N]: N
Do you wish to build TensorFlow with VERBS support? [y/N]: N
Do you wish to build TensorFlow with OpenCL SYCL support? [y/N]: N

Do you wish to build TensorFlow with CUDA support? [y/N]: Y
Please specify the CUDA SDK version you want to use, e.g. 7.0. [Leave empty to default to CUDA 9.0]: 9.1
Please specify the location where CUDA 9.1 toolkit is installed. Refer to README.md for more details. [Default is /usr/local/cuda]: /usr/local/cuda
Please specify the cuDNN version you want to use. [Leave empty to default to cuDNN 7.0]: 7.1.2
Please specify the location where cuDNN 7 library is installed. Refer to README.md for more details. [Default is /usr/local/cuda]:

Please specify a list of comma-separated Cuda compute capabilities you want to build with.
You can find the compute capability of your device at: https://developer.nvidia.com/cuda-gpus.
Please note that each additional compute capability significantly increases your build time and binary size. [Default is: 3.5,5.2]

Do you want to use clang as CUDA compiler? [y/N]: N
Please specify which gcc should be used by nvcc as the host compiler. [Default is /usr/bin/gcc]: /usr/bin/x86_64-linux-gnu-gcc-6

Do you wish to build TensorFlow with MPI support? [y/N]: N
Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -march=native]: -march=native
Would you like to interactively configure ./WORKSPACE for Android builds? [y/N]:N

Configuration finished
8.3 Build Tensorflow using Bazel
$ bazel build --config=opt --config=cuda --cxxopt="-D_GLIBCXX_USE_CXX11_ABI=0" //tensorflow/tools/pip_package:build_pip_package

# The process will probably run 1 hour, and at the end it will issue a command and you need to run it.
$ bazel-bin/tensorflow/tools/pip_package/build_pip_package tensorflow_pkg

$ cd tensorflow_pkg
$ pip install tensorflow-1.7.0-cp36-cp36m-linux_x86_64.whl  # pip install the file under the directory
8.4 Verify Tensorflow
$ python -c "import tensorflow; print(tensorflow.__version__)"
# If the tensorflow installation sucessed, it will print the tensorflow version, which is 1.7.0.

References (Ref1, Ref2) of this TensorFlow installation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment