ubuntu20.04安装多版本cuda,切换版本

1. 安装cuda toolkit:

下载网站 https://developer.nvidia.com/cuda-11.3.0-download-archive

选择版本,这里选择11.3
在这里插入图片描述

wget https://developer.download.nvidia.com/compute/cuda/11.3.0/local_installers/cuda_11.3.0_465.19.01_linux.run

给cuda权限:

chmod +x cuda_11.3.0_465.19.01_linux.run

运行安装文件:

sudo sh cuda_11.3.0_465.19.01_linux.run

在这里插入图片描述

利用上下键以及enter,选中,只安装CUDA Toolkit,

注意:
如果系统可以运行nvidia-smi,说明已经安装好了cuda的驱动,此时一定要在下个界面通过按回车取消Driver的安装,否则会出现[ERROR]: Install of driver component failed.的情况。

选择Options并进入,然后我们需要修改Toolkit Options 、Library install path这两项的路径,把路径都设为/usr/local/cuda-11.3/,
其他项都不选择
最后点击Install,等待一段时间,会出现如下所示安装信息,说明安装成功

Driver:   Not Selected
Toolkit:  Installed in /usr/local/cuda-11.3/
Samples:  Not Selected

若是只安装一个版本的,接下来配置环境变量。

配置环境变量
修改环境变量文件

vim ~/.bashrc

export LD_LIBRARY_PATH=/usr/local/cuda-11.3/lib64
export PATH=$PATH:/usr/local/cuda-11.3/bin

执行:source ~/.bashrc,立即生效

再次执行:nvcc -V,成功
在这里插入图片描述

同样的步骤,可以安装其他版本cuda。

2.切换cuda版本以应对不同的pytorch版本

在已存在多种版本cuda的情况下,切换cuda版本以应对不同的pytorch版本
如cd /usr/local $ls
可以看到有cuda-11.1 cuda-11.3

切换脚本:
参考https://github.com/phohenecker/switch-cuda/blob/master/README.md

vim switch-cuda.sh
#!/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版本

source switch-cuda.sh 

我这里会显示有两个版本的cuda

The following CUDA installations have been found (in '/usr/local'):
* cuda-11.1
* cuda-11.3

2.2 当切换到11.3时

切换指令

source switch-cuda.sh 11.3 

查看当先cuda指令:

nvcc -V

显示

nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2021 NVIDIA Corporation
Built on Sun_Mar_21_19:15:46_PDT_2021
Cuda compilation tools, release 11.3, V11.3.58
Build cuda_11.3.r11.3/compiler.29745058_0

2.3 当切换到11.1时

切换指令

source switch-cuda.sh 11.1 

查看当先cuda指令:

nvcc -V

显示

nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2020 NVIDIA Corporation
Built on Mon_Oct_12_20:09:46_PDT_2020
Cuda compilation tools, release 11.1, V11.1.105
Build cuda_11.1.TC455_06.29190527_0
### 安装准备 确保Ubuntu 20.04系统已更新至最新状态,并且已经成功安装了适用于NVIDIA显卡的驱动程序[^1]。 ```bash sudo apt update && sudo apt upgrade -y ``` ### 下载CUDA Toolkit 前往NVIDIA官方网站下载适合于Ubuntu 20.04系统的CUDA Toolkit版本。考虑到兼容性问题,建议选择经过验证可以正常工作的特定版本,比如CUDA 11.4[^3]。 ### 添加CUDA仓库并安装 为了简化后续操作以及方便管理不同版本之间的切换,可以通过官方提供的.deb文件来设置本地软件源: ```bash wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/cuda-ubuntu2004.pin sudo mv cuda-ubuntu2004.pin /etc/apt/preferences.d/cuda-repository-pin-600 sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/7fa2af80.pub sudo add-apt-repository "deb http://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/ /" sudo apt-get update sudo apt-get -y install cuda-11-4 # 版本号根据实际需求调整 ``` ### 设置环境变量 编辑`~/.bashrc`或其他shell配置文件,在其中加入如下路径设定语句以便能够直接调用nvcc编译器和其他工具链组件: ```bash export PATH=/usr/local/cuda-11.4/bin${PATH:+:${PATH}} export LD_LIBRARY_PATH=/usr/local/cuda-11.4/lib64\ ${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}} ``` 使更改立即生效: ```bash source ~/.bashrc ``` ### 验证安装成果 通过执行下面这条命令测试是否能正确识别到GPU设备及其计算能力等级;同时也可以尝试编译运行一些简单的CUDA示例项目来进行更全面的功能检测。 ```bash nvidia-smi ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值