Tutorial: How to Install Tensorflow-GPU 1.8 for Python 2.7 and Python 3.5 on Ubuntu 16.04
Tensorflow is a machine learning framework offered as an opensource project by Google to help the AI community build and realize their models in a variety of programming languages, it offers a cutting edge toolbox implemented in a professional and polished way, it keeps up with the curve and it is witnessing a lot of growth and progress, which makes it ideal for both testing and production.
The problem is, while it’s easy to install and configure this framework and make it utilize CPU power, its documentation seems lacking just a bit when it comes to installing the GPU enabled version.
In this tutorial, we will go step by step about how to get the prerequisites, configure Ubuntu 16.04 for the installation, then actually have tensorflow-gpu up and running.
- A CUDA enabled Nvidia Graphics Card with compute capability of 3.0 or higher, head to the official Nvidia page and check your card for compatibility:https://developer.nvidia.com/cuda-gpus.
- The required Nvidia drivers & library (at the moment version 390).
- The CUDA runtime library & development library (version 9.0).
- The cuDNN runtime library & development library (v7.0.5 for CUDA 9.0).
- Both python 3.5 and python 2.7, which are shipped with Ubuntu 16.04 (but you may need to upgrade their minor versions, that is 2.7.x and 3.5.x), as well as the corresponding python development libraries.
- Both versions (3.5 and 2.7) of the pip package manager which will help you install the rest of the required packages.
Note: Nvidia, CUDA and cuDNN packages are essential for allowing tensorflow-gpu to access the low-level gpu operations/primitives and actually utilize it.
Make sure that the apt package manager can access the Nvidia and CUDA repositories:
- In your dash search, type: Software & Updates.
- Open “Software & Updates” and go to the tab labeled: Ubuntu Software.
- Make sure that (main), (universe), (restricted) and (multiverse) are selected; if not, select them.
Add the Nvidia CUDA repository to the apt package manager sources:
sudo bash -c "echo 'deb http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64 /' > /etc/apt/sources.list.d/cuda.list"
sudo apt-key adv --fetch-keys http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/7fa2af80.pub
sudo apt update
Upgrade your python minor versions:
sudo apt install python2.7 python3.5
Install the initially required packages from the apt package manager:
sudo apt install python-dev python-software-properties python-pip python-tk python3-dev python3-software-properties python3-pip python3-tk nvidia-390 nvidia-390-dev cuda-9-0 nvidia-cuda-dev nvidia-cuda-toolkit
Then Upgrade the pip package manager to the latest version:
sudo pip2 install --upgrade pip
sudo pip3 install --upgrade pip
Download the required cuDNN packages:
- Head to the cuDNN official site https://developer.nvidia.com/rdp/cudnn-download.
- Register for an account, complete the registration and log in.
- Agree to the Terms of the cuDNN Software License Agreement.
- Check the Archived cuDNN Releases.
- From the subsection Download cuDNN v7.0.5 (Dec 5, 2017), for CUDA 9.0:
- Choose cuDNN v7.0.5 Runtime Library for Ubuntu16.04 (Deb) in order to download: libcudnn7_126.96.36.199–1+cuda9.0_amd64.deb.
- Choose cuDNN v7.0.5 Developer Library for Ubuntu16.04 (Deb) in order to download: libcudnn7-dev_188.8.131.52–1+cuda9.0_amd64.deb.
- Choose cuDNN v7.0.5 Code Samples and User Guide for Ubuntu16.04 (Deb) in order to download: libcudnn7-doc_184.108.40.206–1+cuda9.0_amd64.
Note that you need this version specifically (220.127.116.11–1+cuda9.0), because it is the one most compatible with tensorflow-gpu at the moment.
Install the cuDNN packages, open a terminal inside the folder you downloaded the packages to, and issue the following commands:
sudo dpkg -i libcudnn7_18.104.22.168-1+cuda9.0_amd64.deb
sudo dpkg -i libcudnn7-dev_22.214.171.124-1+cuda9.0_amd64.deb
sudo dpkg -i libcudnn7-doc_126.96.36.199-1+cuda9.0_amd64.deb
Setup the appropriate environment variables so that both pip and tensorflow-gpu understand where your cuda paths are:
Some python packages may be required for tensorflow operations and result visualization:
- The numpy package: since tensors are usually fed numpy arrays, and they are definitely evaulated to numpy arrays after they are executed in a tensorflow session.
- The matplotlib and seaborn packages: for data visualization of our training errors and results.
In the same terminal as in step 7, issue the following commands:
sudo pip2 install --no-cache-dir numpy matplotlib seaborn
sudo pip3 install --no-cache-dir numpy matplotlib seaborn
Note that the –no-cache-dir flag may be necessary since caching may break the installation process on some machines, mainly when using pip2.
Install tensorflow-gpu; in the same terminal as in step 7, issue the following commands:
sudo pip2 install --no-cache-dir tensorflow-gpu
sudo pip3 install --no-cache-dir tensorflow-gpu
Check if tensorflow-gpu is installed correctly:
sudo pip2 show tensorflow-gpu
sudo pip3 show tensorflow-gpu
Tensorflow is an amazing set of tools to have in your machine learning development box, but what is best is to have the power version of this tool with the GPU enabled tensorflow, which cuts training time significantly and provides semi-industrial levels of computation capability on a humble laptop with the minimum requirements.
With the steps above, you should have tensorflow-gpu up and running in little time, depending on your internet connection, good luck!
- The Ubuntu word and associated logos are registered trademarks of Canonical Ltd.
- TensorFlow, the TensorFlow logo and any related marks are trademarks of Google Inc.
- The Python logo is a trademark of the Python Software Foundation.
- I am not associated nor endorsed by either: Canonical Ltd, Google Inc, the Python Software Foundation.