当前位置:   article > 正文

ubuntu22.04安装显卡、CUDA(含多个CUDA切换)、CUDNN、pytorch_ubuntu22安装cuda

ubuntu22安装cuda

一、显卡

  1. 「必须」更新软件列表和安装必要软件、依赖

    1. sudo apt-get update #更新软件列表
    2. sudo apt-get install g++
    3. sudo apt-get install gcc
    4. sudo apt-get install make
  2. 禁用 nouveau 驱动

    ouveau是Ubuntu自带的显卡驱动,但他是核显,我这里想安装独显,就得把他禁掉

    1. 命令(cmd)

      sudo gedit /etc/modprobe.d/blacklist.conf

      (输入密码)

    2. (自动打开的)文本,在末尾

      1. # added
      2. blacklist nouveau
      3. blacklist lbm-nouveau
      4. options nouveau modeset=0
      5. alias nouveau off
      6. alias lbm-nouveau off

      ctrl+s 保存

    3. 更新重启

      1. sudo update-initramfs –u
      2. sudo reboot # 重启电脑
      lsmod | grep nouveau  # 输出内容为空,则表示成功禁用
  3. 安装驱动

    1. 选择合适版本:官网查询,记住版本号xxx.yy
    2. 卸载之前的
      1. sudo apt-get remove nvidia-* # 卸载之前的
      2. add-apt-repository ppa:graphics-drivers/ppa # 更新显卡驱动的源
      3. apt-get install nvidia-driver-xxx # xxx是显卡版本,需要修改
  4. 检查

    nvidia-smi

  5.  安装成功。

    CUDA version :12.4 ——最大CUDA版本

二、CUDA

CUDA-显卡 对应

  1. pytorch 官网:CUDA11.8   12.1

  2. 安装依赖

    sudo apt-get install freeglut3-dev build-essential libx11-dev libxmu-dev libxi-dev libgl1-mesa-glx libglu1-mesa libglu1-mesa-dev
  3. 安装CUDA

    1. CUDA 11.8
    2. CUDA 12.1
    3. 一路选择下来,我最后选了runfile(local),因为命令行少

      第一行加sudo,能防止权限不够

      1. # 11.8
      2. sudo wget https://developer.download.nvidia.com/compute/cuda/11.8.0/local_installers/cuda_11.8.0_520.61.05_linux.run
      3. sudo sh cuda_11.8.0_520.61.05_linux.run
      1. # 12.1
      2. sudo wget https://developer.download.nvidia.com/compute/cuda/12.1.0/local_installers/cuda_12.1.0_530.30.02_linux.run
      3. sudo sh cuda_12.1.0_530.30.02_linux.run
  4. 安装时的选择

    1. continue
    2. 写 accept
    3. 不要driver:在driver处按enter
    4. install
    5. (如果有多个CUDA,会问你symlink改不改): Yes
  5. 检查

    nvcc -V

成功!

安装CUDA失败,重装该版本(希望用不上)

环境配置之cuda的卸载(ubuntu)_ubuntu卸载cuda-CSDN博客

