很多时候我们需要不同的环境,例如torch环境与tensorflow环境,但是他们所依赖的cuda和cudnn版本不一致,这就需要同时安装多个版本,使得他们共存,并工作。
tensorflow 1.11 + cuda9.0 ====================安装cuda9.0============== 由于CUDA 9.0仅支持GCC 6.0及以下版本,而Ubuntu 18.04预装GCC版本为7.3,故手动安装gcc-5与g++-5: sudo apt-get install gcc-5 sudo apt-get install g++-5 ? cd /usr/bin sudo rm gcc sudo ln -s gcc-5 gcc sudo rm g++ sudo ln -s g++-5 g++ ? gcc -v #查看版本 -------------------------------------------------- Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/5/lto-wrapper Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Ubuntu 5.5.0-12ubuntu1' --with-bugurl=file:///usr/share/doc/gcc-5/README.Bugs --enable-languages=c,ada,c++,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-5 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --enable-default-pie --with-system-zlib --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu Thread model: posix gcc version 5.5.0 20171010 (Ubuntu 5.5.0-12ubuntu1)
===================下载cuda9.0===================== 由于不支持ubuntu18.04则下载ubuntu16.04版本,下载cuda和四个补丁文件。(此处可以下载安装其他版本的cuda实现cuda共存) 下载完成后进行安装,cd到下载文件路径下先安装cuda主文件,再安装4个补丁文件。 sudo sh cuda_9.0.176_384.81_linux.run??? # 开始安装 ? Logging to /tmp/cuda_install_20700.log Using more to view the EULA. End User License Agreement -------------------------- ? Preface ------- ? 1. NVIDIA Software License Agreement ------------------------------------ ? ? Release Date: October 20, 2016 ------------------------------ ? ? IMPORTANT NOTICE -- READ BEFORE DOWNLOADING, INSTALLING, COPYING OR USING THE LICENSED SOFTWARE: -------------------------------------------------------- ? # 按 "q"键退出协议说明 ? ---------------------------------------------------------------------------------- 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 384.81? (y)es/(n)o/(q)uit: n??? # 由于已经安装显卡驱动,选择n,切记切记! ? Install the CUDA 9.0 Toolkit? (y)es/(n)o/(q)uit: 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: n??? #*因为本机已经安装了cuda10.1因此不要创建软链接,选择n,切记切记! ? Install the CUDA 9.0 Samples? (y)es/(n)o/(q)uit: y??? # 安装样例 ? Enter CUDA Samples Location ?[ default is /home/zzw ]: /data0/zzw/CUDA-9.0??? # 样例安装地址 ? Installing the CUDA Toolkit in /usr/local/cuda-9.0 ... Installing the CUDA Samples in /data0/zzw/CUDA-9.0 ... Copying samples to /data0/zzw/CUDA-9.0/NVIDIA_CUDA-9.0_Samples now... Finished copying samples. ? =========== = Summary = =========== ? Driver:?? Not Selected Toolkit:? Installed in /usr/local/cuda-9.0 Samples:? Installed in /data0/zzw/CUDA-9.0 ? 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 ? Logfile is /tmp/cuda_install_20700.log ? # ***安装完成***
配置CUDA-9.0的环境变量(此处将原始cuda10.1版本的PATH注释,添加cuda9.0PATH): sudo gedit ~/.bashrc #在bashrc末尾添加 export PATH="$PATH:/usr/local/cuda-9.0/bin" export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/cuda-9.0/lib64/" export LIBRARY_PATH="$LIBRARY_PATH:/usr/local/cuda-9.0/lib64" #source一下 source ~/.bashrc 安装四个补丁文件 sudo sh cuda_9.0.176.1_linux.run sudo sh cuda_9.0.176.2_linux.run sudo sh cuda_9.0.176.3_linux.run sudo sh cuda_9.0.176.4_linux.run
================下载cudnn7.6.0======================= tar -zxvf cudnn-9.0-linux-x64-v7.6.0.64.tgz $ 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* 多版本cuda和cudnn切换 多版本管理在完成cuda和cudnn安装基础上,只要进行环境变量更改和软链接创建即可。
========================================多版本cuda动态切换(举例)============================================================== 1、更改环境变量,将cuda-10.1变为cuda-9.0 sudo gedit ~/.bashrc
注释掉原来的cuda10.1版本的环境变量,替换为cuda9.0的环境变量 export PATH="$PATH:/usr/local/cuda-9.0/bin" export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/cuda-9.0/lib64/" export LIBRARY_PATH="$LIBRARY_PATH:/usr/local/cuda-9.0/lib64" 2、删除之前的软连接,并生成新的软连接 sudo rm -rf /usr/local/cuda #删除之前创建的软链接 sudo ln -s /usr/local/cuda-9.0 /usr/local/cuda #创建新 cuda 的软链接 重启Terminal后可以使用使用如下命令检查:
cat? /usr/local/cuda/version.txt #查看cuda版本
cat /usr/local/cuda/include/cudnn.h | grep CUDNN_MAJOR -A 2? #查看cudnn版本
|