引子:用笔记本折腾机器学习2个多月,是时候正儿八经的攒个机弄个环境了……于是就有了下面无穷无尽的折腾,期间查询了无数的先人文档案例,做了各种尝试……不得不说,到处都是坑……
硬件环境:
Intel 7700K + Z270 + Mem 16G + GTX 1080 + HD 2TB, 500w AP
软件环境:
Ubuntu 16.04 + NVidia-384 + CUDA 8.0 + cuDNN 6
Ubuntu 16.04
下载介质:https://www.ubuntu.com/download/desktop
1、分区:初学者,默认……高级班可以考虑引入grub2多分区,这里是一个坑
2、更新高速apt-get:
vi /etc/apt/sources.list (源如下)
sudo apt-get update
sudo apt-get upgrade
# 163.com
deb http://mirrors.163.com/ubuntu/ zesty main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ zesty main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ zesty-security main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ zesty-security main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ zesty-updates main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ zesty-updates main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ zesty-proposed main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ zesty-proposed main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ zesty-backports main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ zesty-backports main restricted universe multiverse
# 中科大
deb http://mirrors.ustc.edu.cn/ubuntu/ precise-updates main restricted
deb-src http://mirrors.ustc.edu.cn/ubuntu/ precise-updates main restricted
deb http://mirrors.ustc.edu.cn/ubuntu/ precise universe
deb-src http://mirrors.ustc.edu.cn/ubuntu/ precise universe
deb http://mirrors.ustc.edu.cn/ubuntu/ precise-updates universe
deb-src http://mirrors.ustc.edu.cn/ubuntu/ precise-updates universe
deb http://mirrors.ustc.edu.cn/ubuntu/ precise multiverse
deb-src http://mirrors.ustc.edu.cn/ubuntu/ precise multiverse
deb http://mirrors.ustc.edu.cn/ubuntu/ precise-updates multiverse
deb-src http://mirrors.ustc.edu.cn/ubuntu/ precise-updates multiverse
deb http://mirrors.ustc.edu.cn/ubuntu/ precise-backports main restricted universe multiverse
deb-src http://mirrors.ustc.edu.cn/ubuntu/ precise-backports main restricted universe multiverse
# ubuntu.com
deb http://security.ubuntu.com/ubuntu precise-security main restricted
deb-src http://security.ubuntu.com/ubuntu precise-security main restricted
deb http://security.ubuntu.com/ubuntu precise-security universe
deb-src http://security.ubuntu.com/ubuntu precise-security universe
deb http://security.ubuntu.com/ubuntu precise-security multiverse
deb-src http://security.ubuntu.com/ubuntu precise-security multiverse
# sohu
deb http://mirrors.sohu.com/ubuntu/ precise-updates main restricted
deb-src http://mirrors.sohu.com/ubuntu/ precise-updates main restricted
deb http://mirrors.sohu.com/ubuntu/ precise universe
deb-src http://mirrors.sohu.com/ubuntu/ precise universe
deb http://mirrors.sohu.com/ubuntu/ precise-updates universe
deb-src http://mirrors.sohu.com/ubuntu/ precise-updates universe
deb http://mirrors.sohu.com/ubuntu/ precise multiverse
deb-src http://mirrors.sohu.com/ubuntu/ precise multiverse
deb http://mirrors.sohu.com/ubuntu/ precise-updates multiverse
deb-src http://mirrors.sohu.com/ubuntu/ precise-updates multiverse
deb http://mirrors.sohu.com/ubuntu/ precise-backports main restricted universe multiverse
deb-src http://mirrors.sohu.com/ubuntu/ precise-backports main restricted universe multiverse
3、优化pip配置(加速下载)
$vi ~/.config/pip/pip.conf
增加内容
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
4、安装NVIDIA显卡驱动
NVIDIA显卡驱动安装说明:http://us.download.nvidia.com/XFree86/Linux-x86_64/384.90/README/index.html
禁用自带的nouveau显卡驱动
$sudo gedit /etc/modprobe.d/blacklist.conf
增加内容
blacklist nouveau
options nouveau modeset=0
$sudo update-initramfs -u
验证nouveau被屏蔽
$lsmod | grep nouveau
-----------------------------------------------------------------------------------------------
【专题】修改runlevel
NVidia官方安装,要求在驱动安装前,将runlevel设置到3,默认图像模式是5
Mapping between runlevels and systemd targets
┌─────────┬───────────────────┐
│Runlevel │ Target │
├─────────┼───────────────────┤
│0 │ poweroff.target │
├─────────┼───────────────────┤
│1 │ rescue.target │
├─────────┼───────────────────┤
│2, 3, 4 │ multi-user.target │
├─────────┼───────────────────┤
│5 │ graphical.target │
├─────────┼───────────────────┤
│6 │ reboot.target │
└─────────┴───────────────────┘
临时调整runlevel:
$sudo systemctl isolate multi-user.target
or
$sudo systemctl isolate runlevel3.target
永久性调整runlevel
$sudo systemctl enable multi-user.target
$sudo systemctl set-default multi-user.target
-----------------------------------------------------------------------------------------------
1)local run安装(建议方式)
www.nvidia.com,找到对应平台的显卡驱动,如NVIDA-Linux-x86_64-384.90.run
图形界面下按CTRL+ALT+F1,切换到TTY1,关闭图形界面(CTRL+ALT+F7,可以切换到图形的TTY7)
$sudo service lightdm stop
原来用runfile安装的话
$sudo sh NVIDIA-Linux-x86_64-384.90.run --uninstall
安装驱动
$sudo sh NVIDA-Linux-x86_64-384.90.run -no-opengl-files
(因为NVIDIA的驱动默认会安装OpenGL,而Ubuntu的内核本身也有OpenGL、且与GUI显示息息相关,一旦NVIDIA的驱动覆写了OpenGL,在GUI需要动态链接OpenGL库的时候就引起问题) ,
这里是一个坑
重启图形界面
$sudo service lightdm start
2) PPA源
Reference for linux nvidia driver installation: https://www.linuxhint.com/install-nvidia-drivers-linux/
添加PPA源,更新apt-get
$sudo add-apt-repository ppa:graphics-drivers/ppa
$sudo add-apt-repository ppa:xorg-edgers/ppa
$sudo apt-get update
需要先卸载老驱动
原来用apt-get安装的话
$sudo apt-get remove --purge nvidia*
安装驱动
$sudo apt-get install nvidia-384 nvidia-settings
重启
$reboot
1)or 2)后
确认nvidia安装,查看显卡驱动版本
$cat /proc/driver/nvidia/version
or
$sudo nvidia-smi
UI模式下
$sudo nvidia-settings
or
$lsmod | grep nvidia
$lsmod | grep nouveau
-------------------------------------------------------------------------------------------
【专题】调整最佳分辨率
参考文档: http://blog.csdn.net/nostandby/article/details/69383543
我安装后没有出现分辨率问题,备查
查询modeline
$cvt 1920 1080
将显示参数加入到/etc/X11/xorg.conf文件,将此模式保存为默认分辨率。
$sudo gedit /etc/X11/xorg.conf
增加内容:
Section "Monitor"
Identifier "Configured Monitor"
Modeline "1920x1080_60.00" 173.00 1920 2048 2248 2576 1080 1083 1088 1120 -hsync +vsync
Option "PreferredMode" "1920x1080_60.00"
EndSection
Section "Screen"
Identifier "Default Screen"
Monitor "Configured Monitor"
Device "Configured Video Device"
EndSection
Section "Device"
Identifier "Configured Video Device"
EndSection
重启
$reboot
-------------------------------------------------------------------------------------------
5、CUDA 8安装
NVIDIA CUDA下载: https://developer.nvidia.com/cuda-downloads
多阅读官方安装文档(用.run模式,慎用deb),老版本在legacy链接中
老CUDA卸载(#.#为老版本)
$sudo /usr/local/cuda-#.#/bin/uninstall_cuda_#.#.pl
安装CUDA
显卡驱动:建议独立安装,不要用CUDA所带的驱动,无数坑
$sudo sh cuda_8,0.44_linux.run --no-opengl-libs --silent --toolkit
安装后如遇到Imcomplete的警告,查看下安装日志,验证下CUDA的sample是否可以正常执行,
一般可以忽略。这里也是一个大坑
添加运行环境
$export PATH="$PATH:/usr/local/cuda-8.0/bin"
$export LD_LIBRARY_PATH="/usr/local/cuda-8.0/lib64"
6、cuDNN 6安装
cuDNN: https://developer.nvidia.com/rdp/cudnn-download
安装简单,其实就是把lib释放到cuda的lib中,直接就可被引用
$tar xvzf cudnn-8.0-linux-x64-v6.0.tgz
$sudo cp cuda/include/cudnn.h /usr/local/cuda/include
$sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64
$sudo chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn*