Skip to content

Instantly share code, notes, and snippets.

@AbhilashG97
Last active July 18, 2020 18:55
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 AbhilashG97/bbcb7dc0beae25d59df01a76c6f1c390 to your computer and use it in GitHub Desktop.
Save AbhilashG97/bbcb7dc0beae25d59df01a76c6f1c390 to your computer and use it in GitHub Desktop.
Installing Tensorflow-2.0 on Ubuntu 19.04 with RTX 2070 Max Q GPU

Building and Installing TensorFlow-2.0 for Ubuntu-19

There are a few guides which tell how to install TensorFlow for RTX GPUs, but I found out that they were not complete.

This blog is pretty good, but it does not include all the steps. The missing steps will be included here.

Check Compatible Versions

You check the compatabilty of different versions here. This link is for Windows.

Note: The below two points are to be used only when you want install tensorflow using pip.

  1. Have a look here to set up path after installation.

  2. After installing cudaNN, place the cuda dll file from the extracted folder at -

C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\bin

This blog shows how to install a specific version of gcc in Ubuntu.

Installing CUDA and cuDNN

💥 Please ensure that your system is able to detect the GPU before proceeding with the below mentioned steps. The blog mentioned above tells you how to do that.

Get the latest CUDA version from here.

I chose CUDA-10.2

Execute the commands mentioned in the website to install CUDA-10.2.

The files are large, so it may take some time to download the files depending on our internet speed. While installing CUDA-10.2 make sure to INSTALL the nvidia driver suggested by the CUDA-10.2 installer.

After CUDA-10.2 gets installed, execute these commands -

export PATH=$PATH:/usr/local/cuda-10.2/bin
export LD_LIBRARY_PATH=/usr/local/cuda-10.2/lib64
echo 'PATH=$PATH:/usr/local/cuda-10.2/bin' >> ~/.bash_profile
echo 'LD_LIBRARY_PATH=/usr/local/cuda-10.2/lib64' >> ~/.bash_profile

Verfiy the installation by executing -

nvcc -V

You should see the CUDA version that is installed in your system at the last line of the output.

Now, you have to install cuDNN runtime and developer library. You can get it from here. Make sure you choose the correct cuDNN version. In my case it would be V7.6.5

After downloading the two files, install them by executing -

sudo dpkg -i libcudnn7_7.6.5.32-1+cuda10.2_amd64.deb
sudo dpkg -i libcudnn7-dev_7.6.5.32-1+cuda10.2_amd64.deb

Now run the below mentioned code snippet in the terminal to check if everything is working fine -

function lib_installed() { /sbin/ldconfig -N -v $(sed 's/:/ /' <<< $LD_LIBRARY_PATH) 2>/dev/null | grep $1; }
function check() { lib_installed $1 && echo "$1 is installed" || echo "ERROR: $1 is NOT installed"; }
check libcudnn

Installing Bazel

The Bazel ppa's have been removed from Ubuntu 19.04's repository and hence they have to be downloaded from their GitHub repo.

The answer present in this post is pretty helpful in installing Bazel.

Follow these steps to install Bazel -

  1. Execute the below mentioned command to install the pre-requisites for running Bazel

sudo apt-get install pkg-config zip g++ zlib1g-dev unzip python

  1. Go here and download the bazel-0.26.0-installer-linux-x86_64.sh file. When writing this gist, the 0.26.0 version of Bazel seems to work well when building Tensorflow. The latest version of Bazel seems to have compatibility issues.

  2. Run the installer by executing the below mentioned commands -

chmod +x bazel-0.26.0-installer-linux-x86_64.sh
./bazel-0.26.0-installer-linux-x86_64.sh --user
  1. Run the below mentioned command to add Bazel to PATH
export PATH="$PATH:$HOME/bin"

Building Tensorflow

Before building ensure that these dependencies are installed in your system -

For Python2.7

  1. pip install mock
  2. pip install keras_applications==1.0.4 --no-deps
  3. pip install keras_preprocessing==1.0.2 --no-deps
  4. pip install h5py==2.8.0
  5. pip install future

If you're buidling TensorFlow for CUDA-10.2, you'll have to modify the third_party/nccl/build_defs.bzl.tpl file. You'll have to comment out the 116th line in that file. Your file should look somewhat like this.

After performing the above steps proceed with the below mentioned steps -

git clone https://github.com/tensorflow/tensorflow.git
cd tensorflow
git checkout r2.0

Now execute the following the commands -

bazel clean --expunge
./configure
bazel build -s //tensorflow/core:version_info_gen

⚠️ The above commands are to be executed from the root directory of the project.

⚠️ You should now see folders like - bazel-bin, bazel-genfiles etc. It is important that you see these folders in your root project directory.

When you type ./configure, you'll be presented with a set of configurations that you can have for your TensorFlow build. Please ensure that you select Y for the following options -

XLA JIT support: Yes
CUDA support: Yes
CUDA SDK version: 10.2
NCCL version: 1.3

⚠️ You may have to change the default python version if you wish to build TensorFlow for Python3.

Finally, execute the below mentioned command to build TensorFlow-GPU -

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

You can find the other build options here.

This might 1-2 hours.

Now execute the following commands -

sudo bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg/
sudo pip install --upgrade /tmp/tensorflow_pkg/tensorflow-1.0.0-cp27-cp27mu-linux_x86_64.whl

⚠️ You may see a different .whl file depending on your TensorFlow version selection.

Hopefully after followig these steps, you should have Tensorflow running on your system. 🍉

Testing TensorFlow 2.0

If you see any errors while running the below mentioned code snippet in the REPL Python shell, you may not have successfully installed Tensorflow.

import tensorflow as tf

assert tf.test.is_gpu_available()
assert tf.test.is_built_with_cuda()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment