Ubuntu 20.04 安装Tensorflow GPU版本 (GTX-1060显卡)
参考:
- https://cyfeng.science/2020/05/02/ubuntu-install-nvidia-driver-cuda-cudnn-suits/
- https://www.bilibili.com/read/cv9164113
- (cuda 驱动对照表) https://blog.csdn.net/qq_38502918/article/details/108009692
一. NVIDIA 驱动安装
ubuntu-drivers devices
(查看显卡型号)
- 自动安装推荐
ubuntu-drivers autoinstall
- reboot 重新启动 确认安装是否成功
nvidia-smi
推荐的版本CUDA 版本为 11.4 - PS: 如果遇到循环登录问题(Ubuntu 20.04自动安装没有遇到这类问题)
如果遇到循环登录,开机后选择Ubuntu(Advanced)引导,在root权限下删除所有NVIDIA驱动相关软件后,仅用开源noueau驱动,下载并使用runfile进行local安装
sudo sh NVIDIA-Linux-x86_64-440.64.run -no-x-check -no-nouveau-check -no-opengl-files
二、CUDA安装
- 下载到本地 (CUDA 11.4)
wget https://developer.download.nvidia.com/compute/cuda/11.4.2/local_installers/cuda_11.4.2_470.57.02_linux.run
- 运行runfile
sudo sh cuda_11.4.2_470.57.02_linux.run
- 若出现 (Existing package manager installation of the driver found. It is strongly recommended that you remove this before continuing.) 选择Continue
- 在安装时,因为已经安装过驱动,所以要把driver前面的×取消掉
- CUDA10.1支持gcc 7.3.0 但是20.04 自带gcc 9.3.0 可以通过添加override跳过gcc版本检查
sudo sh cuda_10.1.243_418.87.00_linux.run --override
- 配置环境变量:(也可以修改系统环境变量 /etc/profile)
sudo gedit ~/.bashrc
sudo vim .bashrc
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda-11.4/lib64
export PATH=$PATH:/usr/local/cuda-11.4/bin
export CUDA_HOME=/usr/local/cuda-11.4
- 运行环境
source ~/.bashrc
orsource /etc/profile
- 测试CUDA 是否安装成功
nvcc -V
三、cuDNN安装
- 选择cuDNN V 8.2.2(July 6th, 2021) for CUDA 11.4 - cuDNN Library for Linux. (与CUDA 11.4版本相对应的cuDNN)
Link: https://developer.nvidia.com/rdp/cudnn-archive (需要注册) - 下载对应版本解压后进行
sudo cp cuda/include/cudnn.h /usr/local/cuda-11.4/include
sudo cp cuda/lib64/libcudnn* /usr/local/cuda-11.4/lib64
sudo chmod a+r /usr/local/cuda-11.4/include/cudnn.h /usr/local/cuda-10.1/lib64/libcudnn*
四、TensorFlow及Pytorch安装
- TensorFlow 2.x 版本中CPU和GPU是合并在一起的
pip install tensorflow
- 查看TensorFlow版本
ipython
import tensorflow as tf
tf.__version__
In [3]: tf.__version__
Out[3]: '2.5.0'
- Pytorch(下载CUDA11.4对应的版本)
pip install torch==1.7.1+cu110 torchvision==0.8.2+cu110 torchaudio===0.7.2 -f https://download.pytorch.org/whl/torch_stable.html
五、测试TensorFlow - GPU 安装
ipython
import tensorflow as tf
tf.config.list_physical_devices('GPU')