Ubuntu2022.04+cuda12.1+torch2.1

1 Change source

Domestic sources have obvious speed advantages. Commonly used ones include Tsinghua University, Alibaba, etc.

# 1) System software source
# vim /etc/apt/sources.list
# The source code image is commented by default to improve the speed of apt update. You can uncomment it yourself if necessary.
----------
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse

deb http://security.ubuntu.com/ubuntu/ jammy-security main restricted universe multiverse
# deb-src http://security.ubuntu.com/ubuntu/ jammy-security main restricted universe multiverse

# Pre-release software source, not recommended to be enabled
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse
# # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse
-------------------
# 2) pip source
$ sudo apt update
$ sudo apt install python3-pip
$ pip install pip -U #Upgrade to the latest version
$ pip --version
$ pip 22.0.2 from /usr/lib/python3/dist-packages/pip (python 3.10)

# Modify pip source to Tsinghua source
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

2 Download Anaconda

 wget https://repo.anaconda.com/archive/Anaconda3-2023.09-0-Linux-x86_64.sh
 bash Anaconda3-2023.09-0-Linux-x86_64.sh
 source ~/.bashrc
 vim ~/.bashrc
 -------------------
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/home/woodman/anaconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
    eval "$__conda_setup"
else
    if [ -f "/home/woodman/anaconda3/etc/profile.d/conda.sh" ]; then
        . "/home/woodman/anaconda3/etc/profile.d/conda.sh"
    else
        export PATH="/home/woodman/anaconda3/bin:$PATH"
    fi
fi
unset __conda_setup
# <<< conda initialize <<<
conda deactivate
#export PATH=/home/woodman/anaconda3/bin:$PATH
# The following are my terminal interface settings, personal habits, changes or comments
export PS1="\[\e]0;\u@ \w\a\]${debian_chroot: + ($debian_chroot)}\[\033[01;32m\]\u\[\033[01;32m \]:[\W]$"
----------------------------------
# Conda also changed its source to Tsinghua University
$ conda config --set show_channel_urls yes #Modify it after generating the ~/.condarc.
 ----
channels:
  -defaults
show_channel_urls: true
default_channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
custom_channels:
  conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  pytorch-lts: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
----
conda clean -i clears the index cache
conda config --show-sources 

3 Creation and deletion of Anaconda environment

# Commonly used software is installed when creating
$conda create -n woodman python=3.11 ipykernel psutil jupyter jupyterlab nodejs numpy matplotlib

$conda activate woodman
$python -m ipykernel install --user --name woodman --display-name "torch2.1"
$cat ~/.local/share/jupyter/kernels/woodman/kernel.json

$jupyter kernelspec list
$jupyter kernelspec remove kernel_name

# delete
$ conda remove -n your_env_name (virtual environment name) --all
$ conda remove --name your_env_name package_name # Delete a package in the environment

# Back up installed software
conda list -e > requirements.txt
conda install --yes --file requirements.txt

pip freeze > requirements.txt
pip install -r requirements.txt


#Smoothly install torch again.
Correspondence between torch, torchvision, torchaudio and torchtext versions in #PyTorch
#https://blog.csdn.net/shiwanghualuo/article/details/122860521

#CPU
$pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu

#GPU,cu121 is nvidia-smi: CUDA Version: 12.0
$pip install torch==2.1.0 + cu121 torchvision==0.16.0 + cu121 torchaudio==2.1.0 + cu121 -f https://download.pytorch.org/whl/torch_stable.html

4 Nvidia f u

The driver installation of the graphics card should have been placed second, but considering its complexity, I put it last.
Let’s go through the various links first. In fact, all the required URLs and commands are listed later.

https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html
https://developer.nvidia.com/cuda-downloads
https://developer.nvidia.com/CUDA-toolkit-archive

# Version correspondence table between CUDA and NVIDIA Driver
https://docs.nvidia.com/cuda/cuda-toolkit-release-notes/index.html

To download a new version of cuda, I usually use the method (1)

(1)
# Driver Installer
# Check the driver version supported by the graphics card, generally download "recommended"
# The server version can better adapt to the alchemy environment
$ubuntu-drivers devices
----
driver: nvidia-driver-535-server-open - distro non-free recommended
--------
$ubuntu-drivers install nvidia-driver-535-server-open

# Base Installer
# “recommended”
# Installation Instructions:
wget https://developer.download.nvidia.com/compute/cuda/12.3.0/local_installers/cuda_12.3.0_545.23.06_linux.run
# Only select Cuda Toolkit XX.X and Driver installed in front, others are not required.
sudo sh cuda_12.3.0_545.23.06_linux.run

# After the installation is complete, there are cuda and cuda-12.1 in the /usr/local/ path.
# Note that cuda is a soft link, pointing to cuda-12.1

$cd /usr/local/
$file cuda
(2)
# Base Installer
# Installation Instructions:
wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-ubuntu2204.pin
sudo mv cuda-ubuntu2204.pin /etc/apt/preferences.d/cuda-repository-pin-600
wget https://developer.download.nvidia.com/compute/cuda/12.3.0/local_installers/cuda-repo-ubuntu2204-12-3-local_12.3.0-545.23.06-1_amd64.deb
sudo dpkg -i cuda-repo-ubuntu2204-12-3-local_12.3.0-545.23.06-1_amd64.deb
sudo cp /var/cuda-repo-ubuntu2204-12-3-local/cuda-*-keyring.gpg /usr/share/keyrings/
sudo apt-get update
sudo apt-get -y install cuda-toolkit-12-3

#DriverInstaller
#NVIDIA Driver Instructions (choose one option)

#To install the legacy kernel module flavor:
sudo apt-get install -y cuda-drivers
#To install the open kernel module flavor:
sudo apt-get install -y nvidia-kernel-open-545
sudo apt-get install -y cuda-drivers-545
-------
# If it is already installed, delete it first and then install it.
sudo apt-get --purge remove nvidia-kernel-source-XXX
sudo apt-get install --verbose-versions nvidia-kernel-open-XXX
sudo apt-get install --verbose-versions cuda-drivers-XXX

System environment configuration

$ sudo vim ~/.bashrc
----------
#cuda-20231111
export CUDA_HOME=$CUDA_HOME:/usr/local/cuda
export PATH=/usr/local/cuda/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH
----------------
$ source ~/.bashrc
$ nvcc -V

Install cuDnn

It will be faster to surf the Internet scientifically. Just register an account with QQ email.

tar -xf cudnn-linux-x86_64-8.9.5.30_cuda12-archive.tar.xz
mv cudnn-linux-x86_64-8.9.5.30_cuda12-archive cuda

sudo cp cuda/include/cudnn*.h /usr/local/cuda/include
sudo cp cuda/lib/libcudnn* /usr/local/cuda/lib64
sudo chmod a + r /usr/local/cuda/include/cudnn*.h /usr/local/cuda/lib64/libcudnn*

#Verify whether the installation is successful

cat /usr/local/cuda/include/cudnn_version.h | grep CUDNN_MAJOR -A 2

Reference:

3090 graphics card + CUDA11.0 + torch1.7.1_torch1.7.1 and cuda11.0 how to download – CSDN blog

[NVIDIA] Ubuntu 20.04 installation nvidia-460 + cuda-11.2_a100 460 cuda11.2-CSDN Blog

U20.4 upgrade pytorch 1.11_pip upgrade torch_woodman718’s blog-CSDN blog