TensorFlow is an important open-source library for machine learning that is built by Google. It can run on the GPU as well as on the CPU of different devices. TensorFlow is used by many organizations, including PayPal, Intel, Twitter, Lenovo, and Airbus. It can be installed as a Docker container, or in a virtual environment of Python, or with Anaconda.
In this article, you will learn how to install the popular python machine learning library TensorFlow on CentOS 8 using a python virtual environment.
Installation of TensorFlow on CentOS 8
TensorFlow provides compatibility with both Python 2 and Python 3. In this article, we will use Python 3 and inside the virtual environment, we will install TensorFlow. Using a virtual environment, you can make multiple isolated Python environments on a single system and install a particular version of the module on project requirements without affecting your other python projects.
To install TensorFlow on CentOS 8, we will need to perform the following steps:
Open the terminal window through the shortcut method ‘Ctrl + Alt + t’. Or open it by clicking on Activities and select terminal from the left sidebar of the desktop.
Login as root user (or login as administrative user and use sudo -s) to install the required packages for TensorFlow on your system.
Python is not installed by default on CentOS 8. Install Python 3 using the following command on the terminal:
$ sudo dnf install python3
The above-mentioned command will install python 3.6 and pip3 on your system. It is already installed on my system as you see in the screenshot. You can run python by typing python 3 on the terminal explicitly.
Note: To start with python 3, it is recommended to create a virtual environment to use ‘venv’ module.
Now, you will navigate to a directory where you want to store TensorFlow projects. You can store in your home directory or other where you have completely read and write permissions. Create a new directory and name it as ‘tensorflow_project’ for the TensorFlow project and then switch in this directory. Use the following command to perform these actions:
$ mkdir tensorflow_project
$ cd tensorflow_project
Now you will create a virtual environment. Use the following command to create a virtual environment within the ‘tensor_flow’ directory:
$ python3 -m venv venv
The above-given command creates a directory named ‘venv’ that keeps a copy of the binary python, python standard library pip, and other supportive files. You can assign any name that you want for the virtual environment.
Use the following command to activate the virtual environment:
$ source venv/bin/activate
Once the virtual environment is activated, a bin directory will add at the beginning of the path, and the prompt of the terminal will change that will show currently using the name of the virtual environment. Here, we are using the name ‘venv’.
The Tensorflow supports the version of pip 19 or higher. You need to upgrade the pip to the latest version. You will execute the following command on the terminal to upgrade the pip:
(venv) $ pip install --upgrade pip
After activation of the virtual environment, you will install the TensorFlow library by executing the following command:
(venv) $ pip install --upgrade tensorflow
You can verify the installation using the following command that will print the version of TensorFlow:
(venv) $ python -c 'import tensorflow as tf; print(tf.__version__)'
After executing this command, the version of TensorFlow will be displayed on the terminal.
Once you have finished your work, you will deactivate the environment and return to the normal working shell. Use the following command on the terminal to deactivate virtual environment:
(venv) $ deactivate
Now, have been returned to your normal shell and continue your work.
If you did not use TensorFlow before, then you will visit the basic TensorFlow page and learn how to work on machine learning applications. You can also run the clone models of TensorFlow or examples from Github repositories to test on your system.
In this article, you learned how to install the TensorFlow library on CentOS 8. Moreover, you have also learned how to create and deactivate a virtual environment in python using the terminal. I hope you enjoyed this tutorial and would help you.