海哥 发表于 2024-7-23 12:32:38

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

本身学习使用,用以记载,以防遗忘。也盼望对大家有所帮助。
(默认已安装anaconda、显卡驱动)
1.官网下载cuda(CUDA Toolkit Archive | NVIDIA Developer)

https://img-blog.csdnimg.cn/direct/c40d2ecdc050483e8f71f755efb030f7.png
默认已下载cuda11.1,如下图所示选择相应版本(ubuntu向下兼容,22.04可选20.04版本的cuda)
注意选择runfile方法
https://img-blog.csdnimg.cn/direct/0ede648d350c4bde91f8677c9941a590.png
在服务器终端输入:
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
        第二步:第一项不选(回车取消选择),已安装显卡驱动https://img-blog.csdnimg.cn/direct/20ab24368b1b4e7b9be7ab7cf980f5a6.png
选择后小键盘移动到:install  回车
        第三步:应该是a symbol ...什么什么的  选择 no(不要选择yes!!)
等待....
出现和下面的类似代码表示安装乐成!
===========
= 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/bashexport 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/bashexport 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 验证。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: ubuntu22.04多版本安装cuda及快速切换(cuda11.1和11.8)