【全网首发】Ubuntu-22.04服务器系统搭建深度学习情况,安装cuda和cuDNN, ...

打印 上一主题 下一主题

主题 837|帖子 837|积分 2521

一、前言

        截止2024年12月19日,所有搜刮引擎中无法找到在服务器情况下搭建Ubuntu-22.04的cuda情况教程中文文章,并且许多安装教程已经过时、存在错误,使很多人走了弯路,因此发布本篇文章来造福社会。为编写本文耗费了近一周的时间实验、整理,因此本文到处存在非常微小的细节,请迟钝仔细阅读!
        留意!本文章仅适用于Ubuntu-22.04系统并且是在服务器情况下搭建!完全不符合以下条件的慎重采纳本文章的意见:


  • System:Ubuntu-22.04(不含图形化界面)
  • CPU:E5-2686v4(虚拟化)
  • RAM:32G
  • GPU:RTX 2080Ti 22G 300A(独享)
本文目录如下:


二、安装准备

1. 准备情况-重装原始系统

        一般云服务服务商都会提供Ubuntu系统镜像可用,请选择Ubuntu22.04镜像进行系统重装。
        对于已经把系统搞得乱成一团的初学者们也建议重装系统从头开始(好比一来就把核心给改了的,安装上宝塔了的...)。不要以为本身做了那么多很痛惜,对于初学者来说重装系统回到原点是最好最简朴的方法来继续。作者在准备编写本文期间重装了4种系统共重装了十频频的。

        安装完成后请不要进行多余的操作,跟着本文教程继续走。

2. 推荐的工作学习情况-VScodeSSH

        在这里推荐两个各人都不陌生的工具:VScode和Xshell
编辑文件神器-VScode:
        VScode应该都不陌生,不过它的SSH长途毗连服务器功能真的香!!
        具体怎样安装和操作请参见本文:
        最香长途开辟解决方案!手把手教你设置VS Code长途开辟工具
        有了VScodeSSH我们可以非常轻松地编辑服务器上的目录文件!便于之后的操作。
        SSH工具可以随便选一个Bitvise SSH Client、Xshell,但不推荐直接使用VScodeSSH的终端!因为它不支持一些系统,并且轻易出现问题。

3. 切换apt源

        留意!这里的内容只适用于Ubuntu22.04!只适用于Ubuntu22.04!只适用于Ubuntu22.04!紧张的事变说三遍!
        如果你如今还不会编辑文件请先完成上一节内容!
        在/etc/apt/sources.list中写入:
  1. # 默认注释了源码仓库,如有需要可自行取消注释
  2. deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
  3. # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
  4. deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
  5. # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
  6. deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
  7. # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
  8. deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
  9. # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
复制代码
        然后执行命令,更新软件包列表:
  1. sudo apt update
复制代码
        然后进行下一步。

4. 安装依靠-gcc、g++、make

        执行以下命令安装依靠:
  1. sudo apt install build-essential gcc g++ make -y
复制代码
        安装完成后进行下一步。

5. 禁用默认nouveau驱动

        编辑/etc/modprobe.d/blacklist.conf,在末了参加以下内容:
  1. blacklist nouveau
  2. options nouveau modeset=0
复制代码
        更新系统initramfs镜像文件
  1. sudo update-initramfs -u
复制代码
        重启
  1. reboot
复制代码
        使用以下命令查验是否成功禁用nouveau,如果没有输出则表示成功禁用。
  1. lsmod | grep nouveau
复制代码
        完成后我们就可以正式进行cuda的安装了!。

三、安装CUDA与cuDNN

        你大概会问:不是还没装nvidia驱动吗?怎么就装cuda了?
        最开始我也是这么想的,后来发现其实cuda会主动安装nvidia驱动的,如果安装了nvidia驱动再安装cuda,安装好cuda后nvidia驱动就会又用不了了qwq(电子玄学问题,无法表明原因)
1. 安装CUDA

        在bing上搜cuda对应的版本号,例如cuda11.8,就这么简朴//

        然后选择本身的显卡、Ubuntu22.04对应的runfile,这里不过多赘述了:

        留意是选择runfile (local)!!选deb也可以,但推荐使用runfile
        执行命令:
  1. wget https://developer.download.nvidia.com/compute/cuda/...
