Ubuntu 18.04 下 cuda + cudnn 安装和 conda 深度学习环境配置

2020/4/22 21:21:58

本文主要是介绍Ubuntu 18.04 下 cuda + cudnn 安装和 conda 深度学习环境配置,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1. NVIDIA 显卡驱动安装

1、首先设置 bios 禁用 secure boot 将其设置为 disable,一般是在开机尚未进入系统时按 F2 或者 F12 等等进入 bios,具体哪个按键看机器具体的情况。

2、如果之前安装过 NVIDIA 的驱动,卸载之前安装的显卡驱动:

$ sudo apt-get purge nvidia*

3、配置系统,禁用掉默认安装的 nouveau 开源驱动:

$ sudo gedit /etc/modprobe.d/blacklist.conf

在文件末尾添加:

blacklist nouveau

然后执行:

$ sudo update-initramfs -u

重启及其,然后 ctrl+alt+F1 进入文字终端,运行:

$ lsmod | grep nouveau

进行检查,如果无输出则说明成功禁用了 nouveau 开源驱动。

4、从 NVIDIA 官网 https://www.geforce.cn/drivers 查询并下载对应的显卡驱动,最好是直接放在 home 里方便后继操作。重启电脑 ctrl+alt+F1 进入文字终端,首先修改驱动文件的可执行权限并运行:

$ sudo chmod a+x NVIDIA-Linux-*.run
$ sudo ./NVIDIA-Linux-*.run –no-opengl-files

·后面的 –no-opengl-files 表示不安装 opengl。这个也可能不是必须的,但是有些时候不加这个可能会导致循环出现登录界面但就是登陆不进去。要是这样的话,重启进入文字终端重复上述过程。

5、重启,然后运行

$ nvidia-smi

检查是否安装成功,如果输出类似:

Sat Apr 18 23:37:46 2020       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 440.64       Driver Version: 440.64       CUDA Version: 10.2     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  GeForce GTX 166...  Off  | 00000000:01:00.0 Off |                  N/A |
| N/A   39C    P8     5W /  N/A |    299MiB /  5944MiB |     12%      Default |
+-------------------------------+----------------------+----------------------+
                                                                               
+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
|    0      2867      G   /usr/lib/xorg/Xorg                            45MiB |
|    0      6832      G   /usr/lib/xorg/Xorg                           171MiB |
|    0    174826      G   /usr/bin/gnome-shell                          69MiB |
|    0    675187      G   /usr/lib/firefox/firefox                       1MiB |
+-----------------------------------------------------------------------------+

就证明安装没什么问题了。

2. cuda 安装

1、去 cuda 官网 https://developer.nvidia.com/... 按照显卡型号和所需 cuda 版本下载安装包,注意第一次使用需要注册用户才能下载。官网推荐是直接用 wget 下然后再运行:

$ wget http://developer.download.nvidia.com/compute/cuda/10.2/Prod/local_installers/cuda_10.2.89_440.33.01_linux.run
$ sudo sh cuda_10.2.89_440.33.01_linux.run

更推荐把安装包直接下下来,以后说不定还会用上。

2、运行:

$ sudo chmod a+x cuda_*_linux.run
$ sudo ./cuda_*_linux.run

由于已经预先安装好了驱动,所以在安装过程中注意选择不去安装驱动。这时候往往会出现类似:

***WARNING: Incomplete installation! This installation did not install the CUDA Driver. A driver of version at least 384.00 is required for CUDA 10.0 functionality to work.

To install the driver using this installer, run the following command, replacing <CudaInstaller> with the name of this run file:

sudo <CudaInstaller>.run -silent -driver

的提醒,不用管它就好。实际上我也没弄清楚,在安装 cuda 前预先安装 NVIDIA 显卡驱动是不是必须的,但是好像大家都这么搞,也没有很多时间去试错那就按照大家成功的方式去作吧。

3、添加 cuda 的环境变量:

$ export CUDA_HOME=/usr/local/cuda 
$ export PATH=$PATH:$CUDA_HOME/bin 
$ export LD_LIBRARY_PATH=/usr/local/cuda-10.0/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}

注意添加前检查一下 cuda 是不是真的安装到了 LD_LIBRARY_PATH 里的 /usr/local/cuda-10.0/lib64 目录下。

4、尝试编译并运行 cuda 附带的一个例子:

$ cd /usr/local/cuda/samples/1_Utilities/deviceQuery 
$ sudo make
$ ./deviceQuery

运行结果类似:

./deviceQuery Starting...

 CUDA Device Query (Runtime API) version (CUDART static linking)

Detected 1 CUDA Capable device(s)

