How to Install TensorFlow Quickly and Easily

Free TensorFlow course with real-time projects Start Now!!

In our last TensorFlow Tutorial, we discussed Tensorflow API. Today we will see how to install TensorFlow.  While looking at the TensorFlow installation in your system we will also learn how to install Tensorflow with Anaconda.

Moreover, we will see the process to install TensorFlow Virtual environment, Pip, Docker. Also, we will discuss TensorFlow installation process with GPU & CPU.

So let’s understand how to install TensorFlow.

TensorFlow Installation With CPU and GPU

Before getting started with Tensorflow Installation it is important to note that TensorFLow has been tested in 64-bit versions and with Ubuntu 16.04 or higher.

Before you install TensorFlow in your system, it is important to know which kind of installation is best suited for your need. You should choose between the following types:

Install Tensorflow With CPU support only: This usually takes less time than a GPU supported version and this type is recommended if you do not have an NVIDIA GPU in your computer system

Install Tensorflow With GPU support: It takes a little more time but the processing is faster than a CPU, and the requirements should be met as listed below, the following programs must be installed on your system before you install Tensorflow with GPU Support:

  • CUDA® Toolkit 9.0
  • The NVIDIA drivers associated with CUDA Toolkit 9.0.
  • cuDNN v7.0
  • GPU card with CUDA Compute Capability 3.0 or higher.

Methods to Install TensorFlow

In Ubuntu, it is recommended to first install pip in your system if not already installed, as it is one of the things that’ll be required quite often while working with Python. For installing pip, use the following commands:

# for Python 2.7

$ sudo apt-get install python-pip python-dev

# for Python 3.n

$ sudo apt-get install python3-pip python3-dev

You can then proceed with Tensorflow installation using the following methods:

  • Install in Virtual Environment like Virtual Box.
  • Using python package installer pip.
  • Using container software like Docker.
  • Using Anaconda
  • Installing from other sources.

a. Using Virtual Environment

This is a suggested method since it is incapable of interfering with or affecting the other Python programs on the same machine. You need to enable a virtual environment in your BIOS for using it.

Create a virtual environment for TensorFlow Installation:
# for Python 2.7

$ virtualenv --system-site-packages targetDirectory

# for Python 3.n

$ virtualenv --system-site-packages -p python3 targetDirectory

where targetDirectory specifies the top of the Virtualenv tree. Our instructions assume that targetDirectory is ~/TensorFlow, but you may choose any directory. Then activate the environment using.

$ source ~/tensorflow/bin/activate # bash, sh, ksh, or zsh
$ source ~/tensorflow/bin/activate.csh  # csh or tcsh

The source  command now changes to :(tensorflow)$
Now issue one of the following commands in the active virtual environment :
# for Python 2.7

(tensorflow)$ pip install --upgrade tensorflow

# for Python 3.n

(tensorflow)$ pip3 install --upgrade tensorflow

# for Python 2.7 and GPU

(tensorflow)$ pip install --upgrade tensorflow-gpu

# for Python 3.n and GPU

(tensorflow)$ pip3 install --upgrade tensorflow-gpu

b. Using pip

The native pip install TensorFlow directly into your system, without going through any container system. Plus, it is an easy installation. Use the following commands for the installation:

$ pip install tensorflow      # Python 2.7; CPU support (no GPU support)
$ pip3 install tensorflow     # Python 3.n; CPU support (no GPU support)
$ pip install tensorflow-gpu  # Python 2.7;  GPU support
$ pip3 install tensorflow-gpu # Python 3.n; GPU support

If the above step fails, try upgrading the binary files using :

$ sudo pip  install --upgrade tfBinaryURL   # Python 2.7
$ sudo pip3 install --upgrade tfBinaryURL   # Python 3.n

c.  Install TensorFlow With Docker

It isolates the library installation from pre-existing packages on your machine. The only downside is that the Docker image can be a bit large approx to 100s of Mbs. You have to first install Docker and initialize the container.

To install a version of TensorFlow that supports GPUs, you must first install Nvidia-docker, which is stored in GitHub, then, launch a Docker container that contains one of the TensorFlow binary images. With CPU support only do :

$ docker run -it -p hostPort:containerPort TensorFlowCPUImage

Where -p hostPort:containerPort is optional.
TensorFlowCPUImage is required. Specify one of the following values:

  • tensorflow/tensorflow, which is the TensorFlow CPU binary image.
  • tensorflow/tensorflow:latest-devel, which is the latest TensorFlow CPU Binary image plus source code.
  • tensorflow/tensorflow:version, which is the specified version (for example, 1.1.0rc1) of TensorFlow CPU binary image.
  • tensorflow/tensorflow:version-devel, which is the specified version (for example, 1.1.0rc1) of the TensorFlow GPU binary image plus source code.

For example,

$ docker run -it tensorflow/tensorflow bash

You can Jupyter Notebook too,

$ docker run -it -p 8888:8888 tensorflow/tensorflow

d. Installing TensorFlow Using Anaconda

