Install PyTorch (安装 PyTorch)

Install PyTorch {安装 PyTorch}

  • [1. Install PyTorch](#1. Install PyTorch)
    • [1.1. Previous PyTorch Versions](#1.1. Previous PyTorch Versions)
    • [1.2. Latest PyTorch](#1.2. Latest PyTorch)
    • [1.3. 查看 PyTorch 的版本](#1.3. 查看 PyTorch 的版本)
  • References

1. Install PyTorch

https://pytorch.org/

Select your preferences and run the install command. Stable represents the most currently tested and supported version of PyTorch. This should be suitable for many users. Preview is available if you want the latest, not fully tested and supported, builds that are generated nightly. Please ensure that you have met the prerequisites below (e.g., numpy), depending on your package manager. Anaconda is our recommended package manager since it installs all dependencies. Note that LibTorch is only available for C++.

1.1. Previous PyTorch Versions

https://pytorch.org/get-started/previous-versions/

  • v2.5.0

Conda - Linux and Windows

# CUDA 11.8
conda install pytorch==2.5.0 torchvision==0.20.0 torchaudio==2.5.0 pytorch-cuda=11.8 -c pytorch -c nvidia
# CUDA 12.1
conda install pytorch==2.5.0 torchvision==0.20.0 torchaudio==2.5.0 pytorch-cuda=12.1 -c pytorch -c nvidia
# CUDA 12.4
conda install pytorch==2.5.0 torchvision==0.20.0 torchaudio==2.5.0 pytorch-cuda=12.4 -c pytorch -c nvidia
# CPU Only
conda install pytorch==2.5.0 torchvision==0.20.0 torchaudio==2.5.0 cpuonly -c pytorch
  • v2.4.0

Conda - Linux and Windows

# CUDA 11.8
conda install pytorch==2.4.0 torchvision==0.19.0 torchaudio==2.4.0 pytorch-cuda=11.8 -c pytorch -c nvidia
# CUDA 12.1
conda install pytorch==2.4.0 torchvision==0.19.0 torchaudio==2.4.0 pytorch-cuda=12.1 -c pytorch -c nvidia
# CUDA 12.4
conda install pytorch==2.4.0 torchvision==0.19.0 torchaudio==2.4.0 pytorch-cuda=12.4 -c pytorch -c nvidia
# CPU Only
conda install pytorch==2.4.0 torchvision==0.19.0 torchaudio==2.4.0 cpuonly -c pytorch

1.2. Latest PyTorch

Latest PyTorch requires Python 3.9 or later.

conda install pytorch torchvision torchaudio cpuonly -c pytorch

(base) yongqiang@yongqiang:~$ conda install pytorch torchvision torchaudio cpuonly -c pytorch
Channels:
 - pytorch
 - defaults
 - https://repo.anaconda.com/pkgs/main
Platform: linux-64
Collecting package metadata (repodata.json): done
Solving environment: done

## Package Plan ##

  environment location: /home/yongqiang/miniconda3

  added / updated specs:
    - cpuonly
    - pytorch
    - torchaudio
    - torchvision


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    ca-certificates-2024.11.26 |       h06a4308_0         131 KB  defaults
    conda-24.11.0              |  py311h06a4308_0         1.2 MB  defaults
    cpuonly-2.0                |                0           2 KB  pytorch
    ffmpeg-4.3                 |       hf484d3e_0         9.9 MB  pytorch
    filelock-3.13.1            |  py311h06a4308_0          24 KB  defaults
    giflib-5.2.2               |       h5eee18b_0          80 KB  defaults
    gmp-6.2.1                  |       h295c915_3         544 KB  defaults
    gmpy2-2.1.2                |  py311hc9b5ff0_0         191 KB  defaults
    gnutls-3.6.15              |       he1e5248_0         1.0 MB  defaults
    jinja2-3.1.4               |  py311h06a4308_1         362 KB  defaults
    lame-3.100                 |       h7b6447c_0         323 KB  defaults
    libidn2-2.3.4              |       h5eee18b_0         146 KB  defaults
    libjpeg-turbo-2.0.0        |       h9bf148f_0         950 KB  pytorch
    libtasn1-4.19.0            |       h5eee18b_0          63 KB  defaults
    libunistring-0.9.10        |       h27cfd23_0         536 KB  defaults
    libwebp-1.3.2              |       h11a3e52_0          87 KB  defaults
    llvm-openmp-14.0.6         |       h9e868ea_0         4.4 MB  defaults
    markupsafe-2.1.3           |  py311h5eee18b_0          25 KB  defaults
    mpc-1.1.0                  |       h10f8cd9_1          90 KB  defaults
    mpfr-4.0.2                 |       hb69a4c5_1         487 KB  defaults
    mpmath-1.3.0               |  py311h06a4308_0         1.0 MB  defaults
    nettle-3.7.3               |       hbbd107a_1         809 KB  defaults
    networkx-3.3               |  py311h06a4308_0         3.1 MB  defaults
    openh264-2.1.1             |       h4ff587b_0         711 KB  defaults
    pytorch-2.5.1              |     py3.11_cpu_0        92.0 MB  pytorch
    pytorch-mutex-1.0          |              cpu           3 KB  pytorch
    pyyaml-6.0.2               |  py311h5eee18b_0         223 KB  defaults
    sympy-1.13.2               |  py311h06a4308_0        15.4 MB  defaults
    torchaudio-2.5.1           |        py311_cpu         5.1 MB  pytorch
    torchvision-0.20.1         |        py311_cpu         7.1 MB  pytorch
    typing_extensions-4.11.0   |  py311h06a4308_0          73 KB  defaults
    yaml-0.2.5                 |       h7b6447c_0          75 KB  defaults
    ------------------------------------------------------------
                                           Total:       146.1 MB

The following NEW packages will be INSTALLED:

  cpuonly            pytorch/noarch::cpuonly-2.0-0
  ffmpeg             pytorch/linux-64::ffmpeg-4.3-hf484d3e_0
  filelock           anaconda/pkgs/main/linux-64::filelock-3.13.1-py311h06a4308_0
  giflib             anaconda/pkgs/main/linux-64::giflib-5.2.2-h5eee18b_0
  gmp                anaconda/pkgs/main/linux-64::gmp-6.2.1-h295c915_3
  gmpy2              anaconda/pkgs/main/linux-64::gmpy2-2.1.2-py311hc9b5ff0_0
  gnutls             anaconda/pkgs/main/linux-64::gnutls-3.6.15-he1e5248_0
  jinja2             anaconda/pkgs/main/linux-64::jinja2-3.1.4-py311h06a4308_1
  lame               anaconda/pkgs/main/linux-64::lame-3.100-h7b6447c_0
  libidn2            anaconda/pkgs/main/linux-64::libidn2-2.3.4-h5eee18b_0
  libjpeg-turbo      pytorch/linux-64::libjpeg-turbo-2.0.0-h9bf148f_0
  libtasn1           anaconda/pkgs/main/linux-64::libtasn1-4.19.0-h5eee18b_0
  libunistring       anaconda/pkgs/main/linux-64::libunistring-0.9.10-h27cfd23_0
  libwebp            anaconda/pkgs/main/linux-64::libwebp-1.3.2-h11a3e52_0
  llvm-openmp        anaconda/pkgs/main/linux-64::llvm-openmp-14.0.6-h9e868ea_0
  markupsafe         anaconda/pkgs/main/linux-64::markupsafe-2.1.3-py311h5eee18b_0
  mpc                anaconda/pkgs/main/linux-64::mpc-1.1.0-h10f8cd9_1
  mpfr               anaconda/pkgs/main/linux-64::mpfr-4.0.2-hb69a4c5_1
  mpmath             anaconda/pkgs/main/linux-64::mpmath-1.3.0-py311h06a4308_0
  nettle             anaconda/pkgs/main/linux-64::nettle-3.7.3-hbbd107a_1
  networkx           anaconda/pkgs/main/linux-64::networkx-3.3-py311h06a4308_0
  openh264           anaconda/pkgs/main/linux-64::openh264-2.1.1-h4ff587b_0
  pytorch            pytorch/linux-64::pytorch-2.5.1-py3.11_cpu_0
  pytorch-mutex      pytorch/noarch::pytorch-mutex-1.0-cpu
  pyyaml             anaconda/pkgs/main/linux-64::pyyaml-6.0.2-py311h5eee18b_0
  sympy              anaconda/pkgs/main/linux-64::sympy-1.13.2-py311h06a4308_0
  torchaudio         pytorch/linux-64::torchaudio-2.5.1-py311_cpu
  torchvision        pytorch/linux-64::torchvision-0.20.1-py311_cpu
  typing_extensions  anaconda/pkgs/main/linux-64::typing_extensions-4.11.0-py311h06a4308_0
  yaml               anaconda/pkgs/main/linux-64::yaml-0.2.5-h7b6447c_0

The following packages will be UPDATED:

  ca-certificates                      2024.9.24-h06a4308_0 --> 2024.11.26-h06a4308_0
  conda                              24.9.2-py311h06a4308_0 --> 24.11.0-py311h06a4308_0


Proceed ([y]/n)? y


Downloading and Extracting Packages:

Preparing transaction: done
Verifying transaction: done
Executing transaction: done
(base) yongqiang@yongqiang:~$

1.3. 查看 PyTorch 的版本

(base) yongqiang@yongqiang:~$ python
Python 3.11.4 (main, Jul  5 2023, 13:45:01) [GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> print(torch.__version__)
2.5.1
>>> exit()
(base) yongqiang@yongqiang:~$

References

[1] Yongqiang Cheng, https://yongqiang.blog.csdn.net/