Ubuntu16.04 GPU环境配置(新手)
1. 首先将过时的软件依赖项更新一下
$ sudo apt-get update
$ sudo apt-get upgrade
安装cuda和cudnn
一、添加镜像源
$ sudo gedit /etc/apt/sources.list
将下面语句复制到文件末尾,保存,退出
#默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted universe multiverse
#deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse
#deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse
#deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security main restricted universe multiverse
#deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security main restricted universe multiverse
二、下载官方驱动
(1)首先去官网上查看适合你GPU的驱动。
例如:
NVIDIA http://www.nvidia.com/Download/index.aspx?lang=en-us或者网页搜索Download Drivers | NVIDIA
(2)Cuda https://developer.nvidia.com/cuda-downloads
(3)Cudnn https://developer.nvidia.com/cudnn
三、禁止集成的nouveau驱动
(1)打开终端,先删除旧的驱动:
sudo apt-get purge nvidia*
(2)禁用自带的 nouveau nvidia驱动 (important!)
创建一个文件通过命令 sudo vim /etc/modprobe.d/blacklist-nouveau.conf
并添加如下内容:
blacklist nouveau
options nouveau modeset=0
再更新一下
sudo update-initramfs -u
修改后需要重启系统。确认下Nouveau是已经被你干掉,使用命令:
lsmod | grep nouveau
(3)开始安装
先按Ctrl + Alt + F1到控制台,关闭当前图形环境
$sudo service lightdm stop
再安装驱动程序
$cd Download
$sudo ./NVIDIA-Linux-x86_64-xxx.run --no-opengl-files
$sudo service lightdm start
重启
$sudo reboot
四、安装CUDA
(1) 更改文件模式:
$chmod u+x cuda_8.0.44_linux.run
$sudo -s
$./ cuda_8.0.44_linux.run
安装界面:
漫长的读文件过程:可以按Ctrl+c直接跳过;
Do you accept the previously read EULA?
accept/decline/quit: accept
Install NVIDIA Accelerated Graphics Driver for Linux-x86_64 361.62?
(y)es/(n)o/(q)uit: n
Install the CUDA 8.0 Toolkit?
(y)es/(n)o/(q)uit: y
Enter Toolkit Location
[ default is /usr/local/cuda-8.0 ]:
Do you want to install a symbolic link at /usr/local/cuda?
(y)es/(n)o/(q)uit: y
Install the CUDA 8.0 Samples?
(y)es/(n)o/(q)uit: y
Enter CUDA Samples Location
[ default is /home/zhou ]:
Installing the CUDA Toolkit in /usr/local/cuda-8.0 …
Missing recommended library: libGLU.so
Missing recommended library: libX11.so
Missing recommended library: libXi.so
Missing recommended library: libXmu.so
Installing the CUDA Samples in /home/zhou …
Copying samples to /home/zhou/NVIDIA_CUDA-8.0_Samples now…
Driver: Not Selected
Toolkit: Installed in /usr/local/cuda-8.0
Samples: Installed in /home/zhou, but missing recommended libraries
Please make sure that
- PATH includes /usr/local/cuda-8.0/bin
- LD_LIBRARY_PATH includes /usr/local/cuda-8.0/lib64, or, add /usr/local/cuda-8.0/lib64 to /etc/ld.so.conf and run ldconfig as root
To uninstall the CUDA Toolkit, run the uninstall script in /usr/local/cuda-8.0/bin
Please see CUDA_Installation_Guide_Linux.pdf in /usr/local/cuda-8.0/doc/pdf for detailed information on setting up CUDA.
***WARNING: Incomplete installation! This installation did not install the CUDA Driver. A driver of version at least 361.00 is required for CUDA 8.0 functionality to work.
To install the driver using this installer, run the following command, replacing with the name of this run file:
sudo .run -silent -driver
Logfile is /tmp/cuda_install_2961.log
安装完成,但是缺少一些库
(2)安装所依赖的库
sudo apt-get install freeglut3-dev build-essential libx11-dev libxmu-dev libxi-dev libglu1-mesa libglu1-mesa-dev
sudo aptitude install libgl1-mesa-glx
(http://blog.sina.com.cn/s/blog_6792e87c0100s44d.html 关于aptitude说明)
(3)设置环境变量
cd /home/username
gedit ~/.bashrc
写入:
export PATH=/usr/local/cuda-8.0/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda-8.0/lib64:$LD_LIBRARY_PATH
保存并退出
cd /etc/ld.so.conf.d
sudo gedit cuda.conf
写入:
/usr/local/cuda-8.0/lib64
保存并退出后
sudo ldconfig
重启
$sudo reboot
(4)后续验证
- 测试cuda的samples
cd /home/name/NVIDIA_CUDA-8.0_Samples
make - 验证
$ cd /home/name/ NVIDIA_CUDA-8.0_Samples /1_Utilities/deviceQuery
$ sudo ./deviceQuery
运行之后会显示信息,最后一行显示pass说明成功
五、安装cudnn
$ cd ~/Downloads/
$ tar xvf cudnn*.tgz
$ cd cuda
$ sudo cp /.h /usr/local/cuda/include/
$ sudo cp /libcudnn /usr/local/cuda/lib64/
$ sudo chmod a+r /usr/local/cuda/lib64/libcudnn*
至此,GPU环境配置完成,之后就可以安装需要的深度框架Tensorflow, pytorch等。
六、安装anaconda
首先下载anaconda 安装包。
$ bash Anaconda**.sh
$ source /.bashrc
$ which python
七、安装Tensorflow
首先创建虚拟环境,会给之后减少很多不必要麻烦
$ conda create -n tf-gpu(此处是用GPU版本的,CPU类似)
$ source activate tf-gpu
(如果退出虚拟环境,可以 $ source deactivate tf-gpu)
$ conda install tensorflow-gpu=1.12(此处根据系统选择好版本)
最后可以在终端
$ python3
import tensorflow as tf
如果没有报错,说明已经安装成功(注意:可能由于网络原因不成功,多试几次就好了)。