How to Install TensorFlow GPU on Ubuntu 18.04 LTS?
With TensorFlow, you can do a number of important tasks. It supports the urgent execution of the machine-learning model, automatic differentiation, multi-GPU/distributed training, and more. However, the latest and biggest feature of Tensorflow is Keras that works as the deep-learning API tool for the open-source machine-learning platform. TensorFlow GPU was developed by Google for data scientists and researchers to help them implement machine-learning models easily.
TensorFlow can perform multiple numerical computations to help you with machine-learning techniques. Since it works equally great in CPUs, GPUs, and smartphones (Android & iOS both), you won’t have a problem using it. However, in this article, we have shared our guide on installing TensorFlow GPU on Ubuntu 18.04. To install it on an older or later version of Ubuntu, you can check out our other guides. But for now, keep reading this article and follow the quick steps to know How to Install TensorFlow GPU on Ubuntu 18.04 LTS.
But before we begin with the installation process, we want you to know a little bit about the Keras implementation in TensorFlow 2.0. The Keras feature is only available in version 2 of TensorFlow. Keras help you train your deep neural networks as well as fix bugs. The features and implementations of Keras should be inside the tf.keras package.
Now, it’s time to get started with the process for installing TensorFlow GPU on Ubuntu 18.04.
Things to Note Before Starting the Installation Process
Before we start with the installation steps of TensorFlow GPU on Ubuntu, here are some things you must know:
- This article is for Python users only who are coding with IDE and virtual environments that are not related to Anaconda.
- If you are using Anaconda, we encourage you to check out our other guides created especially for installing TensorFlow GPU on Anaconda.
- Though we have highlighted the installation process for Ubuntu 18.04 LTS, the same steps should help you install TensorFlow GPU on other Ubuntu versions.
- Lastly, sometimes you need to configure your TensorFlow to work. If downloading only all the CUDA/cuDNN packages doesn’t help, you need to configure your TensorFlow.
Prerequisites for Installing TensorFlow GPU on Ubuntu
Before you install TensorFlow on Ubuntu, make sure you fulfill the following requirements:
- Your machine should have an NVIDIA graphics card.
- Your system should be running the Ubuntu 18.04 LTS version.
- Make sure you have established a Python development environment for machine learning on your Ubuntu system.
- You should access the command line/terminal window.
- You should have the sudo privileges for your user account.
- You need pip 19.0 or a later version.
Step-by-Step Guide for Installing TensorFlow GPU on Ubuntu 18.04 LTS
Step 1: Download the Required Packages
We have told you that you need the Python development environment in order to run TensorFlow on your Ubuntu system. So, you should have the Python version 3.4 or later and the pip package manager, which should not be earlier than the 19.0 version, installed on your system. Along with that, you need Virtualenv, which is a software specially designed for isolating Python environments.
To set up the Python package, you can run some simple commands on the terminal. Run, sudo apt update and this command will update the repository.
When the update is completed, run sudo apt install python3-dev python3-pip. This will install the python and pip package on your computer.
To install the Virtualenv software, run the following command:
sudo pip3 install -U virtualenv
Once these packages are installed successfully, it’s time to check out the status of your NVIDIA graphics card driver. Your graphics card should be NVIDIA compute 3.0 otherwise TensorFlow won’t run. You can visit this page to acquire deeper knowledge regarding the compute compatibility for TensorFlow. You can check out if the NVIDIA graphics card is installed or not with the command as follows:
To install the NVIDIA graphic card driver on your system, run the following command:
$ modinfo nvidia | grep version
You can also update the NVIDIA driver with the following command:
$ ubuntu-drivers devices
Once the installation and update are done, reboot your computer.
Once certain packages have been installed, we will generate a virtual environment for TensorFlow on Ubuntu.
Step 2: Create a Virtual Environment
We have mentioned that installing the Virtualenv software on your Ubuntu system will isolate your Python environments. Also, it will help you create a virtual Python environment the same way. You can create a virtual environment in a /venv directory with the following command:
virtualenv –system-site-packages -p python3 ./venv
And this command will activate the virtual environment and keep it running:
The shell prompt that appears in the output will have the (venv) prefix. When the output appears, it shows that you have successfully initiated the environment. After activating the virtual environment, you will have to install the pip package inside of that isolated environment:
pip install –upgrade pip
To see all the packages that are staying in the virtual environment, use the following command:
Now an output will appear where you will be able to see all the packages along with their individual versions. After creating the virtual environments, we will install the CUDA Toolkit and cuDNN Library that will help us go ahead further with the TensorFlow installation process.
Step 3: Install CUDA Toolkit and cuDNN Library
Since we already have the latest version of NVIDIA drivers, it’s time to install the CUDA Toolkit that will be compatible with your NVIDIA driver. It can be possible that you have the toolkit already installed on your system. But whether you are running the right version or not, this command will help you know:
$ nvcc –version
$ apt list –installed | grep -i nvcc
If the CUBA version is incompatible or not working out for you, you can uninstall it using the following commands:
$ sudo apt-get — purge remove “*cublas*”
$ sudo apt-get — purge remove “cuda*”
You can install the right version of the CUBA toolkit from here, and this link will tell you how to install the CUBA toolkit that will be compatible with your Tensorflow GPU libraries. In the CUBA Toolkit archive, you will find the respective compatible version of CUBA for different versions of Ubuntu. For example, Ubuntu 18.04 generally supports CUDA Toolkit 10.0. So, you will have to find that option on the archive page and click on the “Download” button.
Once the CUBA toolkit is installed, go ahead and install the cuDNN library.
To check if the cuDNN library is installed or not and compatible with your system, run this command:
$ apt list –installed | grep -i libcudnn
You can remove the existing version of the cuDNN library with this command if it is not compatible with your system:
$ sudo apt-get –purge remove “libcudnn7*”
Then visit here and click on Download cuDNN. First, you will have to sign up or log in to the page. If you have not signed up before, register and agree to the terms to see the cuDNN Download page. On the Download page, you will find your suitable cuDNN version but if it is not there, click on “Archived cuDNN Releases”. The cuDNN version needs to match the CUBA version, for example, cuDNN 7.6.3 with CUDA 10.0.
Once you find the option, click on it and download the three Deb Files for your Ubuntu 18.04 system. Here are the three main files you should download:
- cuDNN Runtime Library
- cuDNN Developer Library
- cuDNN Code Samples and User Guide
You can run these commands to install those downloaded files:
$ sudo dpkg -i libcudnn7_18.104.22.168-1+cuda10.0_amd64.deb
$ sudo dpkg -i libcudnn7-dev_22.214.171.124-1+cuda10.0_amd64.deb
$ sudo dpkg -i libcudnn7-doc_126.96.36.199-1+cuda10.0_amd64.deb
When you are using these commands, make sure to change the names of the files with the ones you have downloaded earlier. However, keep in mind that you should not allow these files to get updated automatically otherwise they would no longer be compatible with your system. To hold the version, use these commands:
$ sudo apt-mark hold libcudnn7 libcudnn7-dev
$ apt-mark showhold
Once all the packages are installed successfully, it’s time to start with the TensorFlow installation.
Step 4: Install TensorFlow GPU and Keras on Ubuntu
The TensorFlow version needs to be supported by the CUDA Toolkit and cuDNN libraries that you have previously installed. Since we have installed CUDA 10.1, it will support any version of TensorFlow. But you can also install a specific version of TensorFlow.
In this section, we will be installing the TensorFlow for GPU 1.14 version with the following command:
$ pip install “tensorflow-gpu==1.14.*”
$ pip install keras
Verify the version with this command:
$ pip list | grep -i tensor
$ pip list | grep -i keras
Now you have successfully installed TF but you will have to check the status of the installation. You need to verify if both the cuDNN and TensorFlow GPU are working or not. To verify the cuDNN installation, run these commands:
$ cp -r /usr/src/cudnn_samples_v7/ $HOME
$ cd $HOME/cudnn_samples_v7/mnistCUDNN
$ make clean && make
When the cuDNN works properly, they show you the Test passed! Message. You can verify TensorFlow with your command line too. Run these commands on the terminal:
>>> import tensorflow as tf
If your TensorFlow is working well, it should show you out like this:
If you want to monitor the current CPU usage and temperature of your TensorFlow GPU process, you can run these commands:
$ watch -n 5 nvidia-smi
It will show you all the information about the TensorFlow GPU on your Ubuntu system.
How to Install TensorFlow for CPU on Ubuntu?
When it comes to installing TensorFlow CPU on Ubuntu, here are the commands:
For installing TensorFlow for CPU 1.14: pip install tensorflow==1.14, and TensorFlow 1.15: pip install tensorflow-gpu==1.15rc2.
That’s all you need to install the TensorFlow on Ubuntu and it’s all an easy process. The command python -c “import tensorflow as tf;print(tf.reduce_sum(tf.random.normal([1000, 1000])))” will help you verify the installation with ease.
With TensorFlow on Ubuntu 18.04 LTS, you can improve your Deep Learning projects. But for whatever reason you need help with the same, feel free to drop us a command below. Alternatively, you can use Google Colab or Docker for your cloud solutions. Google Colab offers GPU and TPU for free.
That’s all about installing the TensorFlow GPU on Ubuntu 18.04 LTS. However, here, we have only mentioned how to install TensorFlow for GPU 1.14. The installation process of TensorFlow for CPU 1.14 and TensorFlow 1.15 works the same way with different commands. You will have to go through all the steps given above to install the TensorFlow on Ubuntu 18.04 LTS.