Install 3 different TensorFlow-GPU on the one Ubuntu machine.

Y.C Cheng / 鄭原真
4 min readMay 26, 2019

In this article, I’d like to share with you how to install TensorFlow 1.12, 1.13 and 2.x-nightly on the same Linux.

Why?

To run TensorFlow on Linux machine, you need to install

nvidia driver
cuda toolkit library
cudnn library
tensorflow (with python)

To get the maximum coverage, here are the supported versions:

nvidia driver: 418

cuda toolkit 9.0 + cudnn 7.1.2 + python 3.6: tensorflow 1.12
cuda toolkit 10.0.130 + cudnn 7.6.0 + python 3.7: tensorflow 1.13
cuda toolkit 10.0.130 + cudnn 7.3.1+ python 3.7: tensorflow 2.x-nightly

Ok, let’s deal with it.

Install Nvidia driver on top of Ubuntu.

Per writing this article, I am using Ubuntu 19.04 with Nvidia driver 418. I believe you can also use Ubuntu 18.04LTS with Nvidia driver 418. Please check the install Nvidia driver part in my article on https://medium.com/datadriveninvestor/install-tensorflow-gpu-to-use-nvidia-gpu-on-ubuntu-18-04-do-ai-71b0ce64ebc5.

The reason to use Nvidia driver 418 (or newer version) is to get the best Cuda compatibility. Check link https://docs.nvidia.com/deploy/cuda-compatibility/index.html#binary-compatibility__table-toolkit-driver.

You need to make sure the command nvidia-smi can properly output the status of your Nvidia GPU.

Install anaconda

Go

https://www.anaconda.com/download/#linux

And get the Python 3.7 version. I use this link

https://repo.anaconda.com/archive/Anaconda3-2019.03-Linux-x86_64.sh

Just run it, and accept it to change your ~/.bashrc so that adds the installation direction to your PATH. Re-login and then your environment will properly work.

Conda environment

We are going to install different tensorflow in their own conda environment. A conda environment is a standalone space that you can install a specific set of packages. In the case that you are going to install conflicting sets of packages, you need to create several environments. There are other tools can do similar things, however, from what I know, conda is the one that’s doing the best for now.

I am not going to dive into details on commands of conda environments. However here are what we will use:

conda create env_name
conda activate env_name
conda deactivate env_name
conda env list
conda env remove — name env_name

Install tensorflow 1.12

Fortunately, we can also install Cuda and cudnn via conda. The following is the command to install tensorflow 1.12 in conda environment with the name “tf12”. (I also add keras and h5py for convenient, you can remove it)

$ conda create tf12
$ conda activate tf12
$ conda install -y \
cudatoolkit==9.0 \
cudnn==7.1.2 \
tensorflow-gpu==1.12 \
keras==2.2.4 \
h5py

I think it’s fairly self-explain. Feel free to ask me questions if any.

Tips for using conda

You might want to put all commands into a script. If you try that, you will find the command “conda activate tf12” is not working properly there. Here is the solution. You need to put a command

source $HOME/anaconda3/etc/profile.d/conda.sh

before the “conda activate tf12” command, so it can work properly. The secret behind that is related to how the shell runs a command and how it can modify the shell environment it’s currently running.

Along the way of testing new environments, you might need to install the same package several times, and you might want to remove the $HOME/anaconda3 and do a clean re-install. You will find it very time-consuming because tensorflow, Cuda, and cudnn are quite large in size, it uses so much time to keep re-downloading those packages.

I create a directory $HOME/anaconda3-pkgs, and set environment variable by command:

export CONDA_PKGS_DIRS=$HOME/anaconda3-pkgs

before doing the installation. Without this change, the package tarball will be stored in $HOME/anaconda3/pkgs. So it’s hard for us to reuse it if we want to remove $HOME/anaconda3 and do the clean re-install again. With the above settings, the newly downloaded package will keep there. So you don’t need to re-download it the next time you want to use it.

Put everything together

To install tensorflow 1.12, use the following commands in your script:

source $HOME/anaconda3/etc/profile.d/conda.sh
export CONDA_PKGS_DIRS=$HOME/anaconda3-pkgs

conda create tf12
conda activate tf12
conda install -y \
cudatoolkit==9.0 \
cudnn==7.1.2 \
tensorflow-gpu==1.12 \
keras==2.2.4 \
h5py

To install tensorflow 1.13, use the following commands in your script:

source $HOME/anaconda3/etc/profile.d/conda.sh
export CONDA_PKGS_DIRS=$HOME/anaconda3-pkgs

conda create tf13
conda activate tf13
conda install -y \
tensorflow-gpu==1.13.1

To install tensorflow 2.x nightly, use the following commands in your script:

source $HOME/anaconda3/etc/profile.d/conda.sh
export CONDA_PKGS_DIRS=$HOME/anaconda3-pkgs

conda create tf2x
conda activate tf2x

conda install -y \
pip \
cudatoolkit==10.0.130 \
cudnn=7.3.1

# this need to install via pip
pip install tf-nightly-gpu-2.0-preview

For tensorflow 2.x nightly, there still no conda package exists for it, so we need to use pip to install it.

Conclusion

Well, Python dependency is hard to manage, and conda makes it simple. Deep learning is hard, so let’s make it easier to setup.

--

--