Install TensorFlow using Python (pip) or Docker Container
TensorFlow is a machine learning platform by Google. It is open source and has a large number of tools, libraries and other resources developed by the developer community as well as Google and other companies.
TensorFlow is available for all popular operating systems used, i.e. Windows, Mac OS, GNU / Linux. It can be downloaded and installed from one of the Python Package Indexes using the pip tool and can run in a virtual python environment. Another way to use it is to install it as a Docker container.
Install TensorFlow using pip
pip is the official package management utility for Python packages. Python and pip are not installed on CentOS by default.
To install the package, run:
sudo dnf install python3
Every time the installation asks for a download confirmation, etc., Enter Y and then press the Enter key to continue the setup. The python3 package will install Python 3 and also Pip 3.
It is recommended to run TensorFlow in a Python virtual environment. Virtual environments allow users to run multiple Python environments, with different versions of the required package, isolated from each other, on the same computer. This is to ensure that development is carried out in a virtual environment with a specific version of the package that does not affect development in other environments.
To run a Python virtual environment, we need to use the venv module. First, create and open your TensorFlow project directory.
mkdir dev/tf cd dev/tf
To create a virtual environment in this directory, run:
python3 -m venv tf_venv
This will create a new tf_venv directory which is a Python virtual environment. It contains the minimum files required, i.e. Python executable files, Pip executable files and some other libraries needed.
To start the virtual environment, run:
This will change the name of the prompt to tf_venv, e.g., the name of the virtual environment folder.
Now we will install TensorFlow in this virtual environment. For TensorFlow, the minimum pip version required is 19. To upgrade to the latest pip version, run:
pip install --upgrade pip
As seen above, version 20.0.2 of pip has been installed.
Install the TensorFlow package in a similar way.
pip install --upgrade tensorflow
This package is quite large (~ 420MB) and may require time to download and install along with the dependencies.
Once installed, we can verify the TensorFlow installation with a small piece of code to check the TensorFlow version.
python -c 'import tensorflow as tf; print(tf.__version__)'
To exit the virtual environment, run:
Install TensorFlow using Docker Container
Docker is now an established way to install and run programs in a virtualized environment called a Container. This is in a way similar to the Python virtual environment that we saw in the previous method. However, Docker has broader scope, and the Docker container is fully isolated and has its own configuration, software bundles, and libraries. Containers can communicate with each other through channels.
We can install and run TensorFlow through the Docker container and run it in a virtualized environment. The TensorFlow developer maintains a Docker Container image that is tested with each release.
First of all, we need to install Docker on our CentOS system. For this, see the official Docker installation guide for CentOS.
Next, to download the latest container image for TensorFlow, run:
docker pull tensorflow/tensorflow
If your system has a dedicated Graphics Processing Unit (GPU), you can download the latest container images with GPU support using the commands below.
docker pull tensorflow/tensorflow:latest-gpu-jupyter
Your system must have appropriate drivers for the GPU installed so that GPU capabilities can be used by TensorFlow. For more information on GPU support for TensorFlow, check the documentation in the Github repository.
To run TensorFlow in the Docker container, run:
docker run -it --rm tensorflow/tensorflow python -c "import tensorflow as tf; print(tf.__version__)"
First of all, let’s try to decipher what each part of the commandment means.
run is a docker order to start a container. The -it flag is provided when we want to start an interactive shell (Eg Bash, Python). – rm flag, called Clean Up, is determined so that the file and log system created internally by Docker to run the container is destroyed when the container exits. This flag should not be used if logs are needed in the future for debugging purposes. But for small foreground runs like ours, this can be used.
In the next section, we specify the name of our Docker container image, e.g., Tensorflow / tensorflow. Following that is the program / command / utility that we want to run in the container. For our tests, we invoked the Python interpreter in the container and passed it the code that printed the TensorFlow version.
We can see that Docker is printing some logs when starting the container. After the container is started, our Python code runs and a Tensor Flow version is printed (2.1.0).
We can also start the Python interpreter as a shell, so we can continue running a few lines of TensorFlow code.
In this article, we see two methods for installing TensorFlow on CentOS. Both methods are intended to run TensorFlow in a virtualized environment, which is the recommended approach when using TensorFlow.
If you are a beginner at TensorFlow, you can start with the basics of the official TensorFlow tutorial.