一、nvidia驱动安装
- 确保linux内核为4.8.x
a: 查看内核版本:uname -a
b: 查询系统中装了多少内核: dpkg --get-selections|grep linux
c: 多余需要删除header和image
sudo apt-get remove linux-image-*.*.*-**(*号用你想删除的实际情况改写)
sudo apt-get remove linux-headers-*.*.*-**(*号用你想删除的实际情况改写)
d: 查看内核是否都删除干净: dpkg --get-selections|grep linux
e: 内核后面显示deinstall 的删除命令为:
dpkg --get-selections | grep deinstall | sed 's/deinstall/\lpurge/' | sudo dpkg --set-selections; sudo dpkg -Pa
2. 驱动安装
2.1 方法一
a: 卸御原驱动
sudo apt-get remove --purge nvidia*
sudo apt-get autoremove
sudo apt-get autoclean
b: 安装新驱动
chmod 777 NVIDIA-Linux-x86_64-387.34.run
sudo ./NVIDIA-Linux-x86_64-387.34.run -no-x-check -no-nouveau-check -no-opengl-files
c: 验证是否安装成功: nvidia-smi
2.2 方法二
a: 卸御原驱动:sudo ./XXX.run --uninstall -silent
b: 安装新驱动
sudo ./NVIDIA-Linux-x86_64-387.34.run -no-x-check -no-nouveau-check -no-opengl-files
c: 获得显卡OpenGL版本信息: glxinfo | grep OpenGL
二、cuda安装
1: 验证包是否完整:md5sum cuda_9.1.85_387.26_linux.run
2: 装依赖库:sudo apt-get install linux-headers-$(uname -r)
3: 禁用nouveau服务
lsmod | grep nouveau
sudo touch blacklist-nouveau.conf
sudo chmod a+w+r blacklist-nouveau.conf
vi blacklist-nouveau.conf(修改配置)
sudo update-initramfs -u
lsmod | grep nouveau
reboot
4: 进入字符界面,关闭图像界面:sudo service lightdm stop
5: 安装
sudo chmod a+x cuda_9.1.85_387.26_linux.run
sudo ./cuda_9.1.85_387.26_linux.run
gedit ~/.bashrc(添加PATH,LD_LIBRARY_PATH)
6: 启动图像界面
sudo service lightdm start
7: 验证安装是否成功:nvcc -V
三、cudnn安装
- 解压cudnn
cp cudnn-9.1-linux-x64-v7.1\ \(2\).solitairetheme8 cudnn-9.1-linux-x64-v7.1.tgz
tar -xvf cudnn-9.1-linux-x64-v7.1.tgz
- cd进入cudnn7.1解压之后的include目录,在命令行进行如下操作:
sudo cp cudnn.h /usr/local/cuda/include/ #复制头文件
- 再将cd进入lib64目录下的动态文件进行复制和链接:
sudo cp lib* /usr/local/cuda/lib64/ #复制动态链接库
cd /usr/local/cuda/lib64/
sudo rm -rf libcudnn.so libcudnn.so.7#删除原有动态文件
sudo ln -s libcudnn.so.7.1.2 libcudnn.so.7#生成软衔接
sudo ln -s libcudnn.so.7 libcudnn.so#生成软链接
四、Opencv3.4.1安装
- 安装依赖库:
$ sudo apt-get install build-essential
$ sudo apt-get install cmakegit libgtk2.0-dev pkg-configlibavcodec-dev libavformat-dev libswscale-dev
$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev liblapacke-dev
Python 支持
sudo apt-get install python2.7-dev
sudo apt-get install python-dev python-numpy libtbb2 libtbb-devlibjpeg-devlibpng-devlibtiff-devlibjasper-dev libdc1394-22-dev
$ sudo apt-get install libxvidcore-dev libx264-dev # 处理视频所需的包
$ sudo apt-get install libatlas-base-dev gfortran # 优化opencv功能
$ sudo apt-get install ffmpeg
GStreamer支持(推荐)(根据具体需求选择行安装)
sudo apt-get install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev
OpenGL支持(根据具体需求选择行安装)
sudo apt-get install libgtkglext1 libgtkglext1-dev
sudo apt-get install qtbase5-dev
video4linux2支持(更好地处理usb相机模式)(根据具体需求选择行安装)
sudo apt-get install libv4l-dev v4l-utils qv4l2 v4l2ucp
- 编译
Mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..
make -j4
sudo make install
sudo make install 执行完毕后OpenCV编译过程就结束了,接下来就需要配置一些OpenCV的编译环境首先将OpenCV的库添加到路径,从而可以让系统找到
sudogedit /etc/ld.so.conf.d/opencv.conf
执行此命令后打开的可能是一个空白的文件,不用管,只需要在文件末尾添加
/usr/local/lib
执行如下命令使得刚才的配置路径生效
sudo ldconfig
- 查询是否安装成功
python -c "import cv2; print cv2.__version__"
五、caffe配置
- 安装库及各种依赖
安装一般依赖项:
sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler
sudo apt-get install --no-install-recommends libboost-all-dev
再安装BLAS依赖项
sudo apt-get install libatlas-base-dev
然后是python和其余依赖项,安装python及其头文件
sudo apt-get install python
sudo apt-get install python-dev
安装python的其他依赖
sudo apt-get install python-numpy
sudo apt-get install ipython
sudo apt-get install ipython-notebook
sudo apt-get install python-sklearn
sudo apt-get install python-skimage
sudo apt-get install python-protobuf
sudo apt-get install python-numpyipythonipython-notebook python-sklearn python-skimage python-protobuf
然后是谷歌glog和gflags和lmdb依赖项
sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev
- hdf5库
cd /usr/lib/x86_64-linux-gnu
sudo ln -s libhdf5_serial.so.10.1.0 libhdf5.so
sudo ln -s libhdf5_serial_hl.so.10.0.2 libhdf5_hl.so
- 修改Makfile.config
解压caffe-master.zip
cdcaffe-master
sudocpMakefile.config.exampleMakefile.config
sudo vim Makefile.config
USE_CUDNN := 1 (可选)
USE_OPENCV := 1 (可选)
OPENCV_VERSION := 3 (如果打开了USE_OPENCV,而且opencv版本为3时)
PYTHON_INCLUDE := /usr/include/python2.7 /usr/lib/python2.7/dist-packages/numpy/core/include
WITH_PYTHON_LAYER := 1
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu/hdf5/serial /usr/local/share/OpenCV/3rdparty/lib
CUDA9.0以上版本删除下面两行内容
-gencode arch=compute_20,code=sm_20 \
-gencode arch=compute_20,code=sm_21 \
- 修改Makfile
将下面这一行:
NVCCFLAGS += -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
替换为:
NVCCFLAGS += -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
- 编译
make all -j6 -数字6为并行编译进程数目,一般设置为CPU物理核心数,可以使用 $(nproc) 来替换或echo $(nproc) 来查看CPU物理核心数
make test -j6
makeruntest -j6
make pycaffe -此时应该已经编译完成,再次执行以确认
如果上述步骤没有出现错误,但是无法导入caffe,执行下列操作
sudo vim ~/.bashrc
在文件最后写入如下内容:
export PYTHONPATH=/path/to/caffe/python:$PYTHONPATH
重新打开终端测试导入caffe
- 错误解决方法
- make test 出现下述错误
更改方式
- pip更新出错
- tensorflow安装
1: sudo apt-get update
2: sudo apt-get install python-pip
3: pip install --user https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.5.0-cp27-none-linux_x86_64.whl
4: pip install --user --upgrade tensorFlow
- pyTorch安装
7.1 方法一:pip安装
1: sudo apt-get install python3-pip
2: sudo pip3 install torch-0.3.1-cp35-cp35m-linux_x86_64.whl
3: sudo pip3 install torchvision
7.2 方法二:anaconda安装
1: sh Anaconda3-5.1.0-Linux-x86_64.sh
sudo vi ~/.bashrc
末尾处添加export PATH="/home/vm-01/anaconda3/bin:$PATH"
source ~/.bashrc
conda install pytorchtorchvision cuda91 -c pytorch
错误:
An HTTP error occurred when trying to retrieve this URL.
HTTP errors are often intermittent, and a simple retry will get you onyourway.
解决方法
condaconfig --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
condaconfig --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
condaconfig --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
condaconfig --set show_channel_urls yes
再次尝试安装命令
conda install pytorchtorchvision cuda91 -c pytorch
在终端中按以下顺序运行命令,不报错的话即代表安装成功:
python
import torch