How To Install Python TensorFlow On Centos 8

Make sure you have Python 3.5+ installed on your system.

In [ ]:
python --version
Python 3.6.8

Let us first update the system

sudo yum update

Install Tensorflow With Gpu

Install Cuda

In [ ]:
sudo rpm -i cuda-repo-rhel8-11-0-local-11.0.3_450.51.06-1.x86_64.rpm    
sudo yum -y install cuda

Depending upon where the libraries get installed, you might run in to following error, when you import tensorflow.

Could not load dynamic library '';

Do following...

cp -p /usr/lib64/ /usr/lib/

Install libcudart

In [ ]:
sudo rpm -i nvidia-cuda-toolkit-11.2.0-8.mga8.nonfree.x86_64.rpm
sudo yum  install nvidia-cuda-toolkit

Now let us install tensorflow using pip.

In [ ]:
pip install tensorflow

Test Tensorflow

Launch Ipython or Python shell and try out these commands.

In [ ]:
In [1]: import tensorflow as tf
2021-07-03 21:00:42.953020: I tensorflow/stream_executor/platform/default/] Successfully opened dynamic library

In [2]: with tf.compat.v1.Session() as sess:
   ...:         hello = tf.constant('hello world')
   ...:         print(
2021-07-03 21:00:48.919579: I tensorflow/stream_executor/platform/default/] Successfully opened dynamic library
2021-07-03 21:00:48.934110: E tensorflow/stream_executor/cuda/] failed call to cuInit: CUDA_ERROR_NO_DEVICE: no CUDA-capable device is detected
2021-07-03 21:00:48.934146: I tensorflow/stream_executor/cuda/] kernel driver does not appear to be running on this host ( /proc/driver/nvidia/version does not exist
2021-07-03 21:00:48.936834: I tensorflow/core/platform/profile_utils/] CPU Frequency: 3700210000 Hz
b'hello world'

If you notice above, there is an error "CUDA_ERROR_NO_DEVICE: no CUDA-capable device is detected". I am getting this error because machine has no GPU.

To avoid the above error, make sure you do the following setting.

In [ ]:
import os
os.environ['CUDA_VISIBLE_DEVICES'] = "0"

Common errors

You might run in to following Tensorflow error while importing "Keras"

Keras requires TensorFlow 2.2 or higher. Install TensorFlow via pip install tensorflow?

Make sure you have Python 3.5+ installed. Then follow the above instructions to install Tensorflow.