Install Tensorflow-GPU to use Nvidia GPU using anaconda on Ubuntu 18.04 / 18.10 do AI!
This is the old version for this article. Please go https://medium.com/datadriveninvestor/install-tensorflow-gpu-to-use-nvidia-gpu-on-ubuntu-18-04-do-ai-71b0ce64ebc5 for the new one.
* Nov 22, 2018: updated for Tensorflow 1.12 released.
* Oct 22, 2018: updated for Ubuntu 18.10 released.
* Oct 15, 2018: updated for Tensorflow 1.11 released.
I will share the background of GPU and Deep Learning. Then how you install Nvidia driver and Tensorflow with GPU acceleration back-end on Ubuntu 18.04 LTS. The following steps also work for Ubuntu 18.10.
If you just want to know all the steps, you can skip to the section “Summary of steps”.
Deep Learning / Machine Learning is a big revolution in the computing world! The software today has not really changed too much compared to 10 years ago in it’s underlying design. It’s definitely more beautiful, faster and more convenient for us. However, Deep Learning/Machine Learning is changing the world in such a different way!
I found there are so many documents around explaining how to install Tensorflow-GPU on Ubuntu. There are good articles, and I learn from those, too. However, it seems none of those have wider enough coverage. That’s what I want to address this issue!
Why GPU?
GPU is one of the key components of this revolution that it can do without. Some scientists might say that there is nothing new in this AI revolution. Well, maybe partially. There are some theories that have been there for 10s of years, however, due to slow CPU and limited data, they won’t be as useful today.
You can install tensorflow without GPU as starting point to learn, however you will find that it takes too long to finish the computing, and then you want to use a machine with GPU or other HW acceleration.
Surely you may pay money so you can use GPU on the cloud server, but you could also want to have your own machine to do the proof of concept or smaller computing before you send it to cloud server.
Nvidia GPU has rather good cost-performance and rather lower entry price, etc. That’s why you will find lots of information about it.
Why Tensorflow!
Google is the company behind it, period. In the meantime, it’s the top choice among people doing AI. It has multiple back-ends. Nvidia GPU is the one that used most except people work for Google or using Google cloud server.
Why Ubuntu!
I’ll rather ask, why not? Ubuntu is the top choice on the public cloud server. It has a good Desktop/Laptop support. You can purchase a certificated machine that’s pre-install Ubuntu on the Market. One of the companies that sell lots of Pre-install Ubuntu is Dell, and Dell also contributes a lot to make sure their computer works well with Ubuntu, like push hardware vendor to provide drivers, etc.
Get a machine with Nvidia GPU
You might want to ask me how to choose one if you are going to buy a GPU card and plug-in to a PC. How big a memory should I choose?
IMHO, I’ll suggest you buy one entry-level GPU card with more memory. Why? Not all kind of Deep Learning algorithm can be effectively paralleled in a machine with multiple GPU cards. There will always be a computing/communication balance issue. For further information, you can check this article [1].
To know if your GPU is supported, check:
https://developer.nvidia.com/cuda-gpus
Ok, here is the background: Tensorflow-GPU uses two software component from Nvidia to access Nvidia GPU. They are CUDA toolkit and cuDNN. If your GPU supports CUDA, then we shall be able to use it to do GPU accelerated tensorflow.
You could get a desktop machine that only has Nvidia GPU, however, if you get a laptop, usually you get a so-called “I+A” machine. An I+A machine is a design that you have both Intel GPU and Nvidia GPU installed on your machine. You can choose to use one of them by configuring your system.
Install Ubuntu 18.04 / 18.10 on it!
Finally, we are going to work on it… If you are using Ubuntu pre-install machine with Nvidia GPU, you can skip this part. Well, Dell Machine with Nvidia GPU that comes with 18.04 might not start to ship for today, believe me, it won’t take too long.
I think most people who read this article already know how to install Ubuntu, but if you don’t, check it in: https://www.ubuntu.com/. The steps are
1. Download the Ubuntu desktop iso image. For today it’s “ubuntu-18.04.1-desktop-amd64.iso”. Ubuntu 18.10 also works.
2. Either burn a CDROM or prepare a USB stick with the ISO file.
3. Boot your machine with it, and finish the installation. Usually, you won’t have trouble at this stage.
4. Reboot your machine!
Some people might have issues after reboot your machine. I have some tips that could help you out if you can’t properly login after reboot.
Install Proprietary Nvidia Driver
Open a terminal, use the following command:
sudo apt update
sudo ubuntu-drivers autoinstall
sudo reboot
If it works well, you will install lots of deb packages, like “nvidia-driver-390”, etc. Then reboot your machine. If it successfully reboots, open a terminal and use command “nvidia-smi”. If it properly shows you the status of Nvidia GPU like below, then it’s already properly installed.