Device 0: "GeForce GTX 1660 Ti"
  CUDA Driver Version / Runtime Version          10.2 / 10.2
  CUDA Capability Major/Minor version number:    7.5
  Total amount of global memory:                 5945 MBytes (6233391104 bytes)
  (24) Multiprocessors, ( 64) CUDA Cores/MP:     1536 CUDA Cores
  GPU Max Clock rate:                            1590 MHz (1.59 GHz)
  Memory Clock rate:                             6001 Mhz
  Memory Bus Width:                              192-bit
  L2 Cache Size:                                 1572864 bytes
  Maximum Texture Dimension Size (x,y,z)         1D=(131072), 2D=(131072, 65536), 3D=(16384, 16384, 16384)
  Maximum Layered 1D Texture Size, (num) layers  1D=(32768), 2048 layers
  Maximum Layered 2D Texture Size, (num) layers  2D=(32768, 32768), 2048 layers
  Total amount of constant memory:               65536 bytes
  Total amount of shared memory per block:       49152 bytes
  Total number of registers available per block: 65536
  Warp size:                                     32
  Maximum number of threads per multiprocessor:  1024
  Maximum number of threads per block:           1024
  Max dimension size of a thread block (x,y,z): (1024, 1024, 64)
  Max dimension size of a grid size    (x,y,z): (2147483647, 65535, 65535)
  Maximum memory pitch:                          2147483647 bytes
  Texture alignment:                             512 bytes
  Concurrent copy and kernel execution:          Yes with 3 copy engine(s)
  Run time limit on kernels:                     Yes
  Integrated GPU sharing Host Memory:            No
  Support host page-locked memory mapping:       Yes
  Alignment requirement for Surfaces:            Yes
  Device has ECC support:                        Disabled
  Device supports Unified Addressing (UVA):      Yes
  Device supports Compute Preemption:            Yes
  Supports Cooperative Kernel Launch:            Yes
  Supports MultiDevice Co-op Kernel Launch:      Yes
  Device PCI Domain ID / Bus ID / location ID:   0 / 1 / 0
  Compute Mode:
     < Default (multiple host threads can use ::cudaSetDevice() with device simultaneously) >

deviceQuery, CUDA Driver = CUDART, CUDA Driver Version = 10.2, CUDA Runtime Version = 10.2, NumDevs = 1
Result = PASS

就说明安装没问题。

3. cudnn 的安装

1、从 cudnn 官网 https://developer.nvidia.com/... 上下载安装包,注意需要注册登录用户后才能下载,Ubuntu 的话下载 *.deb 格式的安装包就好。

2、直接 sudo 安装下载下来的 *.deb 格式安装包:

$ sudo dpkg -i libcudnn7*.deb

4. conda 的安装

1、我主要使用 conda 配置各种深度学习开发环境,anacondaminiconda 的安装包从清华 tuna 的镜像站下载:

  • anaconda: https://mirrors.tuna.tsinghua...
  • miniconda:https://mirrors.tuna.tsinghua...

直接命令行里运行下载的安装包即可,注意可以只为当前用户安装,所以不需要也不推荐用 sudo 运行:

sh Anaconda3-*.sh   # 二选一就行
sh Miniconda3-*.sh  # 推荐使用 miniconda

安装结束时会提示将 conda 加入环境变量里,这个还是必要的。如果不小心错过这个了,实际上手动修改 .bashrc 就行:

$ gedit .bashrc

然后添加以下类似内容(注意按照需要修改 conda 安装路径):

# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/home/aoyile/miniconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
    eval "$__conda_setup"
else
    if [ -f "/home/aoyile/miniconda3/etc/profile.d/conda.sh" ]; then
        . "/home/aoyile/miniconda3/etc/profile.d/conda.sh"
    else
        export PATH="/home/aoyile/miniconda3/bin:$PATH"
    fi
fi
unset __conda_setup
# <<< conda initialize <<<

2、由于大部分情况下从 tuna 上东西非常快,所以可以配置 conda 使用 tuna 的源。一般只需要修改 .condarc 文件就可以了:

$ gedit ~/.condarc

添加以下内容就行:

channels:
  - defaults
show_channel_urls: true
channel_alias: https://mirrors.tuna.tsinghua.edu.cn/anaconda
default_channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/pro
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
custom_channels:
  conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud

更多说明见 https://mirrors.tuna.tsinghua...。为了避免一打开终端就使用 conda 的虚拟环境带来好多麻烦,最好再添加一句:

auto_activate_base: false

5. 深度学习环境部署

需要说明的就一点:不要偷懒把包都装到默认的 base 环境里!因为 base 删不掉,一旦包与包版本冲突搞乱了就非常难处理,往往最终不得不删掉整个 conda 的安装来恢复。推荐别怕麻烦使用不同的环境部署不同的包。

tensorflow + keras

$ conda create -n keras python=3.7
$ conda activate keras
$ conda install ipython numpy scipy pandas 
$ conda install scikit-learn scikit-image 
$ conda install tensorflow-gpu keras-gpu
$ conda install opencv

注意非常邪门,有时候 opencv 装进去了会出一些问题,所以如果不需要就不要装了。

torch

$ conda create -n torch python=3.7
$ conda activate torch
$ conda install ipython numpy scipy pandas
$ conda install scikit-learn scikit-image
$ conda install pytorch torchvision

cpu 版的各种

$ conda create -n cpu python=3.7
$ conda activate cpu
$ conda install ipython numpy scipy pandas 
$ conda install scikit-learn scikit-image 
$ conda install opencv
$ conda install tensorflow keras pytorch-cpu torchvision


这篇关于Ubuntu 18.04 下 cuda + cudnn 安装和 conda 深度学习环境配置的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程