复制代码
        然后进行安装:
  1. sudo sh cuda_xxx_xxx.xxx_linux.run
复制代码
        安装前解压时间长,请耐心等候。
        进入安装界面后,不要做任何操作,直接Install(如果想要安装Kernel,请参见这一文章:Centos8.2 搭建显卡驱动+CUDA+CUDNN_centos8.2安装显卡驱动-CSDN博客,虽然是Centos的,但应该和Ubuntu目录结构大抵相同,可以参考)
        

        安装cuda会主动安装nvidia驱动。如果需要安装最新驱动请见后面。关于怎样使nvcc -V命令有用(将cuda参加到情况变量中),请见"拓展部分-CUDA版本的机动切换",建议按照步骤逐步来。如果你不想实现cuda版本机动切换,可以参考这篇文章直接设置情况:Linux下安装CUDA并设置情况变量_linux cuda位置-CSDN博客
        可以用以下命令查验nvidia驱动是否成功安装:
  1. nvidia-smi
复制代码
        完成后我们进入下一步。

2. 安装cuDNN

        在这个链接中找到本身的系统版本:cuDNN 9.6.0 Downloads | NVIDIA Developer
(如果有更新的版本,请本身手动搜刮查找)

   
     按照官方文章进行安装跟着步骤走即可。
        安装完成后如果提示"The public cudnn-local-repo-ubuntu2204-8.9.7.29 GPG key does not appear to be installed",直接复制下面的指令输进去然后再来一次即可。

  1. sudo cp /var/cudnn-local-repo-ubuntu2204....
  2. sudo dpkg -i cudnn-ubuntu2204.deb
复制代码
        cuDNN自此安装成功。

3. (可选)安装Nvidia驱动

        如果你希望保持更高的Nvidia驱动版本,可以进行以下步骤:
        到官网下载得当本身显卡和系统的驱动:

        然后下载并安装:
  1. wget https://....
  2. sudo NVIDIA-Linux-x86_64-xxx.xxx.run
复制代码
         查验是否安装成功:
  1. nvidia-smi
复制代码


四、拓展

