How to install Tensorflow and CUDA in linux systems - a beginners Guide

archie9211 | May 21, 2019 min read

Getting GPU working for tensorflow and CUDA is a difficult task for naive linux users. In this guide, you can easily understand the installation process. This guide is focussed on debian based distros, but it can be used on any linux distro with respective command for the task according to distro.

So, What do we need for working tensorflow and nvidia drivers on linux

  • A linux Machine, ofcourse.
  • Nvidia Drivers Installed.
  • Compatible tensorflow version Installed
  • Tensorflow compatible Cuda Version Installed.
  1. Make sure You have nvidia Drivers installed
    I have nvidia geforce 940mx, So the latest drivers for me was v430.14
    You can download and install Latest drivers from here

  2. Reboot and make sure nvidia drivers are installed successfully

    • run nvidia-smi in terminal
      It should give some output in tabular form like here :

    Image

    • make sure Nouveau drivers are disbaled ( These are automatically disabled after nvidia drivers installation)
      to check: run lsmod | grep nouveau in terminal

      * If there is no output
          * then its disabled and we are good to go.
      * else
          * Create a file at ` /etc/modprobe.d/blacklist-nouveau.conf ` with the following contents:
      
blacklist nouveau
options nouveau modeset=0
        #Regenerate the kernel initramfs:<br>
sudo update-initramfs -u # (Debian based)
sudo grub-mkconfig -o /boot/grub/grub.cfg # (Arch linux based)
  1. Download cuda runfile from this link (in my case I used cuda 10.0,you can check latest cuda version supported by tf here)
    as shown here :

  2. Installing CUDA
    open terminal in the file location folder and run

    • sudo sh cuda_10.0.130_410.48_linux.run # change the name of file accordingly
    • accept the terms and conditons by typing accept when prompt asks for it
    • when a selection menu comes up select everything accept drivers
    • and install
    • wait for installation (might take 5-10 mins)
  3. Installing cuDNN 7.5.0 (compatible with cuda 10.0,Note : 10.1 was not supported at that time)

# select : "cuDNN Library for Linux"
# extract the downloaded file
tar -zxvf cudnn-10.0-linux-x64-v7.5.0.56.tgz
#Move the unpacked contents to your CUDA directory
sudo cp -P cuda/lib64/libcudnn* /usr/local/cuda-10.0/lib64/
sudo cp  cuda/include/cudnn.h /usr/local/cuda-10.0/include/
# Give read access to all users
sudo chmod a+r /usr/local/cuda-10.0/include/cudnn.h /usr/local/cuda/lib64/libcudnn*
  1. Install libcupti-dev
sudo apt-get install libcupti-dev nvidia-modprobe
  1. Post Install actions (define paths in ~/.bashrc or ~/.zshrc (for zsh users) )
export PATH=/usr/local/cuda-10.0/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda-10.1/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
  1. Finally install tensorflow-gpu using pip
pip install --upgrade tensorflow-gpu
  1. Check the status of tf in python3
    from tensorflow.python.client import device_lib
    device_lib.list_local_devices()
    
    Output should be something like :
[name: "/device:CPU:0"
device_type: "CPU"
memory_limit: 268435456
locality {
}
incarnation: 2919472738789042941
, name: "/device:XLA_GPU:0"
device_type: "XLA_GPU"
memory_limit: 17179869184
locality {
}
incarnation: 17258321204030932820
physical_device_desc: "device: XLA_GPU device"
, name: "/device:XLA_CPU:0"
device_type: "XLA_CPU"
memory_limit: 17179869184
locality {
}
incarnation: 1501423454042607117
physical_device_desc: "device: XLA_CPU device"
, name: "/device:GPU:0"
device_type: "GPU"
memory_limit: 1649016832
locality {
    bus_id: 1
    links {
    }
}
incarnation: 1732355850559614807
physical_device_desc: "device: 0, name: GeForce 940MX, pci bus id: 0000:01:00.0, compute capability: 5.0"
]

And you will have a Working GPU with tensorflow. Happy Coding.

comments powered by Disqus