ubuntu22.04多版本安装cuda及快速切换(cuda11.1和11.8)

蓝心.- 2024-07-20 13:37:04 阅读 91

自己学习使用,用以记录,以防遗忘。也希望对大家有所帮助。

(默认已安装anaconda、显卡驱动)

1.官网下载cuda(CUDA Toolkit Archive | NVIDIA Developer)

默认已下载cuda11.1,如下图所示选择相应版本(ubuntu向下兼容,22.04可选20.04版本的cuda

注意选择runfile方法

在服务器终端输入:

<code>wget https://developer.download.nvidia.com/compute/cuda/11.1.0/local_installers/cuda_11.1.0_455.23.05_linux.run

sudo sh cuda_11.1.0_455.23.05_linux.run

进行安装即可。

接着下载cuda11.8!版本选择按照上述图片进行选择,如果你选择11.8.0,可直接按照我下面的代码进行安装。(输入后会卡一下,耐心等待)

wget https://developer.download.nvidia.com/compute/cuda/11.8.0/local_installers/cuda_11.8.0_520.61.05_linux.run

sudo sh cuda_11.8.0_520.61.05_linux.run

安装过程中

        第一步:选择accept

        第二步:第一项不选(回车取消选择),已安装显卡驱动

选择后小键盘移动到:install  回车

        第三步:应该是a symbol ...什么什么的  选择 no(不要选择yes!!)

等待....

出现和下面的类似代码表示安装成功!

<code>===========

= Summary =

===========

Driver: Not Selected

Toolkit: Installed in /usr/local/cuda-11.8/

Please make sure that

- PATH includes /usr/local/cuda-11.8/bin

- LD_LIBRARY_PATH includes /usr/local/cuda-11.8/lib64, or, add /usr/local/cuda-11.8/lib64 to /etc/ld.so.conf and run ldconfig as root

To uninstall the CUDA Toolkit, run cuda-uninstaller in /usr/local/cuda-11.8/bin

***WARNING: Incomplete installation! This installation did not install the CUDA Driver. A driver of version at least 520.00 is required for CUDA 11.8 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 /var/log/cuda-installer.log

2.配置环境变量

2.1为cuda11.1配置环境变量

编辑.bashrc文件:

nano ~/.bashrc

在文件末尾添加以下内容(已有可跳过,版本号根据自身情况修改):

export PATH=/usr/local/cuda-11.1/bin${PATH:+:${PATH}}

export LD_LIBRARY_PATH=/usr/local/cuda-11.1/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}

保存并退出后,更新环境变量:

source ~/.bashrc

 2.2为cuda11.8配置环境变量

编辑.bashrc文件:

nano ~/.bashrc

在文件末尾添加以下内容(已有可跳过,版本号根据自身情况修改):

export PATH=/usr/local/cuda-11.8/bin${PATH:+:${PATH}}

export LD_LIBRARY_PATH=/usr/local/cuda-11.8/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}

保存并退出后,更新环境变量:

source ~/.bashrc

3.编写快速切换脚本文件

3.1 创建切换到CUDA 11.1的脚本:

创建脚本文件:

sudo nano /usr/local/bin/switch_cuda_11.1.sh

添加以下内容:

#!/bin/bash

export PATH=/usr/local/cuda-11.1/bin${PATH:+:${PATH}}

export LD_LIBRARY_PATH=/usr/local/cuda-11.1/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}

echo "Switched to CUDA 11.1"

保存并退出,赋予执行权限:

sudo chmod +x /usr/local/bin/switch_cuda_11.1.sh

 3.2 创建切换到CUDA 11.8的脚本:

创建脚本文件:

sudo nano /usr/local/bin/switch_cuda_11.8.sh

添加以下内容:

#!/bin/bash

export PATH=/usr/local/cuda-11.8/bin${PATH:+:${PATH}}

export LD_LIBRARY_PATH=/usr/local/cuda-11.8/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}

echo "Switched to CUDA 11.8"

保存并退出,赋予执行权限:

sudo chmod +x /usr/local/bin/switch_cuda_11.8.sh

4.cuda版本切换方法

切换到cuda11.1

source /usr/local/bin/switch_cuda_11.1.sh

切换到cuda11.8

source /usr/local/bin/switch_cuda_11.8.sh

每次切换后可使用

nvcc -V

验证。



声明

本文内容仅代表作者观点,或转载于其他网站,本站不以此文作为商业用途
如有涉及侵权,请联系本站进行删除
转载本站原创文章,请注明来源及作者。