How to install and use TensorFlow in Ubuntu?


TensorFlow is a software library for creating machine learning models for web, mobile, desktop, or cloud. It makes the process of acquiring data, training models, making predictions, and refining future results easier.

The TensorFlow was created by the Google Brain team and it can be used with different languages including Python, JavaScript, C++, and Java.

In this article, I will discuss how to create a programming environment by installing TensorFlow on Ubuntu. You will also see a basic example where we use the TensorFlow library.

Prerequisites

To follow this guide you should have the following –

  • Access to a user account that has superuser privileges
  • Python 3 should be installed on your Ubuntu system

How to install TensorFlow in Ubuntu

Follow the given steps to install and use TensorFlow on a Ubuntu system.

Installing Python, Python pip, and Python venv packages

Before you install TensorFlow, first you need to install Python and Python pip on your Ubuntu system.

Use the following command to install the latest version of Python available in the Ubuntu repository –

sudo apt update
sudo apt install python3 -y

Next, use the following command to install Python pip

sudo apt install python3-pip -y

You also need to install the python-venv package to create Python virtual environment.

The advantage of using virtual environments is that they provide isolation which means no environment interacts with another each of them works as an independent unit and its unusual changes do not damage other projects.

Use the following command to install python-venv package –

sudo apt install python3-venv -y

Installing TensorFlow in Python virtual environment on Ubuntu

First, create a project directory let’s say it is tf-project, make the directory with a meaningful name.

mkdir tf-project

Move to the created directory by using –

cd tf-project

Next, use the given command to create a python virtual environment

python3 -m venv tf-dev

Activate the virtual environment by using –

source tf-dev/bin/activate

Now your prompt will change to a virtual environment as you can see in the image below.

Next, use the given command to install TensorFlow –

pip install --upgrade tensorflow

Once the installation is successful you can verify it by using  –

python -c "import tensorflow as tf;print(tf.reduce_sum(tf.random.normal([1000, 1000])))"

If you want you can deactivate the virtual environment by using –

deactivate

Installing TensorFlow in a Ubuntu system

If you want to install it on your Ubuntu system, not on the virtual environment that you have created, use the following command in your terminal –

pip3 install --user --upgrade tensorflow 

Once it gets installed, you can verify the installation by using the given command –

python3 -c "import tensorflow as tf; print(tf.reduce_sum(tf.random.normal([1000, 1000])))"

If it displays no error that means TensorFlow has been set up successfully on your system.

Now if you have a query then write us in the comments below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.