Install TensorFlow on Linux for Deep Learning

Got a Linux PC with an NVidia graphics card? Let's turn it into a deep learning workstation.

TensorFlow and Linux logos
Install TensorFlow on Linux

In this tutorial, you'll learn how to install TensorFlow on a Linux PC easily to turn it into a deep learning workstation!

If you want an introduction to deep learning, you can check out my post about installing tensorflow on Windows .

Prerequisites

What you need is:

  • A PC with Ubuntu . I’ve used Ubuntu 16.04 LTS, and maybe it will work with later versions of Ubuntu. However, it’s not going to work with other Linux distributions;
  • An NVidia graphics card in the PC . You can check that your NVidia card is supported here . The tutorial will not work with a graphics card from another brand.

Step 1 : Drivers

Even if you have a nice NVidia graphics card, your PC won’t be able to anything with if can’t talk to it. That’s what drivers are for.

To install TensorFlow, you need NVidia drivers of version 384.X or more recent.

We’re now going to install the latest drivers.
First, let’s add a repository to apt to help us with that:

sudo add-apt-repository ppa:graphics-drivers/ppa
sudo apt update
sudo apt upgrade

Then do the following to find out what is the recommended driver:

ubuntu-drivers devices

This gives me:

== /sys/devices/pci0000:16/0000:16:00.0/0000:17:00.0 ==
modalias : pci:v000010DEd00001B06sv00001028sd00003600bc03sc00i00
vendor   : NVIDIA Corporation
driver   : nvidia-396 - third-party free
driver   : nvidia-415 - third-party free recommended
driver   : nvidia-390 - third-party free
driver   : xserver-xorg-video-nouveau - distro free builtin
driver   : nvidia-384 - third-party non-free
driver   : nvidia-410 - third-party non-free

Select the recommended driver and install it:

sudo apt install nvidia-415

Reboot the computer, and check out that everything’s as expected:

nvidia-smi

You should get something like:

Fri Jan 25 18:30:34 2019       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 415.27       Driver Version: 415.27       CUDA Version: 10.0     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  GeForce GTX 108...  Off  | 00000000:17:00.0 Off |                  N/A |
| 23%   33C    P8    17W / 250W |      2MiB / 11178MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+
|   1  GeForce GTX 108...  Off  | 00000000:65:00.0  On |                  N/A |
| 23%   34C    P8    16W / 250W |    296MiB / 11176MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+
                                                                               
+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
|    1      1491      G   /usr/lib/xorg/Xorg                           132MiB |
|    1      2190      G   compiz                                       161MiB |
+-----------------------------------------------------------------------------+

As you can see, the driver version is correct.

If you want to do further tests and look at nice 3D displays, you could use the phoronix test suite .

Step 2: Deep Learning Software

To do deep learning on your card, you will need the following software (don’t install them, I have an easier solution for you below):

  • CUDA : parallel computing architecture from NVidia. CUDA will make it possible for you to use the many computing cores of your GPU for mathematical calculations.
  • cuDNN : specific library for deep neural networks with CUDA.
  • TensorFlow : Google’s deep learning library
  • Keras : Nice python interface to tensorflow

But installing CUDA and cuDNN manually can be a real pain. Fortunately, we won’t need to do that.

Instead, download anaconda for your system:

  • Choose either the python 2.X or 3.X version
  • make sure to pick the 64-bit installer.

Run the installer.

Install TensorFlow and keras:

conda install tensorflow-gpu keras

In most articles on this blog, we use jupyter and matplotlib to run our code interactively and to display the results. So let's install these packages as well:

conda install jupyter matplotlib

Step 3: Test!

And finally, test it:

python -c "import tensorflow as tf; tf.enable_eager_execution(); print(tf.reduce_sum(tf.random_normal([1000, 1000])))"

I'm getting:

2019-01-25 18:39:59.281394: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 AVX512F FMA
2019-01-25 18:39:59.475540: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1432] Found device 0 with properties: 
name: GeForce GTX 1080 Ti major: 6 minor: 1 memoryClockRate(GHz): 1.582
pciBusID: 0000:17:00.0
totalMemory: 10.92GiB freeMemory: 10.76GiB
2019-01-25 18:39:59.617848: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1432] Found device 1 with properties: 
name: GeForce GTX 1080 Ti major: 6 minor: 1 memoryClockRate(GHz): 1.582
pciBusID: 0000:65:00.0
totalMemory: 10.91GiB freeMemory: 10.47GiB
2019-01-25 18:39:59.618681: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1511] Adding visible gpu devices: 0, 1
2019-01-25 18:40:00.021667: I tensorflow/core/common_runtime/gpu/gpu_device.cc:982] Device interconnect StreamExecutor with strength 1 edge matrix:
2019-01-25 18:40:00.021703: I tensorflow/core/common_runtime/gpu/gpu_device.cc:988]      0 1 
2019-01-25 18:40:00.021708: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1001] 0:   N Y 
2019-01-25 18:40:00.021711: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1001] 1:   Y N 
2019-01-25 18:40:00.022288: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1115] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 10407 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1080 Ti, pci bus id: 0000:17:00.0, compute capability: 6.1)
2019-01-25 18:40:00.022606: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1115] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:1 with 10126 MB memory) -> physical GPU (device: 1, name: GeForce GTX 1080 Ti, pci bus id: 0000:65:00.0, compute capability: 6.1)
tf.Tensor(629.1958, shape=(), dtype=float32)

What's next?

In this post, you have learnt how to install TensorFlow on your Linux PC with the help of Anaconda.

Now you can Get Started with Deep Learning!


Please let me know what you think in the comments! I’ll try and answer all questions.

And if you liked this article, you can subscribe to my mailing list to be notified of new posts (no more than one mail per week I promise.)

Back Home