You may use conda to create a virtual environment, and then using either conda install or pip install for installing TensorFlow. First, you need to download and install Anaconda in your system.

$ conda create -n tensorflow pip python=2.7 # or python=3.3, etc.

Creates a conda environment named tensorflow to run a version of Python. Activating the conda environment using,

$ source activate tensorflow
(tensorflow)$

# Your prompt should change
Then, install TensorFlow using :

(tensorflow)$ pip install --ignore-installed --upgrade tfBinaryURL

For example, the following command installs the CPU-only version of TensorFlow for Python 3.4:

(tensorflow)$ pip install --ignore-installed --upgrade \
https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.7.0-cp34-cp34m-linux_x86_64.whl

Now test your installation by typing out the following commands :
# Python

import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))

You should see the following output if everything is done correctly,

Hello, TensorFlow!
Installing Tensorflow on Windows,

  • Using ‘native’ pip
  • Using Anaconda

e. Using Pip to Install TensorFlow

The above methods described earlier are the same for windows as well with a few minor changes.
First, start a terminal, then type :

C:\> pip3 install --upgrade tensorflow

And for GPU support add “-gpu” suffix with tensorflow. That’s it.

f. Again Using Anaconda

Again, create a conda environment as follows:

C:> conda create -n tensorflow pip python=3.5

Then, activate the environment using:

C:> activate tensorflow
(tensorflow)C:>  # Your prompt should change

Now, issue the command for TensorFlow installation as described below:

(tensorflow)C:> pip install --ignore-installed --upgrade tensorflow

Now, it’s time to test the installation using :
First start python in the terminal, then, type the following,

>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
>>> print(sess.run(hello))

If the output is as given below, then, congratulations on your first TensorFlow code!
Hello, TensorFlow!

Install TensorFlow in MacOS

The methods are followed in the installation of TensorFlow:

  • Virtualenv
  • “native” pip
  • Docker

The TensorFlow installation with docker and Virtualenv remain the same as described previously.
Using pip, first install it :

$ sudo easy_install --upgrade pip
$ sudo easy_install --upgrade six

Now, to install TensorFlow
# Python 2.7; CPU support

$ pip install tensorflow

# Python 3.n; CPU support

$ pip3 install tensorflow

That’s it, pretty easy, right!
Now, validate your installation by using the same previous command,
# Python

import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))

And you should see the following output:
Hello, TensorFlow!
So, this was all in how to Install TensorFlow. Hope you like our explanation of Installing TensorFlow.

Conclusion

Hence, we saw how to install Tensorflow by importing the libraries and dependencies using various methods on different systems. Moreover, we discussed the TensorFlow installation with GPU & CPU.

Also, we saw install TensorFlow using Pip, Anaconda & Virtual environment. Next up, we’ll be making clusters in Tensorflow. Still, if any doubt, ask in the comment tab.

If you are Happy with DataFlair, do not forget to make us happy with your positive feedback on Google

follow dataflair on YouTube

1 Response

  1. Rakesh says:

    After run of this command (pip install tensorflow) i got this error
    termcolor, grpcio, pbr, funcsigs, mock, tensorflow
    The script markdown_py is installed in ‘/home/rakesh/.local/bin’ which is not on PATH.
    Consider adding this directory to PATH or, if you prefer to suppress this warning, use –no-warn-script-location.
    The scripts conv-template, f2py and from-template are installed in ‘/home/rakesh/.local/bin’ which is not on PATH.
    Consider adding this directory to PATH or, if you prefer to suppress this warning, use –no-warn-script-location.
    The script tensorboard is installed in ‘/home/rakesh/.local/bin’ which is not on PATH.
    Consider adding this directory to PATH or, if you prefer to suppress this warning, use –no-warn-script-location.
    The script pbr is installed in ‘/home/rakesh/.local/bin’ which is not on PATH.
    Consider adding this directory to PATH or, if you prefer to suppress this warning, use –no-warn-script-location.
    The scripts freeze_graph, saved_model_cli, tensorboard, tflite_convert, toco and toco_from_protos are installed in ‘/home/rakesh/.local/bin’ which is not on PATH.
    Consider adding this directory to PATH or, if you prefer to suppress this warning, use –no-warn-script-location.
    Successfully installed absl-py-0.3.0 astor-0.7.1 backports.weakref-1.0.post1 enum34-1.1.6 funcsigs-1.0.2 futures-3.2.0 gast-0.2.0 grpcio-1.13.0 markdown-2.6.11 mock-2.0.0 numpy-1.15.0 pbr-4.2.0 protobuf-3.6.0 six-1.11.0 tensorboard-1.9.0 tensorflow-1.9.0 termcolor-1.1.0 werkzeug-0.14.1
    rakesh@rakesh-HP-15-Notebook-PC:~$ python2
    Python 2.7.12 (default, Dec 4 2017, 14:50:18)
    [GCC 5.4.0 20160609] on linux2
    Type “help”, “copyright”, “credits” or “license” for more information.
    >>> import tensorflow
    Illegal instruction (core dumped)

Leave a Reply

Your email address will not be published. Required fields are marked *