The (Ultimate) Guide to Installing TensorFlow

Why Would Someone Do This?

As many of you have probably found, sometimes while installing or working with Tensorflow, you can run into occasional "complications" that don't seem to have well documented solutions, or you'll find that someone tells you to go back and follow the installations instructions on Tensorflow's official website, but sometimes these instructions can be a bit lacking, especially when it comes to installing 3rd party software (especially Bazel).

So here I have culminated every single software install process into one super-mega-mash-up, this is your one-stop-shop for installing tensorflow*

*For Python

Contents

Pre-requisites

Python

Tensorflow requires python, but here's the kicker, it only supports 2.7 and up to 3.6.7 (not 3.7), if you already have that, then you're good to go, if not, see below for how to get it setup on your system.

Windows

You need to download and run the python installer from here, scroll down to the bottom and select your desired download, then run it when you're done.

You probably want to add your python directory to your PATH environment variable, to do this, open settings and search for "Advanced System Settings" there should be a button called "Environment Variables", select that, then a window with two lists will pop up, we want the System variables one, scroll down until you see a variable either called PATH or Path, click on it, and select edit. A new window on it with a bunch of paths will pop up, click on the button on the side that says New or Add or Create, one of those three, this will create a new blank entry for you, paste in the folder of your python install. You'll probably also want to add another one for the Scripts folder within the python folder.

//TODO: Insert information on uninstalling python to correct when users have multiple conflicting versions installed.

Installing pip on windows

First things first, do you have Visual Studio installed? If so, you probably already have python installed, look here C:\Program Files(x86)\Microsoft Visual Studio\shared\ if you installed python, then it's probably in here, to avoid installation conflicts, and lots of headaches later, I'd advise using this one.

Obviously we can't just use sudo apt-get install python-pip to install pip on windows, so you're probably wondering, how the hell do I get that on there, it's going to play a pretty important role in the installation process won't it? And yes my friend it will, but check this out. Pip is actually already in python, in the "Scripts" folder of your python install, so just add the Scripts folder to your system PATH variable, then you can use pip or pip3 with no problems.

Now you should be ready to install tensorflow using pip!

And with that you should be ready to install tensorflow, unless of course you need GPU support.

Linux

This is where things might get a tad bit complicated, if you're satisfied with your current python setup, then skip this, but if not, and you want to run python 3.6.7 or higher (most linux distributions these days come with python 2.7 installed, so if that's the version you want, then you can just skip this), then you're probably going to have to work around the fact that there's probably already some form of python3 installed on your computer, so let me lay out some of the typical steps I have to go through when "upgrading" (but let me be perfectly clear IN NO PART OF THIS WILL WE UNINSTALL THE ORIGINAL VERSION OF PYTHON3, DO NOT DO IT, YOUR SYSTEM NEEDS IT).

Installing python 3 on ubuntu or debian

To get python on your system might seem kind of straightforward, but sometimes it's not, in fact if using a ubuntu version less than18, then you'll probably have to add the repository as well, see below:

 

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python3.6

 

Installing pip or pip3

Having trouble installing pip or pip3? Not sure which to install, or maybe you just don't know the syntax of the package name. When I was using Ubuntu 14.04, it turned out pip was moved to a different repository that my linux distro didn't have access to, here's how you get that.

sudo apt-get install software-properties-common
sudo add-apt-repository universe
sudo apt-get update

And then install pip with

sudo apt-get install python-pip

Or, if you have python3 use

sudo apt-get install python3-pip

And now pip is installed! To test is out try installing the ever popular numpy package using:

pip install numpy

Or if you are using python3

pip3 install numpy

Setting up python and pip to execute python3 and pip3

If you're like me, you probably hate having to type the extra 3 in random places just because you use the most recent version of python, if so, check these out, these are the preferred methods for setting up python to execute python3 and pip to execute pip3 safely without altering your system settings

 

sudo echo "alias python='usr/bin/python3.6'" >> ~/.bash_aliases
sudo echo "alias pip=pip3" >> ~/.bash_aliases
sudo source ~/.bash_aliases

 

And with that you should be ready to install tensorflow, unless of course you need GPU support.

GPU Pre-requisites

There are additional requirements in order to setup your computer to use your gpu devices, following are the dependencies required for tensorflow-gpu to work correctly.

First things first, you need a CUDA enabled GPU with a compute capability of at least 3.5, check out a list of common card compute capabilities here

