【无标题】

original article path:https://towardsdatascience.com/installing-tensorflow-gpu-in-ubuntu-20-04-4ee3ca4cb75d

Installing TensorFlow GPU in Ubuntu 20.04

A short guide for installing TensorFlow GPU and its prerequisite packages

Also published at https://afagarap.works/2020/07/26/installing-tf-gpu-ubuntu2004.html

When Ubuntu publishes a long-term support (LTS) release, I usually wait for a while before upgrading, mainly because I’m waiting for CUDA and cuDNN support for the new release. This time, it only took me three months to migrate from Ubuntu 18.04 to Ubuntu 20.04 — well, technically an Ubuntu-based distro, i.e. Regolith Linux. My decision to do so was simply because I upgraded my SSD from 120GB to 1TB, and so I migrated to a different OS as well— albeit just an Ubuntu derivative.
Photo by geordie_strike from Pixabay.

As I expected, it took me a while to work things out in my new system. Fortunately, I saw some helpful answers online, and now I’m expanding on their answers by adding a bit more explanations. So, this post is actually based on the answers given by meetnick and singrium in this related question posted in Ask Ubuntu.

The installation of TensorFlow GPU in Ubuntu 20.04 can be summarized in the following points,

Install CUDA 10.1 by installing nvidia-cuda-toolkit.
Install the cuDNN version compatible with CUDA 10.1.
Export CUDA environment variables.
Install TensorFlow 2.0 with GPU support.

Installing CUDA 10.1

First, ensure that you are using the NVIDIA proprietary driver by going to “Additional Drivers”, and then choosing the appropriate driver, i.e. for CUDA 10.1, the required driver version is ≥ 418.39. We use the proprietary version over the open source one since CUDA can only operate with the proprietary driver.

We are installing CUDA 10.1 because it is the compatible version with TensorFlow GPU.
Image by the author. Choose the appropriate proprietary driver version for CUDA 10.1, i.e. ≥ 418.39.

At the time of this writing, there is no available CUDA 10.1 for Ubuntu 20.04, but as meetnick points out in the referenced Ask Ubuntu post, installing nvidia-cuda-toolkit also installs CUDA 10.1.
Image by the author. The CUDA Toolkit Archive.

For the sake of being verbose, do not try to use 18.10 or 18.04 CUDA 10.1 for Ubuntu 20.04. I learned that the hard way, lol!

So, you can install CUDA 10.1 in Ubuntu 20.04 by running,

$ sudo apt install nvidia-cuda-toolkit

After installing CUDA 10.1, run nvcc -V. Then you will get an output similar to the following to verify if you had a successful installation,

nvcc: NVIDIA ® Cuda compiler driver
Copyright © 2005-2019 NVIDIA Corporation
Built on Sun_Jul_28_19:07:16_PDT_2019
Cuda compilation tools, release 10.1, V10.1.243

Unlike in Ubuntu 18.04 (where I was from), CUDA is installed in a different path in 20.04, i.e. /usr/lib/cuda — which you can verify by running,

$ whereis cuda
cuda: /usr/lib/cuda /usr/include/cuda.h

In Ubuntu 18.04, as you might know, CUDA is installed in /usr/local/cuda or in /usr/local/cuda-10.1.

Installing cuDNN

After installing CUDA 10.1, you can now install cuDNN 7.6.5 by downloading it from this link. Then, choose “Download cuDNN”, and you’ll be asked to login or create an NVIDIA account. After logging in and accepting the terms of cuDNN software license agreement, you will see a list of available cuDNN software.

Click “Download cuDNN v7.6.5 (November 5th, 2019) for CUDA 10.1”, then choose “cuDNN Library for Linux” to download cuDNN 7.6.5 for CUDA 10.1. After downloading cuDNN, extract the files by running,

$ tar -xvzf cudnn-10.1-linux-x64-v7.6.5.32.tgz

Next, copy the extracted files to the CUDA installation folder,

$ sudo cp cuda/include/cudnn.h /usr/lib/cuda/include/
$ sudo cp cuda/lib64/libcudnn* /usr/lib/cuda/lib64/

Set the file permissions of cuDNN,

$ sudo chmod a+r /usr/lib/cuda/include/cudnn.h /usr/lib/cuda/lib64/libcudnn*

Export CUDA environment variables

The CUDA environment variables are needed by TensorFlow for GPU support. To set them, we need to append them to ~/.bashrc file by running,

$ echo 'export LD_LIBRARY_PATH=/usr/lib/cuda/lib64:$LD_LIBRARY_PATH' >> ~/.bashrc
$ echo 'export LD_LIBRARY_PATH=/usr/lib/cuda/include:$LD_LIBRARY_PATH' >> ~/.bashrc

Load the exported environment variables by running,

$ source ~/.bashrc

Installing TensorFlow 2.0

After installing the prerequisite packages, you can finally install TensorFlow 2.0,

$ pip3 install tensorflow==2.2.0

The tensorflow package now includes GPU support by default as opposed to the old days that we need to install tensorflow-gpu specifically.

Verify that TensorFlow can detect your GPU by running,

>>> import tensorflow as tf
>>> tf.config.list_physical_devices("GPU")
[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]

If things went smoothly, you should have a similar output.

You can now enjoy using TensorFlow for your deep learning projects! Hooray!

If you are looking for a TensorFlow project to work on, perhaps you will find my blog on Implementing Autoencoder in TensorFlow 2.0 enjoyable!

Also, if you enjoyed this article, perhaps you will enjoy my other blogs as well!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值