Analytics TensorFlow Ubuntu

Install and Use TensorFlow on Ubuntu

TensorFlow with CPU support

This mechanism takes less time (usually 5 to 10 minutes) during installation. If your system does not have NVIDIA GPU, then you have to install TensorFlow using this mechanism. It is recommendeded to use this mechanism even if you have NVIDIA GPU on your system. You should install TensorFlow flow using this mechanism first.

TensorFlow with GPU support

If your system has NVIDIA GPU, then you can use this mechanism. This mechanism is significantly faster as compared to on a CPU. If you need to run critical applications, then you should use this mechanism.

Installing TensorFlow on Ubuntu

There are four mechanisms to install TensorFlow on Ubuntu (Virtualenv, Native pip, Docker, or Anaconda).

Virtualenv provides a safe and reliable mechanism for installing and using TensorFlow. During virtualenv installation, it installs TensorFlow and all packages that are required for TensorFlow. All you need to do is to activate the virtualenv.  If you are aiming to provide system administrator services. To make TensorFlowlow available for everyone on a multi-user system then this mechanism is recommended. If you understand pip and your python environment, it takes only a single command.  Docker separates all pre-existing packages on the machine during installation. It contains TensorFlow and all data required for TensorFlow.  Click here for information on How to install Docker.  In anaconda to create a virtual environment we use conda.

Installation of TensorFlow with virtualenv

  1. Install pip and virtualenv depending on Python 2 or 3:
    $ sudo apt-get install python-pip python-dev python-virtualenv
    $ sudo apt-get install python3-pip python3-div python-virtualenv
  2. In the second step, we will create a virtual environment with the following commands shown for Python 2 and 3:
    $ virtualenv -–system-site-packages <Directory Name>
    $ virtualenv -–system-site-packages –p python3 <Directory Name>
  3. In the third step, we have to activate our virtual environment with the following commands:
    $ source ~/tensorflow/bin/activate
  4. In our fourth step we will install TensorFlow in the active virtuanenv, with different commands show for CPU, GPU and Python 3:
    $ Pip install –upgrade tensorflow
    $ Pip3 install –upgrade tensorflow
    $ Pip install –upgrade tensorflow-gpu
    $ Pip3 install –upgrade tensorflow-gpu

After installing TensorFlow we have to validate the installation process. For validation, make sure that TensorFlow is active, if it is not, use the above-mentioned activation commands to activate TensorFlow. If your command prompt looks like this, it means tensor flow is active:


To deactivate TensorFlow we will use

$ deactivate

For uninstalling TensorFlow we can use the command:

$ rm –r DirectoryName

Installation of TensorFlow with Docker

In this mechanism, we need to install Docker on our machine first. To install a version of TensorFlow that supports GPU, we first have to install Nvidia-docker.  Then we will launch the docker container. To launch Docker with CPU or GPU support the commands will be as following:

For CPU support only:

$ sudo docker run –it –p <hostport:containerport> <TensorFlow CPU binary image>

For GPU support we will use the command:

$ sudo nvidia-docker run –it –p <hostport:containerport> <TensorFlow GPU binary image>

Installation of TensorFlow with native pip

In the installation of TensorFlow with native pip we need to follow these steps. First we will install TensorFlow using following commands. For python 2.7 and 3, with CPU and GPU support respectively examples are shown:

$ pip install tensorflow
$ pip3 install tensorflow
$ pip install tensorflow-gpu
$ pip3 install tensorflow-gpu

Installation of TensorFlow with Anaconda

In this mechanism, we will download and install anaconda, then we can create a conda environment, using name TensorFlow with this command:

$ conda –n tensorflow

After that, we will activate our conda environment using command

$ source activate tensorflow

After activating conda environment, we will install TensorFlow inside our conda environment. Using following command;

$ pip install –ignore-installed –upgrade <UrlofTensorFlowPythonPackage>

Short TensorFlow Program

After our installation completed we need to check wether our TensorFlow is in running condition or not. For this, we will write a simple code. If the below code generates the result Hello World it mean our TensorFlow is working.

Import tensorflow as tf
h=tf.constant(‘Hello World’)

Basics of TensorFlow

In this session, we will do some sample programing to learn TensorFlow usage. First I think we should define TensorFlow.  TensorFlow is the numerical computation where data flows through graphs. To represent data in TensorFlow we use an n-dimensional array called tensor and mathematical operations.

In TensorFlow every operation or computation resides on a graph. Graphs are considered as a backbone of TensorFlow.
We can access a Graph by using the function.


Using this command can access graphs to analyze values and perform operations on specific data.
To get a list of all operations, we will use the function.


If we want to give a name to any of the operations in the graph, we can use the function as:

For oper in graph.get_operations();

Sessions are used to run the operations. We can create a session in TensorFlow using this piece of a code:

………code segment……………
………code segment……………
s.close() is used to close the session that we have started.

Tensors are used in TensorFlow to hold data. Tensors are Constants, Variables, etc.

Constants are elements whose values cannot be changed. We can declare constants in TensorFlow with this piece of a code:

<TF.Tensor’const:0’ shape() dtype=int32>
Tensor(’const:0’,  shape(),  dtype=int32)

Now this piece of a code is useless until we create a session to run it inside that. The session for this will be created as follow:

With tf.session as s;

Variables are declared in TensorFlow using this piece of a code:

<tensorflow.python.ops.variables.variable obj at 0x7f37ebda1990>

We can create a session for the above code to make it run inside that session using following piece of code:

With tf.session() as s;

As variables can hold different values so they need to be separately initialized by the init operation.
Now we will try to print operations on a graph using this piece of a code:

for op in graph.get_operations();

Linear Regression in TensorFlow

Before going for linear regression let’s discuss some basic TensorFlow functions that will be use in our linear regression code segment.

Random_normal Distribution, where x is a variable with size 720*20 with random value of standard deviation 0.09:

x=tf.variable(tf.random_normal([720, 20], stddev=0.09))

Calculating Mean of an Array:

x=tf.variable([1,2,3,4,5,6], name=’y’)
with tf.session as s;

Linear Regression:

As we know that in linear regression we have to fit a lot of data points in a single line. In our first step, we will be creating training data. Here we will take 100 data point, and we will try to fit them into a line.  To create training data, we will write this piece of code:

Import tensorflow as tf
Import numpy as np

In our next step, we will use placeholder so we can give value at runtime also. For placeholders we will write this piece of code:


Now we will create a linear regression model which is b_model=m*A, and we will calculate the value of m through our model. For this purpose the code will be written as follow:

m=tf.variable(0.0 name=”load”)
c=(tf.pow(B-b_model, 2))

Our next step will be training until we didn’t do a single computation. We just created graphs and nothing else. Now we will create a session to perform some operations on our graphs. For this purpose, we will create a session, but before that, we need to create init_op to initialize all variables.

For that purpose we will use this one line code.


for creating session we will use:

with tf.session as s;
for j in range(100);
for(a,b) in zip(trainX, trainY);, feed_dict={A: a, B: b})

More Information About TensorFlow

About the author



A passionate Linux user for personal and professional reasons, always exploring what is new in the world of Linux and sharing with my readers.