Next you need the most recent GPU drivers for your card, most likely you already have these, but your card may limit you to what you can use, seeing as how CUDA 9.0 requires 384.x or higher.

You also need the CUDA toolkit, make sure that you get 9.0, and not the most recent version as tensorflow only supports up to 9.0, you can use this link here to get it. If you want a different version, check out here. You'll have to make a developer account in order to install it, just follow the directions and everything will be fine.

If you are using Windows then you need to follow these directions while installing CUDA.

If you are using linux then you need to add the Nvidia repository and install cuda through apt

You need to get cuDNN next, and this one is also tricky, again, you need to make sure to get the CUDA 9.0 version of the DLL, by default it ships version 7.4 of CUDA 10.0, you need to switch it back before downloading, to your current version. If you don't tensorflow will have compatibility issues when training models, but not all the time, only sometimes, it's frustrating, so get this one right. You can download it here again, you need a membership to get it, it's free, just log in and it'll take you to the download page.

Installing CUDA with apt

To install CUDA on linux you need to add nvidia's repository to your distro, see below:

 

sudo apt-key adv --fetch-keys http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/7fa2af80.pub
wget http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/cuda-repo-ubuntu1604_9.1.85-1_amd64.deb
sudo apt install ./cuda-repo-ubuntu1604_9.1.85-1_amd64.deb
wget http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1604/x86_64/nvidia-machine-learning-repo-ubuntu1604_1.0.0-1_amd64.deb
sudo apt install ./nvidia-machine-learning-repo-ubuntu1604_1.0.0-1_amd64.deb
sudo apt update

 

Now you need to install CUDA and it's tools, include the NCCL option

sudo apt install cuda9.0 cuda-cublas-9-0 cuda-cufft-9-0 cuda-curand-9-0 \
    cuda-cusolver-9-0 cuda-cusparse-9-0 libcudnn7=7.2.1.38-1+cuda9.0 \
    libnccl2=2.2.13-1+cuda9.0 cuda-command-line-tools-9-0

Windows

For windows after you finish the above, you are also required to follow the CUDA Install guide for Windows but holy crap is it long and vague, try this instead.

You need Microsoft Visual Studio, make sure that you select the "Desktop Development with C++" workload

This part is actually okay, follow these directions.

Once you have all of these installed you need to update your PATH variable again to include the folders containing CUDA, CUPTI, and cuDNN. The CUDA location is typically C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\bin, CUPTI: C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\extras\CUPTI\libx64, and cuDNN depends on where you stored the DLL, but typically C:\tools\cuda\bin.

That should be it, you should be ready to install tensorflow now!

Pre-Requisites for building from source

Hope you're ready to vomit, just kidding, I'm here to clean up these directions so that that doesn't happen. Okay, so you need a bunch of pip packages, Bazel, and Microsoft Visual Studio Build Tools, and more. Lets get started.

Pip packages

Install the following packages with pip (or pip3 is using python3)

 

pip3 install six numpy wheel
pip3 install keras_applications==1.0.6 --no-deps
pip3 install keras_preprocessing==1.0.5 --no-deps

 

That's it for the pip packages, on to Bazel (and good luck)

Bazel

Bazel can be a bit of a bear to install, so bear with me here.

Windows

You need the Microsoft Visual C++ Redistributable for Visual Studio 2015, which can be found here.

You also need MSYS2, download it from here. You don't actually need to run it, you just need the installed binaries later.

Okay, here's the weird part, you need to download a version of bazel from here but be aware, that supposedly, tensorflow only works with bazel version 19.0and earlier, I went with that one, when I built with version 21.0. I would tell you to use the version from tensorflow's install table, but those releases aren't even available anymore (earlier than version 15.0), so use this one.

Once downloaded, you don't actually want to execute the file, but instead rename it to bazel.exe and move it to your desired location

Now it's time to edit the PATH variable again! Add the directory (folder) where you stored bazel.exe at (normally C:\bazel), the usr/bin directory for MSYS2 (try C:\msys64\usr\bin), and if you haven't yet, add python (assuming you're using through visual studio it's probably C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36...).

Now we need to add new environment variables: create one called BAZEL_SH with a value depicting the bash.exe of your MSYS2 installation (probably here C:\msys64\usr\bin\bash.exe), you don't need to worry about JAVA_HOME unless you plan on working with Java yourself, tensorflow is written in C++, hence the next section.

