Hello everyone. This is going to be a tutorial on how to install tensorflow using official pre-built pip packages. To install tensorflow with pip packages is easier as compared to building using CMake or Bazel. Pre-built pip package are fully tested officially. However, since they are configured in such a way that they can support legacy hardware too, using pip package may not use full capability on your new and powerful hardware. Building pip package the solution to fully optimize tensorflow to use full capability of your hardware. However, building is a time-consuming process and generally recommended for advanced users only. If you are looking to build tensorflow instead, you can check out our other blog posts:
In this tutorial, we will look at how to install tensorflow CPU and GPU both for Ubuntu as well as Windows OS. For our purpose, we will look at installing the latest version tensorflow, tensorflow 1.7.0, at the time this blog is published. To install tensorflow in any OS, I highly recommended using virtual environment setup (conda, virtualenv etc.). Currently only 64-bit python is supported by Tensorflow.
We have also performed speed comparison on the tensorflow 1.5.0 with CUDA 9 and cuDNN 7.5 support with tensorflow 1.4.1 with CUDA 8 and cuDNN 6 to calculate just how faster the new version of tensorflow is in comparison. You can click on the link here to check that out.
There must be 64-bit python installed tensorflow does not work on 32-bit python installation.
For Ubuntu 16.04 64bit OS:
Install CPU Version of Tensorflow:
CPU version of tensorflow is recommended for new users of tensorflow. Unless you are handling large datasets, CPU version of tensorflow works just fine. Also, this is the simplest method to install tensorflow.
Step1: Download whl file
Goto https://pypi.python.org/pypi/tensorflow and download whl pacakage related to your python version and os.
For eg. If your tensorflow version is 1.7.0, your python version is 3.5, and OS is linux then select
Step 2: Install whl file
Create a new virtual environment and activate it then install the whl file using the command. If you are having trouble setting up a virtual environment, you can refer to our other article here.
for python 2:
pip2 install [whl file path]
for python 3:
pip3 install [whl file path]
Step 3: Verify tensorflow installation
Verify tensorflow using following commands:
$ python3 >>> import tensorflow as tf >>> hello = tf.constant('Hello, TensorFlow!') >>> sess = tf.Session() >>> sess.run(hello) 'Hello, TensorFlow!' >>> a = tf.constant(10) >>> b = tf.constant(32) >>> sess.run(a + b) 42 >>> sess.close()
This is all you need to do to install tensorflow CPU version on Ubuntu 16.04.
Install GPU Version of Tensorflow:
Using GPU version of tensorflow will greatly speed up training dataset time. Once you are working with large datasets, it is impractical to rely only on CPU for deep learning. Tensorflow GPU is recommended for intermediate to advanced users and anyone who works with handling large dataset. Advanced users who want to build pip package to get optimum performance can follow the link at the top of the article to build tensorflow gpu for ubuntu.
Step 1: Update and Upgrade your system:
sudo apt-get update sudo apt-get upgrade
Step 2: Verify You Have a CUDA-Capable GPU:
lspci | grep -i nvidia
Note GPU model. eg. GeForce 840M
If you do not see any settings, update the PCI hardware database that Linux maintains by entering update-pciids (generally found in /sbin) at the command line and rerun the previous lspci command.
If your graphics card is from NVIDIA then go to http://developer.nvidia.com/cuda-gpus and verify if listed in CUDA enabled GPU list.
Step 3: Verify You Have a Supported Version of Linux:
To determine which distribution and release number you’re running, type the following at the command line:
uname -m && cat /etc/*release
The x86_64 line indicates you are running on a 64-bit system which is supported by Cuda 9.0
Step 4: Install Dependencies:
sudo apt-get install build-essential
sudo apt-get install cmake git unzip zip
sudo apt-get install python2.7-dev python3.5-dev python3.6-dev pylint
Step 5: Install linux kernel header:
Goto terminal and type:
You can get like “4.10.0-42-generic”. Note down linux kernel version.
To install linux header supported by your linux kernel type the following command:
sudo apt-get install linux-headers-$(uname -r)
Step 6: Download the NVIDIA CUDA Toolkit:
Go to this link and download Installer for Linux > x86_64 > Ubuntu > 16.04 > deb[network]
I highly recommend network installer to get updated gpu driver supported by your linux kernel.
For, direct download
If you have Cuda 9.1 or other version installed then this tensorflow prebuilt package will not work. Remove nvidia cuda related files (drivers, Cuda Toolkit, etc)
sudo apt-get purge nvidia* sudo apt-get auto-remove
sudo apt-key adv --fetch-keys http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/7fa2af80.pub sudo dpkg -i cuda-repo-ubuntu1604_9.0.176-1_amd64.deb sudo apt-get update sudo apt-get install cuda-9.0
Step 7: Reboot the system to load the NVIDIA drivers.