ubuntu:多版本CUDA安装及使用


前言

在深度学习项目使用过程中,因项目所需cuda版本的不同,故针对这种情况,实现多CUDA版本的管理使用,并不影响单独环境之间的冲突。


安装管理多版本的CUDA

1.通过建立软连接的方式

在NVIDIA官网下载所需版本的runfile文件,(CUDA Toolkit Archive | NVIDIA Developer) ,如果本身已经安装好显卡驱动,可以不勾选驱动项。安装后,可以在 /usr/local 中找到。其中cuda文件夹是生效cuda版本的软链接。如果想要指定版本的cuda,可以生成所需版本的cuda链接。举个例子:

选择cuda 11.8:

# 替换当前软链接

sudo rm -rf /usr/local/cuda
sudo ln -s /usr/local/cuda-11.8/ /usr/local/cuda

# 查看当前cuda版本

nvcc -V

2.通过程序自由切换

在官网下载需要的版本:CUDA Toolkit Archive | NVIDIA Developer

下载安装以后不需要配置任何环境和创建软连接

创建一个.sh文件,将以下程序复制在文件中

#可以查看到当前安装的所有cuda版本
source switch-cuda.sh
#选择需要的版本
source switch-cuda.sh 11.8
	#!/usr/bin/env bash
	
	# Copyright (c) 2018 Patrick Hohenecker
	#
	# Permission is hereby granted, free of charge, to any person obtaining a copy
	# of this software and associated documentation files (the "Software"), to deal
	# in the Software without restriction, including without limitation the rights
	# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	# copies of the Software, and to permit persons to whom the Software is
	# furnished to do so, subject to the following conditions:
	#
	# The above copyright notice and this permission notice shall be included in all
	# copies or substantial portions of the Software.
	#
	# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
	# SOFTWARE.
	
	# author:   Patrick Hohenecker <mail@paho.at>
	# version:  2018.1
	# date:     May 15, 2018
	
	
	set -e
	
	
	# ensure that the script has been sourced rather than just executed
	if [[ "${BASH_SOURCE[0]}" = "${0}" ]]; then
	    echo "Please use 'source' to execute switch-cuda.sh!"
	    exit 1
	fi
	
	INSTALL_FOLDER="/usr/local"  # the location to look for CUDA installations at
	TARGET_VERSION=${1}          # the target CUDA version to switch to (if provided)
	
	# if no version to switch to has been provided, then just print all available CUDA installations
	if [[ -z ${TARGET_VERSION} ]]; then
	    echo "The following CUDA installations have been found (in '${INSTALL_FOLDER}'):"
	    ls -l "${INSTALL_FOLDER}" | egrep -o "cuda-[0-9]+\\.[0-9]+$" | while read -r line; do
	        echo "* ${line}"
	    done
	    set +e
	    return
	# otherwise, check whether there is an installation of the requested CUDA version
	elif [[ ! -d "${INSTALL_FOLDER}/cuda-${TARGET_VERSION}" ]]; then
	    echo "No installation of CUDA ${TARGET_VERSION} has been found!"
	    set +e
	    return
	fi
	
	# the path of the installation to use
	cuda_path="${INSTALL_FOLDER}/cuda-${TARGET_VERSION}"
	
	# filter out those CUDA entries from the PATH that are not needed anymore
	path_elements=(${PATH//:/ })
	new_path="${cuda_path}/bin"
	for p in "${path_elements[@]}"; do
	    if [[ ! ${p} =~ ^${INSTALL_FOLDER}/cuda ]]; then
	        new_path="${new_path}:${p}"
	    fi
	done
	
	# filter out those CUDA entries from the LD_LIBRARY_PATH that are not needed anymore
	ld_path_elements=(${LD_LIBRARY_PATH//:/ })
	new_ld_path="${cuda_path}/lib64:${cuda_path}/extras/CUPTI/lib64"
	for p in "${ld_path_elements[@]}"; do
	    if [[ ! ${p} =~ ^${INSTALL_FOLDER}/cuda ]]; then
	        new_ld_path="${new_ld_path}:${p}"
	    fi
	done
	
	# update environment variables
	export CUDA_HOME="${cuda_path}"
	export CUDA_ROOT="${cuda_path}"
	export LD_LIBRARY_PATH="${new_ld_path}"
	export PATH="${new_path}"
	
	echo "Switched to CUDA ${TARGET_VERSION}."
	
	set +e
	return


总结

推荐第二种方法实现多版本的cuda管理和使用

  • 7
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以在Ubuntu安装多个版本CUDA。以下是一种方法: 1. 首先,确保您的系统上尚未安装任何版本CUDA。如果已经安装CUDA,请先卸载它。 2. 前往NVIDIA官方网站(https://developer.nvidia.com/cuda-toolkit-archive)并下载您想要安装CUDA版本安装程序。选择适用于您的Ubuntu版本和GPU的相应版本。 3. 打开终端,并导航到您下载的CUDA安装程序所在的目录。 4. 运行以下命令来确保您具有执行权限: ``` chmod +x cuda_<version>_linux.run ``` 5. 然后,运行安装程序: ``` sudo ./cuda_<version>_linux.run ``` 在此命令中,将 `<version>` 替换为您下载的CUDA版本的实际版本号。 6. 安装程序将提示您接受许可协议和选择安装选项。请按照屏幕上的指示进行操作。 7. 当安装程序完成后,您需要配置环境变量。打开终端并编辑 `~/.bashrc` 文件: ``` nano ~/.bashrc ``` 8. 在文件末尾添加以下行,将 `<version>` 替换为您安装CUDA版本的实际版本号: ``` export PATH=/usr/local/cuda-<version>/bin${PATH:+:${PATH}} export LD_LIBRARY_PATH=/usr/local/cuda-<version>/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}} ``` 9. 保存文件并退出编辑器。 10. 运行以下命令以使更改生效: ``` source ~/.bashrc ``` 11. 现在,您应该已经成功安装了所需版本CUDA。您可以通过运行以下命令来验证安装: ``` nvcc --version ``` 现在您已经安装多个版本CUDA。您可以按照上述步骤重复执行以安装其他版本
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值