Create anaconda virtual environment in ubuntu environment and install pytorch-gpu version

1. Ubuntu environment

  • unbuntu:20.04
  • cuda:12.0
    insert image description here

2. Install version

  • cuda:11.3
  • cudnn:8.2.0
  • python:3.8
  • pytorch:1.10.0

1. Find the corresponding version

2. Installation environment steps

2.1 download cuda

  • Official website link: https://developer.nvidia.com/cuda-toolkit-archive
  • ubuntucudaThe highest matching environment version 12.0, I download11.3.0
    insert image description here
    insert image description here
  • install command
    wget https://developer.download.nvidia.com/compute/cuda/11.3.0/local_installers/cuda_11.3.0_465.19.01_linux.run
    sudo sh cuda_11.3.0_465.19.01_linux.run
    
    insert image description here
    • 选continue
      insert image description here
    • 填acceptinsert image description here
    • 电脑里有驱动,按空格取消【键盘最长的那个空白键】,把x去掉,选择install,安装
      insert image description here
    • 安装成功
      insert image description here
  • Configure environment variables
    • Open the configuration file, [I need to use administrative rights, otherwise the file cannot be modified, added in front sudo]
      sudo vi /etc/profile
      
    • Add configuration information [ cuda-11.3,11.3It is the version I downloaded cuda, please modify the number if the version is different]
      • Insertion method: linuxmodify the file command, press the key once i[ irepresents insertinsert], press the up, down, left, and right keys to modify the position of the mouse, and add data at the corresponding position
      • Save method: Click the upper left corner of the keyboard Esc, press and hold shift+:, and enter wq!Enter
      export PATH=//usr/local/cuda-11.3/bin:$PATH
      export LD_LIBRARY_PATH=/usr/local/cuda-11.3/lib64$LD_LIBRARY_PATH
      
      insert image description here
    • sourceconfiguration file
      source /etc/profile
      
  • Check if the installation is successful
    • Enter localthe path to view cudathe file name
      cd /usr/local
      ls -a
      
      insert image description here
    • Enter the relevant command [ cuda-11.3modify according to your own file name]
      cd /usr/local/cuda-11.3/samples/1_Utilities/deviceQuery
      sudo make
      ./deviceQuery
      
      insert image description here
    • The result Result=passwas successful
      insert image description here

2.2 install cudnn

  • Official website link: https://developer.nvidia.com/rdp/cudnn-archive
  • cudaThe version is 11.3.0, choose cudnn: 8.2.0, but I don’t see cuda为11.3.0the corresponding cudnnspecific version
    insert image description here
  • Unzip and copy to the corresponding folder
    • Unzip [Find the downloaded file directory to unzip]
    tar -xvf cudnn-11.3-linux-x64-v8.2.0.53.tgz
    
    insert image description here
    • Copy the file [the name of the decompressed file cuda]
      	cd cuda
      	sudo cp lib64/* /usr/local/cuda-11.3/lib64/
      	sudo cp include/* /usr/local/cuda-11.3/include/
      	sudo chmod a+r /usr/local/cuda-11.3/lib64/*
      	sudo chmod a+r /usr/local/cuda-11.3/include/*
      
    • view cuDNNversion
      cat /usr/local/cuda/include/cudnn_version.h | grep CUDNN_MAJOR -A 2
      
      insert image description here
    • Check whether ordinary users nvcccan use [I don’t know if it is an ordinary user, and the sudo command is not used]
      insert image description here

2.3 Steps to install pytorch

  • Create a virtual environment

    conda create --name env_torch_cuda113 python=3.8
    
  • Enter the virtual environment

    conda activate env_torch_cuda113
    
  • Installpytorch

    • condaCan't install it, it seems that it can condabe changed topip
    pip install torch==1.10.0+cu113 torchvision==0.11.0+cu113 torchaudio==0.10.0 -f https://download.pytorch.org/whl/torch_stable.html
    

    insert image description here

  • Whether the verification is successful

    torch.__version__
    
  • Check GPUavailability

    torch.cuda.is_available()
    

insert image description here

2.4 The virtual environment created by importing pycharm

  • Find anacondathe virtual environment directory pythoninterpreter
    • my environment is inaoaconda/dataapp/envs/env_torch_cuda113/bin/python.exe
      insert image description here
  • Test the code again
    import torch
    print(torch.__version__)
    print(torch.cuda.is_available())
    print(torch.cuda.device_count())
    print(torch.cuda.current_device())
    

insert image description here

The installation is complete! ! !

Summarize

  • ubuntu20.04It doesn't seem to be necessary cuda,cudnn, but I downloaded it
  • When installing , the relevant version requires only the installation command on pytorchthe official website , but it can’t be installed. I found a command and changed my version number to install it. I haven’t found any problems yet.condapip
  • Report an error later, talk about it later if there is a problem
  • end

Guess you like

Origin blog.csdn.net/m0_46926492/article/details/129544634