Skip to content

Instantly share code, notes, and snippets.

@mveeneman
Last active November 27, 2018 16:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mveeneman/053612baf7d092d301c8e7cfc930209e to your computer and use it in GitHub Desktop.
Save mveeneman/053612baf7d092d301c8e7cfc930209e to your computer and use it in GitHub Desktop.
Latest versions of NVIDIA CUDA 9.1, OpenCV 3.4 and TensorFlow 1.8 on Ubuntu 18.04 LTS
title author date output
Installing the latest versions of NVIDIA CUDA 9.1, OpenCV 3.4 and TensorFlow 1.8 on Ubuntu 18.04 LTS
Mourad Veeneman
July 23, 2018
html_document

Installing the latest versions of NVIDIA CUDA 9.1, OpenCV 3.4 and TensorFlow 1.8 on Ubuntu 18.04 LTS

Installing CUDA from the NVIDIA repository is not advisable if you want the latest versions of OpenCV and TensorFlow all to work together seamlesly.

Instead you should install the latest NVIDIA binaries and compile OpenCV and Tensorflow from source. It's a little bit of work, but I found it very rewarding in the end.

There are however a few things to take into account. So, here are the steps that work for me:

Installing the latest NVIDIA graphics driver

First make sure you remove the nouveau drivers from your Ubuntu release. Create the modprobe black list for nouveau:

sudo cat > /etc/modprobe.d/blacklist-nouveau.conf << EOF
blacklist nouveau
blacklist lbm-nouveau
options nouveau modeset=0
alias nouveau off
alias lbm-nouveau off
EOF

Don't forget to update the initramfs before you reboot.

sudo update-initramfs -u

Reboot the machine. If it worked correctly the Desktop will re-appear in lower resolution, because you no longer have the driver. Now it's time to install the latest MVIDIA driver for your machine.

Download the latest Linux video driver from NVIDIA site. In my case it was version 390.48. The driver installation detects the capabilities of your graphics card.

http://www.nvidia.com/object/unix.html

sudo apt install gcc make

Run the install script and reboot the machine with the drivers.

sudo sh NVIDIA-Linux-x86_64-396.24.run

Run this command to check if everything went OK:

      nvidia-smi

and see something like this

    Mon May  7 10:02:33 2018       
    +-----------------------------------------------------------------------------+
    | NVIDIA-SMI 390.48                 Driver Version: 390.48                    |
    |-------------------------------+----------------------+----------------------+
    | GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
    | Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
    |===============================+======================+======================|
    |   0  TITAN X (Pascal)    Off  | 00000000:01:00.0  On |                  N/A |
    | 23%   35C    P8    16W / 250W |    240MiB / 12192MiB |      0%      Default |
    +-------------------------------+----------------------+----------------------+
                                                                           
    +-----------------------------------------------------------------------------+
    | Processes:                                                       GPU Memory |
    |  GPU       PID   Type   Process name                             Usage      |
    |=============================================================================|
    |    0      1212      G   /usr/lib/xorg/Xorg                            16MiB |
    |    0      1296      G   /usr/bin/gnome-shell                          13MiB |
    |    0     13074      G   /usr/lib/xorg/Xorg                           108MiB | 
    |    0     13220      G   /usr/bin/gnome-shell                          99MiB |
    +-----------------------------------------------------------------------------+

Install CUDA 9.1

Download the latest CUDA binaries from the NVIDIA Developers site. You will need to register a developers account with NVIDIA if you don't already have one.

You can download it from NVIDIA's CUDAZone

NVIDIA_CUDA

The default gcc version on Ubuntu 18.04 is gcc-7. However, when we build OpenCV from source with CUDA support, it requires gcc-6.

    sudo apt-get install gcc-6 g++-6

Verify the gcc version:

    gcc --version

You may stil see version 7 detected. We have to set higher priority for gcc-6 as follows (assuming your gcc installation is located at /usr/bin/gcc-6, and gcc-7's priority is less than 60.

    sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-6 60
    sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-6 60

Now, to fix the CXX and CC environment variables, add the following lines in your .bashrc file:

    echo 'export CXX=/usr/bin/g++-6' >> ~/.bashrc
    echo 'export CC=/usr/bin/gcc-6' >> ~/.bashrc
    source ~/.bashrc

Now you can install the CUDA binaries

    sudo sh cuda_9.1.85_387.26_linux.run

After reading and accepting the license. Do NOT install the Graphics Driver. The first question is a little misleading

    You are attempting to install on an unsupported configuration. 
    Do you wish to continue? (y)es/(n)o [ default is no ]: y
    
    Install NVIDIA Accelerated Graphics Driver for Linux-x86_64 387.26? (y)es/(n)o/(q)uit: n
    
    Install the CUDA 9.1 Toolkit? (y)es/(n)o/(q)uit: y
    
    Enter Toolkit Location [ default is /usr/local/CUDA-9.1 ]: 
     
    Do you want to install a symbolic link at /usr/local/CUDA? (y)es/(n)o/(q)uit: y
    
    Install the CUDA 9.1 Samples? (y)es/(n)o/(q)uit: y
    
    Enter CUDA Samples Location [ default is /home/username ]: 

Follow the instructions for PATH and LD_LIBRARY_PATH.

    Please make sure that
     -   PATH includes /usr/local/cuda-9.1/bin
     -   LD_LIBRARY_PATH includes /usr/local/cuda-9.1/lib64, or, add /usr/local/cuda-9.1/lib64 to /etc/ld.so.conf and run ldconfig

I added in my .bashrc file export PATH=${PATH}:/usr/local/cuda/bin and copied the directories for shared libraries in /etc/ld.so.conf (then run sudo ldconfig)

Don't forget to also install the patches:

sudo sh ./cuda_9.1.85.1_linux.run sudo sh ./cuda_9.1.85.2_linux.run sudo sh ./cuda_9.1.85.3_linux.run

Since you've already installed the latest driver, you can ignore the warning about not having installed the driver.

Install CuDNN 9.1

Download CuDNN 9.1 from the NVIDIA Developers site and unpack it in CUDA directory /usr/local/cuda (sym links to cuda-9.1) You will need to log in to the NVIDIA developer site.

    cd /usr/local/
    sudo tar xvf ~/Downloads/cudnn-9.1-linux-x64-v7.1.tgz
    sudo ldconfig

Verify installation:

nvcc -V
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2017 NVIDIA Corporation
Built on Fri_Nov__3_21:07:56_CDT_2017
Cuda compilation tools, release 9.1, V9.1.85

Compiling and Installing OpenCV

Details on installing OpenCV you can find in OpenCV Installation in Linux In your workdirectory:

sudo apt install -y git cmake python-dev python-pip libpng-dev ccache 
sudo apt install -y libv4l-dev 
sudo apt install -y libopus-dev libmp3lame-dev libfdk-aac-dev libvpx-dev libx264-dev yasm libass-dev libtheora-dev libvorbis-dev mercurial
sudo -H pip install numpy
mkdir ~/src
cd ~/src
git clone https://github.com/opencv/opencv
git clone https://github.com/opencv/opencv_contrib
cd opencv
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D WITH_GTK2=ON -D CXX_FLAG="-std=c++11" -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_CUDA=ON -D CUDA_FAST_MATH=1 -D WITH_CUBLAS=ON -D WITH_GSTREAMER=ON -D WITH_TBB=ON -D WITH_V4L=ON -D WITH_QT=OFF -D WITH_OPENGL=ON -D BUILD_PROTOBUF=OFF -D CUDA_NVCC_FLAGS="-ccbin gcc-6" ..

Make sure you make optimal use of all your processor cores, otherwise make is going to take a very long time to compile OpenCV

    make -j7

Installing TensorFlow from sources

Next we need to install Bazel

    sudo apt-get install openjdk-8-jdk
    sudo apt-get install curl
    sudo apt-get install zip zlib1g-dev unzip
    
    echo "deb [arch=amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list
    curl https://bazel.build/bazel-release.pub.gpg | sudo apt-key add -
    sudo apt-get update && sudo apt-get install bazel
    sudo apt-get upgrade bazel
    
    wget https://github.com/bazelbuild/bazel/releases/download/0.11.1/bazel-0.11.1-installer-linux-x86_64.sh
    chmod +x bazel-0.11.1-installer-linux-x86_64.sh
    ./bazel-0.11.1-installer-linux-x86_64.sh --user
    
    export PATH="$PATH:$HOME/bin"
    source ~/.bashrc

To verify installation of Bazel run:

    bazel version

Download the latest version of TensorFlow:

TensorFlow 1.8.0

    ./configure
    You have bazel 0.11.1 installed.
    Please specify the location of python. [Default is /usr/bin/python]:    
    
    Found possible Python library paths:
      /usr/local/lib/python2.7/dist-packages/
      /usr/local/tensorflow/models/research
    Please input the desired Python library path to use.  Default is [/usr/local/tensorflow/models/research/slim]
    /usr/local/lib/python2.7/dist-packages/
    Do you wish to build TensorFlow with jemalloc as malloc support? [Y/n]: 
    jemalloc as malloc support will be enabled for TensorFlow.
    
    Do you wish to build TensorFlow with Google Cloud Platform support? [Y/n]: n
    No Google Cloud Platform support will be enabled for TensorFlow.
    
    Do you wish to build TensorFlow with Hadoop File System support? [Y/n]: n
    No Hadoop File System support will be enabled for TensorFlow.
    
    Do you wish to build TensorFlow with Amazon S3 File System support? [Y/n]: n
    No Amazon S3 File System support will be enabled for TensorFlow.
    
    Do you wish to build TensorFlow with Apache Kafka Platform support? [Y/n]: n
    No Apache Kafka Platform support will be enabled for TensorFlow.
    
    Do you wish to build TensorFlow with XLA JIT support? [y/N]: n
    No XLA JIT support will be enabled for TensorFlow.
    
    Do you wish to build TensorFlow with GDR support? [y/N]: n
    No GDR support will be enabled for TensorFlow.
    
    Do you wish to build TensorFlow with VERBS support? [y/N]: n
    No VERBS support will be enabled for TensorFlow.
    
    Do you wish to build TensorFlow with OpenCL SYCL support? [y/N]: n
    No OpenCL SYCL support will be enabled for TensorFlow.
    
    Do you wish to build TensorFlow with CUDA support? [y/N]: y
    CUDA support will be enabled for TensorFlow.
    
    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]:  
    
    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]:
    
    Do you wish to build TensorFlow with TensorRT support? [y/N]: 
    No TensorRT support will be enabled for TensorFlow.
    
    Please specify the NCCL version you want to use. [Leave empty to default to NCCL 1.3]: 
    
    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: 6.1]
    
    Do you want to use clang as CUDA compiler? [y/N]: 
    nvcc will be used as CUDA compiler.
    
    Please specify which gcc should be used by nvcc as the host compiler. [Default is /usr/bin/gcc]: 
    
    Do you wish to build TensorFlow with MPI support? [y/N]: 
    No MPI support will be enabled for TensorFlow.
    
    Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -march=native]: 
    
    Would you like to interactively configure ./WORKSPACE for Android builds? [y/N]: 
    Not configuring the WORKSPACE for Android builds.
    
    Preconfigured Bazel build configs. You can use any of the below by adding "--config=<>" to your build command. See tools/bazel.rc for more details.
            --config=mkl            # Build with MKL support.
            --config=monolithic     # Config for mostly static monolithic build.
    Configuration finished

Now you can build it

    bazel build --config=opt --config=CUDA //tensorflow/tools/pip_package:build_pip_package

Install TensorFlow

    pip install tensorflow-gpu  # Python 2.7;  GPU support

More detais you can find at Installing TensorFlow from Sources

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