Compiling and Installing GPU-Supported TensorFlow 1.8.0 from Source on Ubuntu 18.04

Compiling and Installing GPU-Supported TensorFlow 1.8.0 from Source on Ubuntu 18.04

When I first used Linux, the first thing I wanted to do was install the GPU version of TensorFlow. I found a great guide, but even considering that, I spent over 40 hours on the installation. In this article, I want to save you time and share my experience. Below you will find an updated guide from python36.com.

Step 1: Update and Upgrade Your System:

sudo apt-get update 
sudo apt-get upgrade

Step 2: Verify That You Have a CUDA-Compatible GPU:

lspci | grep -i nvidia

Note the GPU model. For example: GeForce 840M

If you do not see any settings, update the Linux-maintained PCI hardware database by typing update-pciids in the command line (usually located in /sbin) and then re-run the previous lspci command.

Step 3: Verify That You Have a Supported Linux Version:

To determine the distribution and version number you are running, type the following in the command line:

uname -m && cat /etc/*release

x86_64 indicates that you are running on a 64-bit system which supports CUDA 9.1.

Step 4: Install Dependencies:

The following libraries must be installed to compile from source:

sudo apt-get install build-essential 
sudo apt-get install cmake git unzip zip
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python2.7-dev python3.5-dev python3.6-dev pylint

Step 5: Install the Linux Kernel:

Go to the terminal and type:

uname -r

You should get a version number similar to “4.15.0-23-generic”. Take note of your Linux kernel version.

In Ubuntu 18.04, you have the 4.15 version Linux kernel. However, because of this version, you may get stuck on the CUDA installation. I searched on Google for several hours and did not solve this issue.

Compiling and Installing GPU-Supported TensorFlow 1.8.0 from Source on Ubuntu 18.04

If you see this on the terminal, your CUDA is not installed correctly.

To avoid this issue, you need to install the 4.16 kernel:

wget http://kernel.ubuntu.com/~kernel-ppa/mainline/v4.16/linux-headers-4.16.0-041600_4.16.0-041600.201804012230_all.deb
wget http://kernel.ubuntu.com/~kernel-ppa/mainline/v4.16/linux-headers-4.16.0-041600-generic_4.16.0-041600.201804012230_amd64.deb
wget http://kernel.ubuntu.com/~kernel-ppa/mainline/v4.16/linux-image-4.16.0-041600-generic_4.16.0-041600.201804012230_amd64.deb

After downloading all the above kernel files, install them as follows. The Linux headers will also be installed with this command:

sudo dpkg -i *.deb

Once the installation is complete, restart the computer and verify that you are using the new kernel version:

uname -sr

You should get something like this:

Compiling and Installing GPU-Supported TensorFlow 1.8.0 from Source on Ubuntu 18.04

Now you have the correct version of the kernel.

The kernel version you are using is newer than the default installed kernel version of Ubuntu.

Step 6: Install NVIDIA CUDA 9.2:

Remove previous CUDA installations:

sudo apt-get purge nvidia*
sudo apt-get autoremove
sudo apt-get autoclean
sudo rm -rf /usr/local/cuda*

Install CUDA:

sudo apt-key adv --fetch-keys http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1710/x86_64/7fa2af80.pub
echo "deb https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1710/x86_64 /" | sudo tee /etc/apt/sources.list.d/cuda.list
sudo apt-get update 
sudo apt-get -o Dpkg::Options::="--force-overwrite" install cuda-9-2 cuda-drivers

Pay attention to Step 6 during the execution of the last command!

During the first attempt, the script usually hangs at unitramfs (every time I boot on the 4.15 kernel, I see this unitramfs file not found, and the CUDA installation fails). On the 4.16 kernel, you will not encounter this issue.

Building for 4.15.0-22-generic
Building for architecture x86_64
Building initial module for 4.15.0-22-generic

If this line does not update within a few minutes, open the system monitor and wait for the CPU core load to decrease. Do not type immediately!

Then try this in the terminal:

Hit ESC a few times and then type: password + Enter + password + Enter..

If that doesn’t help:

Hit ESC a few times and then type: password + Enter + password + Enter..

Be patient and slowly type the password. Hit ESC 10 times and type again.

If you are installing CUDA on a new system, you only need to “blindly” enter the password once. Otherwise, be prepared to do it twice: when compiling the kernel, when you see this message:

+++
writing new private key to ‘/var/lib/shim-signed/mok/MOK.priv’
— — -

This way you will succeed!

Step 7: Restart the System to Load NVIDIA Drivers.

Step 8: Go to the Terminal and Type:

echo 'export PATH=/usr/local/cuda-9.2/bin${PATH:+:${PATH}}' >> ~/.bashrc
echo 'export LD_LIBRARY_PATH=/usr/local/cuda-9.2/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}' >> ~/.bashrc
source ~/.bashrc
sudo ldconfig
nvidia-smi

Check the driver version, which may be: 396.26.

Now, if you use the nvidia-smi command, you can get the temporary properties of the GPU, and that’s it (no processes found below). Also, your screen resolution is low because your NVIDIA drivers do not detect the GPU monitor.

Compiling and Installing GPU-Supported TensorFlow 1.8.0 from Source on Ubuntu 18.04

I know how bad it is to work with a FullHD monitor at only 1024×768 resolution.

But you can solve this with Xorg!

Use this command to create Xorg:

sudo nvidia-xconfig
This will create a file xorg.conf in the path: (etc/X11/xorg.conf). To change the resolution, you need to modify this file. To do this, use this command along with the monitor parameters. For my computer, my command is as follows:
cvt 1920x1080 60

Press Enter.

Modeline "1920x60_60.00" 9.25 1920 1976 2160 2400 60 63 73 76 -hsync + vsync

Just copy it and open xorg.conf:

sudo -i gedit /etc/X11/xorg.conf

Paste it here (instead of modeline). Also change HorizSync and VertRefresh:

Compiling and Installing GPU-Supported TensorFlow 1.8.0 from Source on Ubuntu 18.04

Then restart it. Now your screen resolution should be back to normal. Now you can enter:

nvidia-settings

To see this:

Compiling and Installing GPU-Supported TensorFlow 1.8.0 from Source on Ubuntu 18.04

Now you can see the temperature of the GPU and other useful information.

Step 9: Install cuDNN 7.1.4:

NVIDIA cuDNN is a GPU-accelerated library for deep neural networks.

Go to https://developer.nvidia.com/cudnn and log in to download.

After logging in and accepting the agreement, download the following:

cuDNN v7.1.4 library for Linux.

Go to the downloaded folder and execute the following in the terminal:

tar -xf cudnn-9.2-linux-x64-v7.1.tgz
sudo cp -R cuda/include/* /usr/local/cuda-9.2/include
sudo cp -R cuda/lib64/* /usr/local/cuda-9.2/lib64

Step 10: Install NCCL 2.2.13:

NVIDIA Collective Communication Library (NCCL) implements multi-GPU and multi-node collective communication primitives optimized for NVIDIA GPUs.

Visit https://developer.nvidia.com/nccl, fill out the survey, and then download Nvidia NCCL.

After completing the survey, download the following:

Download NCCL v2.2.13, for CUDA 9.2 -> NCCL 2.2.13 O/S agnostic and CUDA 9.2

Go to the downloaded folder and execute the following in the terminal:

tar -xf nccl_2.2.13-1+cuda9.2_x86_64.txz
cd nccl_2.2.13-1+cuda9.2_x86_64
sudo cp -R * /usr/local/cuda-9.2/targets/x86_64-linux/
sudo ldconfig

Step 11: Install Dependencies

libcupti

sudo apt-get install libcupti-dev
echo 'export LD_LIBRARY_PATH=/usr/local/cuda/extras/CUPTI/lib64:$LD_LIBRARY_PATH' >> ~/.bashrc

Python Related:

To install these packages for Python 2.7, use the following command:

sudo apt-get install python-numpy python-dev python-pip python-wheel

To install these packages for Python 3.n, use the following command:

sudo apt-get install python3-numpy python3-dev python3-pip python3-wheel

Step 12: Configure TensorFlow from Source:

Download Bazel:

cd ~/ 
wget https://github.com/bazelbuild/bazel/releases/download/0.14.0/bazel-0.14.0-installer-linux-x86_64.sh
chmod +x bazel-0.14.0-installer-linux-x86_64.sh
./bazel-0.14.0-installer-linux-x86_64.sh --user
echo 'export PATH="$PATH:$HOME/bin"' >> ~/.bashrc

Reload Environment Variables

source ~/.bashrc
sudo ldconfig

Start Building TensorFlow by Downloading the Latest TensorFlow 1.8.0:

cd ~/ 
git clone https://github.com/tensorflow/tensorflow.git
cd tensorflow
git pull
git checkout r1.8./configure

Specify the Python path:

Please specify the location of python. [Default is /usr/bin/python]: /usr/bin/python3

Press Enter twice.

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]: Y
Do you wish to build TensorFlow with Hadoop File System support? [Y/n]: Y
Do you wish to build TensorFlow with Amazon S3 File System support? [Y/n]: Y
Do you wish to build TensorFlow with Apache Kafka Platform support? [Y/n]: Y
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.2Please specify the location where CUDA 9.2 toolkit is installed. Refer to README.md for more details. [Default is /usr/local/cuda]: /usr/local/cuda-9.2Please specify the cuDNN version you want to use. [Leave empty to default to cuDNN 7.0]: 7.1.4Please specify the location where cuDNN 7 library is installed. Refer to README.md for more details. [Default is /usr/local/cuda-9.2]: /usr/local/cuda-9.2Do you wish to build TensorFlow with TensorRT support? [y/N]: NPlease specify the NCCL version you want to use. [Leave empty to default to NCCL 1.3]: 2.2Please specify the location where NCCL 2 library is installed. Refer to README.md for more details. [Default is /usr/local/cuda-9.2]: /usr/local/cuda-9.2/targets/x86_64-linux

Now you need to note the GPU compute capability, which we noted in Step 1, for example 5.0.

Please note that each additional compute capability significantly increases your build time and binary size. [Default is: 5.0] 5.0Do you want to use clang as CUDA compiler? [y/N]: NPlease specify which gcc should be used by nvcc as the host compiler. [Default is /usr/bin/gcc]: /usr/bin/gccDo you wish to build TensorFlow with MPI support? [y/N]: NPlease specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -march=native]: -march=nativeWould you like to interactively configure ./WORKSPACE for Android builds? [y/N]:N

Configuration complete!

Step 13: Build TensorFlow Using Bazel

The next step in the process of installing the TensorFlow GPU version will be to build TensorFlow using Bazel. This process takes quite a long time! Be prepared!

To build the pip package for TensorFlow, you typically call the following command:

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

Note: If you receive an error about unsupported platforms, make sure you are running the correct pip command associated with the Python you configured TensorFlow to build with.

add "--config=mkl" if you want Intel MKL support for newer intel cpu for faster training on cpu
add "--config=monolithic" if you want static monolithic build (try this if build failed)
add "--local_resources 2048,.5,1.0" if your PC has low ram causing Segmentation fault or other related errors

This process will take a long time, possibly 1-2 hours or longer. For instance, on my i5-4590, it took 2 hours and 3 minutes. There’s no way but to wait!

Additionally, if you encounter errors like Segmentation Fault, trying again usually works.

The bazel build command builds a script named build_pip_package. Running this script as shown below will generate a .whl file inside the tensorflow_pkg directory:

To build the whl file, execute the following command:

bazel-bin/tensorflow/tools/pip_package/build_pip_package tensorflow_pkg

Install TensorFlow using pip:

cd tensorflow_pkg

For existing virtual environment:

pip install tensorflow*.whl

Create a new virtual environment using virtualenv:

sudo apt-get install virtualenv
virtualenv tf_1.8.0_cuda9.2 -p /usr/bin/python3
source tf_1.8.0_cuda9.2/bin/activate
pip install tensorflow*.whl

For Python 2: (Use sudo if necessary)

pip2 install tensorflow*.whl

For Python 3: (Use sudo if necessary)

pip3 install tensorflow*.whl

Note: If you encounter errors like unsupported platforms, make sure you are running the correct pip command associated with the Python you configured TensorFlow to build with.

You can check the pip version and associated Python with the following command:

pip -V

Step 14: Verify TensorFlow Installation

Run the following in the terminal:

python
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))

If the system outputs the following, you are ready to start writing TensorFlow programs:

Hello, TensorFlow!

Success! You have now successfully installed TensorFlow 1.8.0 on your machine.

Compiling and Installing GPU-Supported TensorFlow 1.8.0 from Source on Ubuntu 18.04

Click the original text to view the web version.

Welcome to visit www.52cv.net for more CVML technical information.

Leave a Comment