提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
ubuntu20.04安装显卡驱动,cuda , cudnn, tensorflow-gpu2.*
1、源配置文件
备份源文件
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
修改源文件
sudo gedit /etc/apt/sources.list
163源:
···
deb http://mirrors.163.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-backports main restricted universe multiverse
···
阿里:
···
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
···
清华:
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
保存并关闭文件
终端输入:
···
sudo apt update
sudo apt upgrade
···
换源后,安装build-essential(安装完成后终端输入gcc不报错):
sudo apt-get install build-essential
查看显卡
···
ubuntu-drivers devices
···
自动安装驱动
···
ubuntu-drivers autoinstall
···
禁用开源驱动
···
sudo gedit /etc/modprobe.d/blacklist.conf
···
在文本最后添加
···
blacklist nouveau
options nouveau modeset=0
···
生成新的内核初始化文件
···
sudo update-initramfs -u
···
此处重启,输入
···
lsmod | grep nouveau
···
若没有输出,则成功禁用开源驱动。
输入
···
nvidia-smi
···
输出如下内容说明显卡安装成功
cuda10.2依赖gcc7,安装gcc7
sudo apt install gcc-7 g++-7
此处系统中存在两个gcc,分别是9和7,此时默认为9,这里需要将默认gcc改为gcc7。终端输入:
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 90 --slave /usr/bin/g++ g++ /usr/bin/g++-9 --slave /usr/bin/gcov gcov /usr/bin/gcov-9
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 70 --slave /usr/bin/g++ g++ /usr/bin/g++-7 --slave /usr/bin/gcov gcov /usr/bin/gcov-7
sudo update-alternatives --config gcc
选择1回车然后再次输入gcc –v查看版本为7.*
官方下载cuda10.2
cuda10.2官方地址
在驱动文件所在路径执行如下操作(此处需要根据自己的文件名称修改)
···
chmod a+x cuda_10.2.89_440.33.01_linux.run
sudo ./cuda_10.2.89_440.33.01_linux.run
···
如果安装完成未出现以下画面,说明安装成功,直接执行下面添加环境变量操作,否则执行cuda10.2安装异常操作
添加环境变量
安装完毕后,添加环境变量:
打开.bashrc文件:
···
gedit ~/.bashrc
···
在文末添加
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda-10.2/lib64
export PATH=$PATH:/usr/local/cuda-10.2/bin
export CUDA_HOME=$CUDA_HOME:/usr/local/cuda-10.2
保存并关闭文件,更新环境:
source ~/.bashrc
三、cuda10.2安装异常操作
在.bashrc中添加环境变量
···
gedit ~/.bashrc
···
在文件的末尾加入下面两行语句:
export PATH=/usr/local/cuda-10.2/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda-10.2/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
设置环境变量和动态链接库
···
sudo gedit /etc/profile
···
在末尾加入下面语句:
export PATH=/usr/local/cuda/bin:$PATH
保存退出
创建链接文件:
···
sudo gedit /etc/ld.so.conf.d/cuda.conf
···
加入下面语句:
/usr/local/cuda/lib64
然后执行:
sudo ldconfig // 使文件生效
测试CUDA的Sample
cd /usr/local/cuda-10.2/samples/1_Utilities/deviceQuery
make
sudo ./deviceQuery
去官网下载cudnn,期间需要登录才可以下载。下载与cuda对应的cudnn版本即可,此处选择了7.6.5。
终端到下载路径解压缩
‘’’
tar -xzvf cudnn-10.2-linux-x64-v7.6.5.32.tgz
‘’’
将文件复制到cuda下:
cp cuda/include/cudnn.h /usr/local/cuda/include
cp cuda/lib64/libcudnn* /usr/local/cuda/lib64
chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn*
安装完成
默认已安装anaconda3
创建虚拟环境
conda create -n python38-tensorflow python=3.8
激活虚拟环境
conda activate python38-tensorflow
安装tensorflow-gpu
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple tensorflow-gpu
测试tensorflow是否可以调用gpu
python
import tensorflow as tf
print(tf.test.is_gpu_available())
输出为True说明tensorflow-gpu可以调用gpu
以上就是今天要讲的内容,本文仅仅简单介绍了如何在ubuntu20.04上安装tensorflow2.0以上的版本