Fresh Reinstall of TensorFlow with CUDA

I already have an existing TensorFlow installation with CUDA toolkit on Ubuntu 16.04 LTS. For a fresh reinstall of the latest TensorFlow with all required dependencies, I first removed TensorFlow and CUDA toolkit (including NVIDIA driver).

To uninstall TensorFlow, use:
$ source activate tfgpu
$ python3 -m pip uninstall protobuf
$ python3 -m pip uninstall tensorflow-gpu
$ python3 -m pip uninstall tensorflow

The following command will remove everything from NVIDIA, including the GPU driver, CUDA, and cuDNN.
$ sudo apt-get purge nvidia*

After that, install the latest NVIDIA driver.
$ sudo apt install nvidia-VERSION-NUMBER

Decide on the version of TensorFlow you want to install, and the CUDA and cuDNN requirements for that version. Download CUDA 10.0 from NVIDIA wesbite and install it.
$ sudo dpkg -i cuda-repo-ubuntu1604-`VERSION-NUMBER`-1_amd64.deb
$ sudo apt-key add /var/cuda-repo-`VERSION-NUMBER`/
$ sudo apt-get update
$ sudo apt-get install cuda

Now, download cuDNN from NVIDIA developer website. You would need to register an account if you don’t have one.
$ sudo dpkg -i libcudnn7_`VERSION_NUMBER`_amd64.deb
$ sudo dpkg -i libcudnn7-dev_`VERSION-NUMBER`_amd64.deb
$ sudo dpkg -i libcudnn7-doc_`VERSION_NUMBER`_amd64.deb

After installing CUDA and cuDNN, you would need to set the environment variable. Otherwise, you might encounter an error like this when importing TensorFlow in Python – "ImportError: cannot open shared object file: No such file or directory".

You can also go for the easier option of installing through Anaconda in this, instead of the above two.
$ conda install cuda
$ conda install cudatoolkit

Reboot your system and check if the installation was correct.
$ nvidia-smi

Now, install TensorFlow using pip.
$ python3 -m pip install tensorflow-gpu

Check if the TensorFlow is working correctly.
$ source activate tfgpu
$ python3
>>> import tensorflow as tf
>>> tf.VERSION #or tf.__version__
>>> sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))

Write a Comment