If you’ve been this far, you have finished the riskiest part of the installation process.
Some troubleshooting tips
There could be too many situations to handle. I’ll only share the most common one from my experience.
On an Intel GPU + Nvidia GPU (mostly this case when you use a Laptop with Nvidia GPU), sometime you just can’t log in after system installation, like system freeze after booting. If you are kind of Linux expert, I think the following step is enough for you, or we need another article to do a step by step guide.
- Add “module_blacklist=nouveau” to the Linux booting parameter as booting in GRUB. Usually, you will be able to login into the system with this.
- Install the proprietary Nvidia driver mentioned in the previous section. Usually, after this, you can have properly working system after reboot. If not… well, you might have a special hardware combination that needs more work to make it work.
Make sure Nvidia is enable
If you purchase an Ubuntu-pre-installed machine from a vendor like Dell, the Nvidia driver is already installed, but it’s not turned on by default.
Use the following command to make sure Nvidia GPU is enabled.
$ sudo prime-select nvidia
$ sudo reboot
After that, “nvidia-smi” will output the GPU status.
We can install Tensorflow now!
There are many ways to install tensorflow. If you are a heavy docker user, you might want the docker way:
https://www.tensorflow.org/install/install_linux#InstallingDocker
It’s fairly simple. Feel free to ask us questions if you have any.
Except that, I think the easiest way is using Anaconda. If you use Anaconda before, you must finish reading this article, or it won’t work. Why? Anaconda seems not so recommend in the tensorflow web site.
First, you need to install Anaconda. Go
https://www.anaconda.com/download/#linux
And get the Python 3.6 version. I use this link
https://repo.anaconda.com/archive/Anaconda3-5.2.0-Linux-x86_64.sh
Just run it, and accept it to change your ~/.bashrc so that adds the installation direction to your PATH.
Anaconda already has a newer version 5.3, however, it’s Python 3.7 and that’s not tensorflow-GPU pre-compiled version built with. Don’t use it!
Here is the key part. Use the following command to install tensorflow-gpu 1.12:
conda install \
tensorflow-gpu==1.12 \
cudatoolkit==9.0 \
cudnn=7.1.2 \
h5py
You might want to add other packages like keras, scikit-learn, panda or numpy. Just add it to the command line with or without version. Conda will let you review the operation as you doing the installation. Just be sure that you keep the version number of cudatoolkit and cudnn.
Why? Recent tensorflow-GPU use Nvidia library with above version. Anaconda provides a newer version of those Nvidia libraries, but it’s not the pre-compiled tensorflow-GPU is using. If you use other versions, the pre-compile tensorflow-GPU just won’t work !!
Verify installation
Well, if you follow the step above, it works. However, you want to test it. On the tensorflow website, it suggests you use the following python code:
import tensorflow as tf
hello = tf.constant(‘Hello, TensorFlow!’)
sess = tf.Session()
print(sess.run(hello))
It works! However, it runs if you are using CPU to do computing. How do you know if you are using GPU? Try the following code:
import tensorflow as tf
print(“tf version = “, tf.__version__)
with tf.device(‘/gpu:0’):
a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name=’a’)
b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name=’b’)
c = tf.matmul(a, b)with tf.Session() as sess:
print (sess.run(c))
The final result output will be
tf version = 1.12.0
[[22. 28.]
[49. 64.]]
plus lots of other tensorflow messages as tensorflow-GPU is doing internal initialization.
Summary of steps
- Install Ubuntu 18.04 / 18.10 Desktop.
- Install Nvidia driver on a machine with a supported Nvidia card.
$ sudo apt update
$ sudo ubuntu-drivers autoinstall
$ sudo reboot
3. You might need to switch to Nvidia GPU from Intel GPU by using
$ sudo prime-select nvidia
$ sudo reboot
4. Install Anaconda with python 3.6.
https://repo.anaconda.com/archive/Anaconda3-5.2.0-Linux-x86_64.sh (Don’t use python 3.7.)
5. Install tensorflow-gpu
$ conda install \
tensorflow-gpu==1.12 \
cudatoolkit==9.0 \
cudnn==7.1.2 \
h5py
6. You’re done.
Conclusion
There is so much to learn on the way of Machine Learning/Deep Learning. You need to have the domain knowledge to know your data. To know the mathematics behind so your trained model works and lots more. However, setting up computer should be fairly easy. Hope this article gives you a quick way to prepare you Ubuntu to start with!
Reference:
[1] http://timdettmers.com/2018/08/21/which-gpu-for-deep-learning/