TensorFlow is an open-source machine learning library written in Python and built by Google. Many well-known organizations are using TensorFlow including Paypal, Lenovo, Intel, Twitter, and Airbus. You can install it using Anaconda, as a docker container or in a Python virtual environment. A virtual environment allows users to have different python environments on a single system and they can install a particular module version based on per-project requirements, without affecting the other projects.
This article will show you how to install the TensorFlow library in a Python virtual environment using the command line application on Ubuntu 20.04 LTS and Ubuntu 21.04.
Installation of TensorFlow
Open the terminal then install the pre-requisites that i will show them for you
Install Python and pip
sudo apt install -y python3 sudo apt install -y pip
Check Python installation
Python 3.9 comes pre-installed on Ubuntu 21.04 system by default. Therefore, you can easily verify the installation of Python 3 on your system by using the following command:
$ python3 -V $ pip3 -V
The following output should display on the terminal:
Install Python Virtualenv
Recommended that using the venv module, you have to create a virtual environment that you can find it in the python3-venv package. The following command you can use to install the required packages of python3-venv:
$ sudo apt install python3-venv python3-dev
Setup virtual environment
After installing the venv module, you need to set up a virtual environment for the TensorFlow.
Now, navigate into the directory where you want to store the Python 3 virtual environment. You can store it into your home directly or as well in any other directory where you have read and write permissions.
Create a directory by using the following mkdir command for the TensorFlow project and the move into it using the cd command as follows:
$ mkdir my_tensorflow $ cd my_tensorflow
Using the following command you can create a python virtual environment in the current directory:
$ python3 -m venv venv
In the above command, the second word venv is the name of your new virtual environment. Therefore, you can give any name for the virtual environment.
I have created a new directory named venv that contains the python standard library, the copy of the Python binary, the package manager of Pip, and all other supporting files.
Activate the virtual environment ‘venv’ by executing the below mentioned activate script:
$ source venv/bin/activate
Once the environment activated, you will see that at the beginning of the system
$PATH variable the bin directory of the virtual environment will be added. You will notice that the shell’s prompt name is changed now. The name of the currently using virtual environment will show on the shell’s prompt. Here, the name of the virtual environment in which we are currently working is ‘venv’.
To install the TensorFlow, it’s required to first install the pip version 20 or more latest. The following command you can use to upgrade pip from previous to the latest version: (venv)
$ pip install --upgrade pip
Installation of TensorFlow on Ubuntu 21.04
Once the virtual environment is activated, it’s time to start the installation of TensorFlow on your system. Type the following command to install the TensorFlow packages:
(venv) $ pip install --upgrade tensorflow
The TensorFlow successfully installed on your system.
To verify the installation of TensorFlow, execute the following command that will display the installed version of TensorFlow on the terminal:
$ python -c 'import tensorflow as tf; print(tf. version )'
Many examples and TensorFlow models are available on the Github repositories.
Once you completed your work, using the following command you can deactivate it:
So We have explained how to install TensorFlow on Ubuntu 21.04 system by using the terminal. TensorFlow is a library usedto solve different machine learning problems.