切换CUDA(选读)

  1. 切换脚本
    1. sudo vim switch-cuda.sh
      1. #!/usr/bin/env bash
      2. # Copyright (c) 2018 Patrick Hohenecker
      3. #
      4. # Permission is hereby granted, free of charge, to any person obtaining a copy
      5. # of this software and associated documentation files (the "Software"), to deal
      6. # in the Software without restriction, including without limitation the rights
      7. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
      8. # copies of the Software, and to permit persons to whom the Software is
      9. # furnished to do so, subject to the following conditions:
      10. #
      11. # The above copyright notice and this permission notice shall be included in all
      12. # copies or substantial portions of the Software.
      13. #
      14. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
      15. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      16. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
      17. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      18. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
      19. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
      20. # SOFTWARE.
      21. # author: Patrick Hohenecker <mail@paho.at>
      22. # version: 2018.1
      23. # date: May 15, 2018
      24. set -e
      25. # ensure that the script has been sourced rather than just executed
      26. if [[ "${BASH_SOURCE[0]}" = "${0}" ]]; then
      27. echo "Please use 'source' to execute switch-cuda.sh!"
      28. exit 1
      29. fi
      30. INSTALL_FOLDER="/usr/local" # the location to look for CUDA installations at
      31. TARGET_VERSION=${1} # the target CUDA version to switch to (if provided)
      32. # if no version to switch to has been provided, then just print all available CUDA installations
      33. if [[ -z ${TARGET_VERSION} ]]; then
      34. echo "The following CUDA installations have been found (in '${INSTALL_FOLDER}'):"
      35. ls -l "${INSTALL_FOLDER}" | egrep -o "cuda-[0-9]+\\.[0-9]+$" | while read -r line; do
      36. echo "* ${line}"
      37. done
      38. set +e
      39. return
      40. # otherwise, check whether there is an installation of the requested CUDA version
      41. elif [[ ! -d "${INSTALL_FOLDER}/cuda-${TARGET_VERSION}" ]]; then
      42. echo "No installation of CUDA ${TARGET_VERSION} has been found!"
      43. set +e
      44. return
      45. fi
      46. # the path of the installation to use
      47. cuda_path="${INSTALL_FOLDER}/cuda-${TARGET_VERSION}"
      48. # filter out those CUDA entries from the PATH that are not needed anymore
      49. path_elements=(${PATH//:/ })
      50. new_path="${cuda_path}/bin"
      51. for p in "${path_elements[@]}"; do
      52. if [[ ! ${p} =~ ^${INSTALL_FOLDER}/cuda ]]; then
      53. new_path="${new_path}:${p}"
      54. fi
      55. done
      56. # filter out those CUDA entries from the LD_LIBRARY_PATH that are not needed anymore
      57. ld_path_elements=(${LD_LIBRARY_PATH//:/ })
      58. new_ld_path="${cuda_path}/lib64:${cuda_path}/extras/CUPTI/lib64"
      59. for p in "${ld_path_elements[@]}"; do
      60. if [[ ! ${p} =~ ^${INSTALL_FOLDER}/cuda ]]; then
      61. new_ld_path="${new_ld_path}:${p}"
      62. fi
      63. done
      64. # update environment variables
      65. export CUDA_HOME="${cuda_path}"
      66. export CUDA_ROOT="${cuda_path}"
      67. export LD_LIBRARY_PATH="${new_ld_path}"
      68. export PATH="${new_path}"
      69. echo "Switched to CUDA ${TARGET_VERSION}."
      70. set +e
      71. return
      1. # 保存,优先选1
      2. :w !sudo tee %
      3. :wq!
    1. source switch-cuda.sh # 查看拥有的CUDA版本
    2. source switch-cuda.sh XX.X # XX.X为版本号

三、CUDNN

  1. 选择合适版本:官网,下载tar(或者三个包)

  2. 单个包跳到3,
    三个包点链接:ubuntu下的cudnn安装_cudnn安装 ubuntu-CSDN博客

  3. 解压

    1. tar -xvf cudnn-linux-x86_64-8.9.6.50_cuda11-archive.tar.xz
    2. # 改文件名称
    3. # 或者可以删掉文件名,然后把想解压的文件拖到命令窗口里,自动获取文件路径。都可以
  4. 进入文件夹,复制

    1. cd cudnn-linux-x86_64-8.9.6.50_cuda11-archive/
    2. sudo cp -d -r ./lib/* /usr/local/cuda-11.8/lib64/
    3. sudo cp -r ./include/* /usr/local/cuda-11.8/include/
  5. 测试

    1. sudo chmod a+r /usr/local/cuda-11.8/include/cudnn.h /usr/local/cuda-11.8/lib64/libcudnn*
    2. cat /usr/local/cuda-11.8/include/cudnn_version.h | grep CUDNN_MAJOR -A 2

    出现的三个数字==版本号

四、pytorch

官网:Start Locally | PyTorch

  1. 官网命令

    pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118

    我用的conda xxx,没有conda用pip3 也行,据说pip3快

  2. 检查(依然在CMD)

    1. python # 如果报错,试python3
    2. import torch
    3. from torch.backends import cudnn
    4. print(cudnn.is_available())
    5. print(torch.backends.cudnn.version())
    6. print(torch.cuda.is_available()) # True,则gpu版本的pytorch安装成功
    7. print(torch.zeros(1).cuda())

    如果有python编译器,如pycharm、vscode,在那里面import print 更简单,直接复制。这里是照顾没装这些编译器的人

可能报错

#error -- unsupported GNU version! gcc versions later than 8 are not supported!-CSDN博客

Gcc多版本安装和切换_安装gcc新版本,并能切换-CSDN博客

「解决」ubuntu CUDA版本什么都对,但torch.cuda.is_available()是false-CSDN博客

参考

ubuntu下,安装配置CUDA_cuda安装教程 ubuntu-CSDN博客

 ubuntu20.04安装多版本cuda,切换版本_ubuntu 多版本nvcc-CSDN博客

【深度学习环境配置】ubuntu 20.04+4060 Ti+CUDA 11.8+pytorch(装机、显卡驱动、CUDA、cudnn、pytorch)_4060ti cuda cudnn-CSDN博客

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/947584
推荐阅读
相关标签
  

闽ICP备14008679号