Ubuntu16.04下安装多版本cuda和cudnn
原文 https://blog.csdn.net/tunhuzhuang1836/article/details/79545625
前言
因为之前针对Pytorch,caffe,torch等,装了cuda8.0和对应cudnn5.1,但是最近在装MxNet的时候,发现官网上能下载到的MxNet版本仅支持cuda9.0和对应cudnn7.0.5,所以无奈不想卸载cuda8.0只能在电脑上安装多个版本的cuda和对应cudnn。
安装cuda
好了进入正题,首先安装cuda 需要去nvidia官网注册一个账号登录下载。这里给一下历史版本的cuda链接(现在官网好像进去默认下载最新版本)。这里我们举例下载cuda9.0:(当你电脑上只有一个cuda时候下载哪种类型无所谓,如果已存在一个或以上时候需要选择runfile安装)
安装按照官方指引就好
# if deb
sudo dpkg -i cuda-repo-ubuntu1604-9-0-local-rc_9.0.103-1_amd64.deb
sudo apt-get update
sudo apt-get install cuda
sudo dpkg -i cuda-repo-ubuntu1604-9-0-local-rc_9.0.103-1_amd64.deb
sudo apt-get update
sudo apt-get install cuda
# if runfile
sudo chmod +x cuda_9.0.176_384.81_linux.run
./cuda_9.0.176_384.81_linux.run12345678
sudo chmod +x cuda_9.0.176_384.81_linux.run
./cuda_9.0.176_384.81_linux.run12345678
这里我给了deb版本和runfile版本cuda安装。 如果你是第一遍装cuda,按官方教程什么版本都可以; 如果是电脑上已存在cuda,则安装需要runfile,且有几个需要注意的地方:(这里详情可参考Maple2014博客)
Do you accept the previously read EULA? (accept/decline/quit): accept
You are attempting to install on an unsupported configuration. Do you wish to continue? ((y)es/(n)o) [ default is no ]: y
Install NVIDIA Accelerated Graphics Driver for Linux-x86_64 346.46? ((y)es/(n)o/(q)uit): n
Do you want to install the OpenGL libraries? ((y)es/(n)o/(q)uit) [ default is yes ]: n
Install the CUDA 9.0 Toolkit? ((y)es/(n)o/(q)uit): y
Enter Toolkit Location [ default is /usr/local/cuda-9.0 ]:
/usr/local/cuda-9.0 is not writable.
Do you wish to run the installation with ‘sudo’? ((y)es/(n)o): y
Please enter your password:
Do you want to install a symbolic link at /usr/local/cuda? ((y)es/(n)o/(q)uit): n
Install the CUDA 9.0 Samples? ((y)es/(n)o/(q)uit): y
Enter CUDA Samples Location [ default is /home/xxx ]:
Installing the CUDA Toolkit in /usr/local/cuda-9.0 …
Installing the CUDA Samples in /home/xxx …
Copying samples to /home/xxx/NVIDIA_CUDA-9.0_Samples now…
Finished copying samples.12345678910111213141516
You are attempting to install on an unsupported configuration. Do you wish to continue? ((y)es/(n)o) [ default is no ]: y
Install NVIDIA Accelerated Graphics Driver for Linux-x86_64 346.46? ((y)es/(n)o/(q)uit): n
Do you want to install the OpenGL libraries? ((y)es/(n)o/(q)uit) [ default is yes ]: n
Install the CUDA 9.0 Toolkit? ((y)es/(n)o/(q)uit): y
Enter Toolkit Location [ default is /usr/local/cuda-9.0 ]:
/usr/local/cuda-9.0 is not writable.
Do you wish to run the installation with ‘sudo’? ((y)es/(n)o): y
Please enter your password:
Do you want to install a symbolic link at /usr/local/cuda? ((y)es/(n)o/(q)uit): n
Install the CUDA 9.0 Samples? ((y)es/(n)o/(q)uit): y
Enter CUDA Samples Location [ default is /home/xxx ]:
Installing the CUDA Toolkit in /usr/local/cuda-9.0 …
Installing the CUDA Samples in /home/xxx …
Copying samples to /home/xxx/NVIDIA_CUDA-9.0_Samples now…
Finished copying samples.12345678910111213141516
安装好后我们可以去/usr/local/下看到安装好的cuda-9.0文件夹:
安装cudnn
其次我们需要安装cudnn,同理去nvidia官网下载cudnn。这里我们安装的是cudnn7.0.5。
注意,我们这里选择cuda9.0下cuDNN v7.0.5 Library for Linux。下载好后直接命令行解压然后复制lib64和include文件夹到usr/local/cuda-9.0,命令如下:
# Installing from a Tar File
sudo cp cuda/include/cudnn.h /usr/local/cuda-9.0/include
sudo cp cuda/lib64/libcudnn* /usr/local/cuda-9.0/lib64
sudo chmod a+r /usr/local/cuda-9.0/include/cudnn.h /usr/local/cuda-9.0/lib64/libcudnn*1234
sudo cp cuda/include/cudnn.h /usr/local/cuda-9.0/include
sudo cp cuda/lib64/libcudnn* /usr/local/cuda-9.0/lib64
sudo chmod a+r /usr/local/cuda-9.0/include/cudnn.h /usr/local/cuda-9.0/lib64/libcudnn*1234
cuda版本的*切换
以上讲的就是多版本cuda的安装。最后,我们要做的就是cuda版本的*切换。命令行sudo gedit ~/.bashrc打开.bashrc,也许你已经装过cuda8.0,则.bashrc下应该有如下行:(详情请参考mumoDM博客)
export PATH="$PATH:/usr/local/cuda-8.0/bin"
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/cuda-8.0/lib64/"
export LIBRARY_PATH="$LIBRARY_PATH:/usr/local/cuda-8.0/lib64" 123
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/cuda-8.0/lib64/"
export LIBRARY_PATH="$LIBRARY_PATH:/usr/local/cuda-8.0/lib64" 123
我们将所有的cuda-8.0更改为cuda文件夹:
export PATH="$PATH:/usr/local/cuda/bin"
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/cuda/lib64/"
export LIBRARY_PATH="$LIBRARY_PATH:/usr/local/cuda/lib64" 123
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/cuda/lib64/"
export LIBRARY_PATH="$LIBRARY_PATH:/usr/local/cuda/lib64" 123
到这一步我们已经设置好了链接库,最后我们只需要在终端敲入:
sudo rm -rf /usr/local/cuda #删除之前创建的软链接
sudo ln -s /usr/local/cuda-8.0 /usr/local/cuda #创建新 cuda 的软链接12
sudo ln -s /usr/local/cuda-8.0 /usr/local/cuda #创建新 cuda 的软链接12
这里我是从cuda8.0切换到9.0 。同理反一下就可以切换回8.0 。所有完事后,可以用命令来查看cuda是否切换完成:
$ nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2017 NVIDIA Corporation
Built on Fri_Sep__1_21:08:03_CDT_2017
Cuda compilation tools, release 9.0, V9.0.17612345
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2017 NVIDIA Corporation
Built on Fri_Sep__1_21:08:03_CDT_2017
Cuda compilation tools, release 9.0, V9.0.17612345
总结
当然本人在安装过程中还是踩过好多坑的,举几个栗子说明。
(1) nvcc可能默认是你以前deb安装时候其他版本的,所以你会发现就算你操作按上面一步步来,nvcc –version结果还是没变。所以我们可以找到nvcc位置,然后创建usr/local/cuda/nvcc软链接到原先nvcc位置,应该是usr/bin/nvcc,操作为:
which nvcc # 找到nvcc位置,假定在/usr/bin/nvcc且是以前版本8.0
sudo ln -s /usr/local/cuda/bin/nvcc /usr/bin/nvcc # 创建软链接链接到当前cuda的nvcc
nvcc --version # 显示当前cuda版本123
sudo ln -s /usr/local/cuda/bin/nvcc /usr/bin/nvcc # 创建软链接链接到当前cuda的nvcc
nvcc --version # 显示当前cuda版本123
(2) 也许你切换到8.0跑Pytorch的时候发现报错了:
Traceback (most recent call last):
File "train.py", line 318, in <module>
main()
File "train.py", line 182, in main
out = model(inputs)
File "/usr/local/lib/python2.7/dist-packages/torch/nn/modules/module.py", line 206, in __call__
result = self.forward(*input, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/torch/nn/parallel/data_parallel.py", line 61, in forward
outputs = self.parallel_apply(replicas, inputs, kwargs)
File "/usr/local/lib/python2.7/dist-packages/torch/nn/parallel/data_parallel.py", line 71, in parallel_apply
return parallel_apply(replicas, inputs, kwargs)
File "/usr/local/lib/python2.7/dist-packages/torch/nn/parallel/parallel_apply.py", line 45, in parallel_apply
raise output
RuntimeError: CUDNN_STATUS_INTERNAL_ERROR1234567891011121314
File "train.py", line 318, in <module>
main()
File "train.py", line 182, in main
out = model(inputs)
File "/usr/local/lib/python2.7/dist-packages/torch/nn/modules/module.py", line 206, in __call__
result = self.forward(*input, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/torch/nn/parallel/data_parallel.py", line 61, in forward
outputs = self.parallel_apply(replicas, inputs, kwargs)
File "/usr/local/lib/python2.7/dist-packages/torch/nn/parallel/data_parallel.py", line 71, in parallel_apply
return parallel_apply(replicas, inputs, kwargs)
File "/usr/local/lib/python2.7/dist-packages/torch/nn/parallel/parallel_apply.py", line 45, in parallel_apply
raise output
RuntimeError: CUDNN_STATUS_INTERNAL_ERROR1234567891011121314
这个时候需要删除root下一个隐藏文件夹 .nv:
sudo rm -rf ~/.nv1
就是这么多,祝玩得开心!(附一张成功图:原8.0切换到9.0)
引用
http://blog.csdn.net/maple2014/article/details/78574275
http://blog.csdn.net/mumoDM/article/details/79462604
https://github.com/SeanNaren/deepspeech.pytorch/issues/32