1. 实现CUDA版本机动切换

        本节参考文章 创建多个cuda版本,可以自由切换 ,感谢作者!
        由于很多模子框架基于的cuda版本差别,因此我们大概需要频仍地使用各种各样的cuda。不过本节内容可以彻底解放大脑,轻松切换CUDA版本!
        进入到usr/bin目录下,创建文件switch-cuda.sh,写入以下内容:
  1. #!/usr/bin/env bash
  2.        
  3.         # Copyright (c) 2018 Patrick Hohenecker
  4.         #
  5.         # Permission is hereby granted, free of charge, to any person obtaining a copy
  6.         # of this software and associated documentation files (the "Software"), to deal
  7.         # in the Software without restriction, including without limitation the rights
  8.         # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9.         # copies of the Software, and to permit persons to whom the Software is
  10.         # furnished to do so, subject to the following conditions:
  11.         #
  12.         # The above copyright notice and this permission notice shall be included in all
  13.         # copies or substantial portions of the Software.
  14.         #
  15.         # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16.         # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17.         # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18.         # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19.         # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20.         # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21.         # SOFTWARE.
  22.        
  23.         # author:   Patrick Hohenecker <mail@paho.at>
  24.         # version:  2018.1
  25.         # date:     May 15, 2018
  26.        
  27.        
  28.         set -e
  29.        
  30.        
  31.         # ensure that the script has been sourced rather than just executed
  32.         if [[ "${BASH_SOURCE[0]}" = "${0}" ]]; then
  33.             echo "Please use 'source' to execute switch-cuda.sh!"
  34.             exit 1
  35.         fi
  36.        
  37.         INSTALL_FOLDER="/usr/local"  # the location to look for CUDA installations at
  38.         TARGET_VERSION=${1}          # the target CUDA version to switch to (if provided)
  39.        
  40.         # if no version to switch to has been provided, then just print all available CUDA installations
  41.         if [[ -z ${TARGET_VERSION} ]]; then
  42.             echo "The following CUDA installations have been found (in '${INSTALL_FOLDER}'):"
  43.             ls -l "${INSTALL_FOLDER}" | egrep -o "cuda-[0-9]+\\.[0-9]+$" | while read -r line; do
  44.                 echo "* ${line}"
  45.             done
  46.             set +e
  47.             return
  48.         # otherwise, check whether there is an installation of the requested CUDA version
  49.         elif [[ ! -d "${INSTALL_FOLDER}/cuda-${TARGET_VERSION}" ]]; then
  50.             echo "No installation of CUDA ${TARGET_VERSION} has been found!"
  51.             set +e
  52.             return
  53.         fi
  54.        
  55.         # the path of the installation to use
  56.         cuda_path="${INSTALL_FOLDER}/cuda-${TARGET_VERSION}"
  57.        
  58.         # filter out those CUDA entries from the PATH that are not needed anymore
  59.         path_elements=(${PATH//:/ })
  60.         new_path="${cuda_path}/bin"
  61.         for p in "${path_elements[@]}"; do
  62.             if [[ ! ${p} =~ ^${INSTALL_FOLDER}/cuda ]]; then
  63.                 new_path="${new_path}:${p}"
  64.             fi
  65.         done
  66.        
  67.         # filter out those CUDA entries from the LD_LIBRARY_PATH that are not needed anymore
  68.         ld_path_elements=(${LD_LIBRARY_PATH//:/ })
  69.         new_ld_path="${cuda_path}/lib64:${cuda_path}/extras/CUPTI/lib64"
  70.         for p in "${ld_path_elements[@]}"; do
  71.             if [[ ! ${p} =~ ^${INSTALL_FOLDER}/cuda ]]; then
  72.                 new_ld_path="${new_ld_path}:${p}"
  73.             fi
  74.         done
  75.        
  76.         # update environment variables
  77.         export CUDA_HOME="${cuda_path}"
  78.         export CUDA_ROOT="${cuda_path}"
  79.         export LD_LIBRARY_PATH="${new_ld_path}"
  80.         export PATH="${new_path}"
  81.        
  82.         echo "Switched to CUDA ${TARGET_VERSION}."
  83.        
  84.         set +e
  85.         return
复制代码
        之后我们就可以使用"switch-cuda.sh+版本"来实现机动切换CUDA版本。具体指令如下:
  1. # 查询当前已安装的CUDA版本
  2. source switch-cuda.sh
  3. # 切换到指定CUDA版本,例如cuda11.8
  4. source switch-cuda.sh 11.8
复制代码
        可以发现CUDA版本成功切换了。

         自此文章大部分内容结束,深度学习情况成功搭建完成!

2. 使用Anaconda搭建虚拟情况

        末了还是得推荐下自家宝子 Anaconda 信赖大多数人都用过,也是由于各种各样的模子框架所需要的Python库版本差别,以是可以使用 Anaconda创建差别情况来支持差别的模子。
        主要命令:
  1. # 创建新的虚拟环境
  2. conda create -n env_name python=3.8
  3. # 激活虚拟环境
  4. conda activate env_name
  5. # 退出虚拟环境
  6. conda deactivate
  7. # 删除虚拟环境
  8. conda remove -n env_name --all
  9. # 查看已安装的环境列表
  10. conda env list
  11. # 安装指定的包
  12. conda install package_name
  13. # 升级指定的包
  14. conda update package_name
  15. # 移除指定的包
  16. conda remove package_name
  17. # 查看已安装的包
  18. conda list
  19. # 搜索可用的包
  20. conda search package_name
  21. # 更新Anaconda自身
  22. conda update conda
  23. conda update anaconda
  24. # 导出虚拟环境
  25. conda env export > environment.yml
  26. # 从环境文件导入虚拟环境
  27. conda env create -f environment.yml
  28. # 检查依赖问题并修复
  29. conda doctor
  30. # 查看conda帮助
  31. conda --help
复制代码

3. 实现主动毗连无需暗码

        本节参考本篇文章:最香长途开辟解决方案!手把手教你设置VS Code长途开辟工具,感谢作者!
       


  • 打开win cmd终端,输入 ssh-keygen -t rsa 天生秘钥对
    1. ssh-keygen -t rsa
    复制代码



  • 打开天生的秘钥生存路径,拷贝 id_rsa.pub 内容,添加到到云服务器的 ~/.ssh/authorized_keys 文件后面。



  • 实验再次毗连,不消输暗码了。

以上就是本文的全部内容,感谢各人的耐心阅读,希望本文能够帮到你!如果有任何疑问请在批评区留言!

五、后记

1. 参考链接

- https://blog.csdn.net/qq_32892383/article/details/141460197
- https://blog.csdn.net/qq_34972053/article/details/127689332
- https://forums.developer.nvidia.com/t/info-finished-with-code-256-error-install-of-driver-component-failed/107661/9
- https://kernel.ubuntu.com/mainline/v6.12/
- https://blog.csdn.net/bocai1215/article/details/126559197
- https://www.cnblogs.com/chua-n/p/13208414.html
- https://www.cnblogs.com/chua-n/p/13208398.html
- https://zhuanlan.zhihu.com/p/136234910
- https://developer.nvidia.com/cuda-11-8-0-download-archive?target_os=Linux&target_arch=x86_64&Distribution=Ubuntu&target_version=22.04&target_type=runfile_local
- https://blog.csdn.net/wjinjie/article/details/108997692
- https://blog.csdn.net/lishuaigell/article/details/124740342
- https://blog.csdn.net/weixin_43472800/article/details/128434810
- https://blog.csdn.net/onlyyoujojo/article/details/129024630
- https://www.nvidia.cn/drivers/lookup
- https://www.bilibili.com/video/BV1JH4y137sy/?spm_id_from=333.337.search-card.all.click&vd_source=228350a2a097728d450346c0a016177a
- https://www.bilibili.com/video/BV1oK411b7RP/?spm_id_from=333.337.search-card.all.click
- https://zhuanlan.zhihu.com/p/133323571
- https://blog.csdn.net/Long_xu/article/details/135039596
- https://blog.csdn.net/marleylee/article/details/70739131
- https://blog.csdn.net/qq_21095573/article/details/99736630
- https://developer.aliyun.com/mirror/
- https://zhuanlan.zhihu.com/p/251009600
- https://blog.csdn.net/qq_21095573/article/details/99736630
- https://blog.csdn.net/Sihang_Xie/article/details/127347139
- https://blog.csdn.net/m0_63171455/article/details/139150054
- https://www.cnblogs.com/pprp/p/9430836.html
- https://blog.csdn.net/weixin_44009447/article/details/120034467
- https://blog.csdn.net/Ben__Ho/article/details/139202015
- https://www.bilibili.com/video/BV1v84y1D7Rw/?spm_id_from=333.337.search-card.all.click&vd_source=228350a2a097728d450346c0a016177a
- https://www.cnblogs.com/bile/p/12502739.html
- https://docs.nvidia.com/cuda/cuda-toolkit-release-notes/index.html
- https://blog.csdn.net/weixin_42301220/article/details/130078734
- https://zhuanlan.zhihu.com/p/581634820
- https://blog.csdn.net/qq_42055933/article/details/142030339
- https://www.cnblogs.com/raiuny/p/16963325.html
- https://blog.csdn.net/huiyoooo/article/details/128015155
- https://blog.csdn.net/cwjcw81/article/details/139604268
- https://www.modelscope.cn/models/Qwen/Qwen2.5-7B-Instruct/summary
- https://blog.csdn.net/hb_learing/article/details/115547461
- https://www.cnblogs.com/137point5/p/15000954.html
- https://blog.csdn.net/tiansyun/article/details/131453705
- https://blog.csdn.net/xuezhe5212/article/details/139087879
- https://blog.csdn.net/A15216110998/article/details/113402172
- https://forums.developer.nvidia.com/t/linux-6-7-3-545-29-06-550-40-07-error-modpost-gpl-incompatible-module-nvidia-ko-uses-gpl-only-symbol-rcu-read-lock/280908/6
- https://forums.developer.nvidia.com/t/nvidia-545-installation-is-getting-failed-in-my-ubuntu-20-04-and-i-have-rtx4000/297029/5

2. 特别鸣谢

- 未音云 - https://cloud.whyyin.com/
感谢未音云提供的云服务器服务
- 欢雨云 - https://cloud.rainly.net/
感谢欢雨云提供的GPU服务器服务

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

来自云龙湖轮廓分明的月亮

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表