Ubuntu server 18.04 安装cuda9.0 和 cudnn 7.4.1

实验室的服务器买来好久了,中途碰上了期末考试,刚考完试老师就让我们作年度总结,然后实验室又开始装修,直到最近一周才将服务器的基本环境配置好。目前只是按照各位博客大神的说法在管理员用户下可以测试成功,可能后续还会有各种改进。

得感谢师兄们的帮忙和各位博客大神的指导。配置环境期间参考了很多大神的经验,下面会列举一些参考的博客。
(https://blog.csdn.net/breeze5428/article/details/80013753)
(https://blog.csdn.net/qq_30163461/article/details/80314630)
(https://blog.csdn.net/xrinosvip/article/details/81177873)
(https://blog.csdn.net/darren817/article/details/75174679)
(https://blog.csdn.net/qq_34877350/article/details/81182022)
(https://blog.csdn.net/wanzhen4330/article/details/81699769)
Ubuntu server 18.04 基础环境的配置主要就是安装cuda和cudnn,我分为3个部分来安装:

  1. 安装nvidia显卡驱动
  2. 安装cuda9.0
  3. 安装cudnn

一 安装NVIDIA显卡驱动

安装显卡驱动有很多方法,很多方法都是Ubuntu桌面版的。下面带删除线的是我入得一些坑,或许也不能算是坑,也许在服务器版本下不起作用而在桌面系统下就可以生效。

  1. 卸载旧版本或安装失败的nvidia驱动

    sudo apt-get purge nvidia*

有的会在nvidia后面加一个杠,我个人觉得都一样 sudo apt-get purge nvidia-*

  1. 把显卡驱动加入PPA
    add-apt-repository ppa:graphics-drivers
    sudo apt-get update
  2. 查看Ubuntu推荐的驱动版本
    ubuntu-drivers devices

会有如下结果
在这里插入图片描述
4. 采用apt-get在终端安装指定版本

sudo apt-get install nvidia-415

或者自动安装系统推荐版本

sudo ubuntu-drivers autoinstall
  1. 另外一种方法就是去nvidia官网(https://www.geforce.cn/drivers)下载对应的显卡驱动(注意显卡驱动和操作系统的选择)
    这种方法需要手动禁止nouveau驱动,比如会遇到下图这种情况
    在这里插入图片描述
  • [ a] 查看属性sudo ls -lh /etc/modprobe.d/blacklist.conf
  • [ b] 修改属性sudo chmod 666 /etc/modprobe.d/blacklist.conf
  • [ c] 打开文件: sudo vim /etc/modprobe.d/blacklist.conf
  • [ d] 在文件后面添加:
    blacklist vga16fb
    blacklist nouveau
    blacklist rivafb
    blacklist rivatv
    blacklist nvidiafb
  • [ e] 执行sudo update-initramfs -u
  • [ f] 重启后执行lsmod | grep nouveau
  • [g ] 没有输出即为屏蔽好了
  1. 很遗憾,我用上面的方法怎么都屏蔽不了,于是在师兄的建议下我去查看nvidia驱动程序的readme(http://download.nvidia.com/XFree86/Linux-x86/352.21/README/index.html),在第八章,我找到了解决方法:
    How do I prevent Nouveau from loading and performing a kernel modeset?
    A simple way to prevent Nouveau from loading and performing a kernel modeset is to add configuration directives for the module loader to a file in one of the system’s module loader configuration directories: for example, /etc/modprobe.d/ or /usr/local/modprobe.d. These configuration directives can technically be added to any file in these directories, but many of the existing files in these directories are provided and maintained by your distributor, which may from time to time provide updated configuration files which could conflict with your changes. Therefore, it is recommended to create a new file, for example, /etc/modprobe.d/disable-nouveau.conf, rather than editing one of the existing files, such as the popular /etc/modprobe.d/blacklist.conf. Note that some module loaders will only look for configuration directives in files whose names end with .conf, so if you are creating a new file, make sure its name ends with .conf.

Whether you choose to create a new file or edit an existing one, the following two lines will need to be added:

blacklist nouveau
options nouveau modeset=0
The first line will prevent Nouveau’s kernel module from loading automatically at boot. It will not prevent manual loading of the module, and it will not prevent the X server from loading the kernel module; see “How do I prevent the X server from loading Nouveau?” below. The second line will prevent Nouveau from doing a kernel modeset. Without the kernel modeset, it is possible to unload Nouveau’s kernel module, in the event that it is accidentally or intentionally loaded.

You will need to reboot your system after adding these configuration directives in order for them to take effect.

If nvidia-installer detects Nouveau is in use by the system, it will offer to create such a modprobe configuration file to disable Nouveau.

看着内容比较多,其实只需要执行下面几步:

  • 创建新文件(不使用旧文件的理由可以在上面英文中找到): sudo vim /etc/modprobe.d/disable-nouveau.conf
  • 在打开的文件中添加下面两行:
    blacklist nouveau
    options nouveau modeset=0
  • 重启系统就可以禁用成功
  1. 使用cd 命令转移到下载的驱动目录下,
    sudo chmod a+x NVIDIA-Linux-x86_64-415.13.run
    sudo ./NVIDIA-Linux-x86_64-384.130.run
    在安装过程中,系统又提示我的gcc、g++ 版本不对,在这里插入图片描述
    我系统的gcc版本是4.8.5的,显卡驱动要求是7.3.0的,于是更换gcc、g++版本:
  • 通过下面两条指令来查看版本
    ls /usr/bin/gcc*
    ls /usr/bin/g++*
  • 具体可以参考这篇博客(https://blog.csdn.net/xrinosvip/article/details/81177873)
  1. 完成安装后重启即可。
  2. 使用nvidia-smi即可查看显卡驱动(Ubuntu server 端没有图形界面,使用nvidia-settings会报错)
    在这里插入图片描述

二 安装cuda 9.0

  1. 安装依赖包
sudo apt-get install build-essential pkg-config linux-headers-$(uname -r)

或者下面的方法也可以(用了上面的语句下面的语句就不需要了):

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
sudo apt-get install libopenblas-dev liblapack-dev libatlas-base-dev
sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev
  1. 去cuda官网(https://developer.nvidia.com/cuda-downloads)下载安装包,我下载的是.run文件去cuda官网(https://developer.nvidia.com/cuda-downloads)下载安装包,我下载的是.run文件
  2. 使用命令使用命令
    sudo sh cuda****.run进行安装。
  3. 再次报错,cuda 9.0 需要gcc、g++ 6的版本,安装gcc-6:
sudo apt-get update
sudo apt-get install build-essential software-properties-common -y
sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y
sudo apt-get update
sudo apt-get install gcc-snapshot -y
sudo apt-get update
sudo apt-get install gcc-6 g++-6 -y

参考博客(https://blog.csdn.net/xrinosvip/article/details/81177873)进行版本切换
5. sudo sh cuda****.run进行安装
6. 配置环境变量
echo 'export PATH=/usr/local/cuda/bin:$PATH' >> ~/.bashrc
echo 'export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH' >> ~/.bashrc
source ~/.bashrc
环境变量也有可能是这个,我有点记不太清了
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda-9.0/lib64
export PATH=$PATH:/usr/local/cuda-9.0/bin
export CUDA_HOME=$CUDA_HOME:/usr/local/cuda-9.0

  1. 检验cuda是否安装成功
    nvcc -V
    在这里插入图片描述

三 安装cudnn

注意cuda对应的cudnn版本

  1. 去官网下载cudnn(可能需要进行注册nvidia账号),我下载的是cuDNN library for Linux
  2. 解压下载的文件,可以看到cuda文件夹,在当前目录打开终端,执行如下命令(目标文件的路径可能不对,需要更改,有的是cuda-9.0,根据具体情况自己修改就好了)
    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
    sudo chmod a+r /usr/local/cuda/lib64/libcudnn*
  3. 查看cudnn版本
    cat /usr/local/cuda/include/cudnn.h | grep CUDNN_MAJOR -A 2
    在这里插入图片描述

总结

  1. 虽然各种博客大神的方法很好,有时候还需要查看官方文档
  2. 安装软件版本对应十分重要

如果文章有什么不足,欢迎各位批评指正。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值