The extra C++ stuff for bazel that nobody told us about

You MUST have visual studio installed to use Bazel on windows (at least Visual Studio 2015 or above, community version is fine)

Either during your installation or after (using the Visual Studio Installer application), you need to add some extra components not installed by default, these are:

For Visual Studio 2015

Visual C++ > Common Tools for Visual C++ and Visual C++ > Microsoft Foundation Classes for C++

Or for Visual Studio 2017

Desktop development with C++ > Visual C++ MFC for x86 and x64

Now you need the Build Tools for Visual Studio. When you're done it should appear alongside Visual Studio as it's own installation in the Visual Studio Installer application.

And environment variables, one more time! The installation page states that you only need to set one of the two variables here, so just do this one: Make a variable called BAZEL_VS and set it to your Microsoft Visual Studio Installation directory, this is different than the python one, it's normally here C:\Program Files (x86)\Microsoft Visual Studio 14.0\. But for Visual Studio 2017, you should use this path instead C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools.

Okay, last step, you need the windows sdk for your version of windows, see here.

Linux

To get bazel installed on linux is a bit easier than on windows, first you need some additional dependencies installed with apt

sudo apt-get install pkg-config zip g++ zlib1g-dev unzip

Next you need to download a linux binary installer from here, but be aware, that supposedly, tensorflow only works with bazel version 19.0and earlier, I went with that one, when I built with version 21.0. I would tell you to use the version from tensorflow's install table, but those releases aren't even available anymore (earlier than version 15.0), so use this one.

Next you need to run the shell installer from the downloaded files

 

wget "https://github.com/bazelbuild/bazel/releases/download/0.19.1/bazel--installer-linux-x86_64.sh" chmod +x bazel--installer-linux-x86_64.sh
./bazel--installer-linux-x86_64.sh --user

 

The --user directive is not absolutely necessary

To reduce having to use the fully qualified path to bazel everytime we want to build a project we need to add the path to bazel into our $HOME/.bashrc file.

echo export PATH="${PATH}:${HOME}/bin" >> "$HOME/.bashrc" source "$HOME/.bashrc"

Now for the actual installation part

Install the apt resources

sudo apt-get install openjdk-8-jdk

On ubuntu 14.04 you need to add the PPA repository

 

echo "deb [arch=amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list
curl https://bazel.build/bazel-release.pub.gpg | sudo apt-key add -

 

Now install bazel with apt-get

sudo apt-get update && sudo apt-get install bazel

I think this is it, but given how difficult it was to figure out that there are extra dependencies for windows with C++, I'd be there's some page I'm missing here, if you find it let me know down in the comments and I'll add it here

Installing Tensorflow

Installing Tensorflow with pip or pip3

Installing pip on any system is pretty straightforward, pick your flavor of python, install python, and use pip to install the tensorflow or tensorflow-gpu package

pip install tensorflow

Installing Tensorflow by building it from source

Need extra gpu support, want that AVX2 architecture support, who cares about the reason, if you wanna build from source, lets do it!

Windows

First things first we need to install some more dependencies that we skipped earlier becaus bazel hogged the entire section. Open Command prompt and use pacman (installed with MSYS2) to install git, patch, and unzip.

pacman -S git patch unzip

If you want gpu support, go back and install it here, if you haven't already.

Pick a good place, I usually make a separate repository directory ie. C:/Github_Repositories/ and download the tensorflow github repository

git clone https://github.com/tensorflow/tensorflow.git cd tensorflow

You need to checkout the version of tensorflow that you want, which you can find a list of releases here.

git checkout branch_name # r1.9, r1.10, etc.

Okay, so now if you've done everything right up until now you should be ready to start configuring your build! Execute this from the tensorflow/ folder that you downloaded just before this step.

python ./configure.py

This script will walk you through setting up your build, be aware that the capital letter on the [y/n] choices is the suggested choice! You can only install 1 of the gpu support libraries ie. CUDA, otherwise the configure will fail!

Now for the fun part! BEFORE YOU BUILD (At the time of writing this, I don't know if you can do this before you attempt to build the source at least once, if the file does not exist yet, don't create it, simply try to build the source and if it fails with this error message: '...more than one instance of overloaded function "__hadd" matches the argument list:...', come back and do this step) if you are using python3, and you have a graphics card with compute capability greater than 6.0, then you need to go and edit a header file according to this post here, find the lines described here in this file found in your user profile, for example, if my username was iggy the file would be C:\users\iggy\_bazel_iggy\4znqud2j\external\eigen_archive\Eigen\src\Core\arch\CUDA\Half.h. You need to change the following sections:

Starting at about line 200

// conversion steps back and forth.

EIGEN_STRONG_INLINE __device__ half operator + (const half& a, const half& b) {
  return __hadd(a, b);
}

Change the code to look something like this:

// conversion steps back and forth.

EIGEN_STRONG_INLINE __device__ half operator + (const half& a, const half& b) {
#if defined(EIGEN_CUDACC_VER) && EIGEN_CUDACC_VER >= 90000
  return __hadd(::__half(a), ::__half(b));
#else
  return __hadd(a, b);
#endif
}

Then starting at about line 220

EIGEN_STRONG_INLINE __device__ half operator / (const half& a, const half& b) {
  float num = __half2float(a);
  float denom = __half2float(b);
  return __float2half(num / denom);
}

Change the code to look something like this:

EIGEN_STRONG_INLINE __device__ half operator / (const half& a, const half& b) {
#if defined(EIGEN_CUDACC_VER) && EIGEN_CUDACC_VER >= 90000
  return __hdiv(a, b);
#else
  float num = __half2float(a);
  float denom = __half2float(b);
  return __float2half(num / denom);
#endif
}

Now, theoretically, we should be ready to build the package! (cross our fingers)

If using the CPU-only version (no gpu support) run this:

bazel build --config=opt //tensorflow/tools/pip_package:build_pip_package

If using GPU support, run this:

bazel build --config=opt --config=cuda --define=no_tensorflow_py_deps=true --copt=-nvcc_options=disable-warnings //tensorflow/tools/pip_package:build_pip_package

You'll notice the only difference between the two is --config=cuda, --copt=-nvcc_options=disable-warnings, and --define=no_tensorflow_py_deps=true, the latter has to do with a prior issue with package creation (source). The first is the configuration change. And the second suppresses nvcc warning messages.

If your rig doesn't have much RAM, then maybe add the --local_resources 2048,.5,1.0 to limit bazel's RAM consumption to 2GB.

Building the Pip package

Now it's time to build the Pip .whl package. Now when I tried to run this command from inside of the tensorflow/ folder containing the github repository, it didn't work because I guess I can't treat a shortcut as a directory for some reason, I don't know, I just double-clicked on the bazel-bin shortcut inside the folder, then copied that location down and used command prompt, then executed the command using the fully qualified path, if your build was successful you'll know what shortcut I'm talking about and if the shortcut isn't there, then your build probably wasn't successful, or you're inside of the tensorflow/tensorflow/ directory, which is not the one I'm talking about, go back a level to just tensorflow/.

bazel-bin\tensorflow\tools\pip_package\build_pip_package C:/tmp/tensorflow_pkg

Or, if that doesn't work for you, using the shortcut copy and paste thing:

path\to\bazel-bin\bazel-bin\tensorflow\tools\pip_package\build_pip_package C:/tmp/tensorflow_pkg

This creates a .whl file inside of C:/tmp/tensorflow_pkg directory, stored in the file called tensorflow-version-cp36-cp36m-win_amd64.whl

Installing the .whl package using pip or pip3

Now this wheel file can be installed using pip or pip3

pip3 install C:/tmp/tensorflow_pkg/tensorflow-version-cp36-cp36m-win_amd64.whl

Success! Tensorflow is now installed! And honestly if you made it this far without errors, dude, you're a god among mortals

Resetting the ./configure.py script after a failed configure or build

If you misconfigured your tensorflow build, you probably need to delete the .bazelrc file and the tf_configure.bazelrc file that it generated inside of the tensorflow folder, otherwise the configure.py will error out upon start up.

Uninstalling Tensorflow

Didja mess something up, tired of working with this mess of a software package? I understand, here's how you get it out of your system

Contents

Uninstalling with Pip

This is pretty straightforward, just use pip to uninstall tensorflow or tensorflow-gpu in the terminal

pip uninstall tensorflow

Uninstalling from source

Good luck...

Hopefull this blog can help a lot of people, cuz it sure took a long time to write, leave a comment below if you need anything, or just shoot me an email, I can do my best

Comments

Popular posts from this blog

Using Custom Datasets with Tensorflow's Object Detection API

QR Codes

Heapsort and heaps