ubuntu18.04中cuda11.0和cuda9.0多版本安装与切换
服务器中已经存在cuda11.0,现在需要用到cuda9.0
查看当前cuda版本:
~$ nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2020 NVIDIA Corporation
Built on Thu_Jun_11_22:26:38_PDT_2020
Cuda compilation tools, release 11.0, V11.0.194
Build cuda_11.0_bu.TC445_37.28540450_0
也可以通过nvidia-smi
查看
下载cuda9.0的安装包
这里是历史版本的下载地址
https://developer.nvidia.com/cuda-toolkit-archive
选择runfile
类型的安装文件
安装
下载完毕,得到文件cuda_9.0.176_384.81_linux.run
到对应目录下执行命令:
#先执行下面的命令安装相关依赖,
#否则会出现`Missing recommended library`错误
sudo apt-get install freeglut3-dev build-essential libx11-dev libxmu-dev libxi-dev libgl1-mesa-glx libglu1-mesa libglu1-mesa-dev
sudo sh cuda_9.0.176_384.81_linux.run #开始安装
#按q退出协议说明.
accept/decline/quit: accept #接受协议
Install NVIDIA Accelerated Graphics Driver for Linux-x86_64 375.26?
y)es/(n)o/(q)uit: n #是否显卡驱动包,由于已经安装显卡驱动,选择n
Install the CUDA 9.0 Toolkit?
(y)es/(n)o/(q)uit: y #是否安装工具包,选择y
Enter Toolkit Location
[ default is /usr/local/cuda-9.0 ]: #工具包安装地址
Do you want to install a symbolic link at /usr/local/cuda?
(y)es/(n)o/(q)uit: y #添加软链接,第一次安装cuda选yes,如果已经有cuda了就选no
Install the CUDA 9.0 Samples?
(y)es/(n)o/(q)uit: y #安装样例
Enter CUDA Samples Location
[ default is /root ]: #样例安装地址默认即可
如果出现“You are attempting to install on an unsupported configuration. Do you wish to continue?”,意味着可能是gcc的版本过高,后续安装会报错:
Error: unsupported compiler: 7.5.0. Use --override to override this check.
Error: cannot find Toolkit in /usr/local/cuda-9.0
===========
= Summary =
===========
Driver: Not Selected
Toolkit: Installation Failed. Using unsupported Compiler.
Samples: Cannot find Toolkit in /usr/local/cuda-9.0
问题出在gcc的版本过高,查看gcc的版本信息:
~$ gcc --version
gcc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
这时就需要安装更低版本的gcc(6.0),因为cuda9.0不支持gcc7
gcc降级
查看gcc文件夹中是否存在gcc6:
~$ ls /usr/bin/gcc*
/usr/bin/gcc /usr/bin/gcc-ar /usr/bin/gcc-nm /usr/bin/gcc-ranlib
/usr/bin/gcc-7 /usr/bin/gcc-ar-7 /usr/bin/gcc-nm-7 /usr/bin/gcc-ranlib-7
如果不存在,则需要安装gcc6:
sudo apt-get install gcc-6
安装后的gcc文件夹:
~$ ls /usr/bin/gcc*
/usr/bin/gcc /usr/bin/gcc-ar /usr/bin/gcc-nm /usr/bin/gcc-ranlib
/usr/bin/gcc-6 /usr/bin/gcc-ar-6 /usr/bin/gcc-nm-6 /usr/bin/gcc-ranlib-6
/usr/bin/gcc-7 /usr/bin/gcc-ar-7 /usr/bin/gcc-nm-7 /usr/bin/gcc-ranlib-7
gcc版本切换
修改版本优先级:
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-6 100
上述命令将gcc-6的优先级设定为100
查看并调整优先级:
sudo update-alternatives --config gcc
继续安装cuda
调整gcc版本后继续进行安装,步骤与之前相同,最后安装成功:
===========
= Summary =
===========
Driver: Not Selected
Toolkit: Installed in /usr/local/cuda-9.0
Samples: Installed in /home/songhui
Please make sure that
- PATH includes /usr/local/cuda-9.0/bin
- LD_LIBRARY_PATH includes /usr/local/cuda-9.0/lib64, or, add /usr/local/cuda-9.0/lib64 to /etc/ld.so.conf and run ldconfig as root
To uninstall the CUDA Toolkit, run the uninstall script in /usr/local/cuda-9.0/bin
Please see CUDA_Installation_Guide_Linux.pdf in /usr/local/cuda-9.0/doc/pdf for detailed information on setting up CUDA.
***WARNING: Incomplete installation! This installation did not install the CUDA Driver. A driver of version at least 384.00 is required for CUDA 9.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版本切换
安装完后可以在/usr/local/
目录下看到有多个版本的cuda存在:
~$ ls /usr/local/
bin cuda cuda-11.0 cuda-9.0 etc games include lib man sbin share src
正在使用的cuda-11.0
需要切换成cuda-9.0
,而cuda
是一个软链接,目前指向了cuda-11.0
,所以只要将cuda
指向cuda-9.0
就完成了切换。
用stat
命令查看cuda
文件夹属性:
~$ stat /usr/local/cuda
File: /usr/local/cuda -> /usr/local/cuda-11.0/
Size: 21 Blocks: 0 IO Block: 4096 symbolic link
Device: fd00h/64768d Inode: 8916387 Links: 1
Access: (0777/lrwxrwxrwx) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2021-09-22 14:40:15.444379761 +0800
Modify: 2020-11-19 10:37:24.010222584 +0800
Change: 2020-11-19 10:37:24.010222584 +0800
Birth: -
可以看到文件类型是symbolic link
软链接,指向了/usr/local/cuda-11.0/
所以把这个cuda
文件夹删掉再新建一个指向/usr/local/cuda-9.0/
的软链接就好了:
$ sudo rm -rf cuda
$ sudo ln -s /usr/local/cuda-9.0 /usr/local/cuda
如果没有修改成功,查看.bashrc
文件配置:
vim ~/.bashrc
在最后添加:
export PATH="/usr/local/cuda/bin:$PATH"
export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH