직접 설치하기
사용 환경을 선택하고 설치 명령을 복사해서 실행해 보세요. Stable 버전은 테스트 및 지원되고 있는 가장 최근의 PyTorch 버전으로, 대부분의 사용자에게 적합합니다. Preview 버전은 아직 완전히 테스트나 지원이 되지 않는 최신 버전으로 매일 밤 업데이트됩니다. 사용 중인 패키지 매니저에 따라 아래의 사전 요구사항(예: numpy)이 충족되었는지 확인해 주세요. 모든 의존성을 설치할 수 있는 Anaconda를 패키지 매니저로 추천합니다. 이전 버전의 PyTorch도 설치할 수 있습니다. LibTorch는 C++에서만 지원합니다.
참고: 최신 버전의 PyTorch를 사용하기 위해서는 Python 3.8 이상이 필요합니다. 자세한 내용은 Python 섹션을 참고해주세요.
macOS에서 설치하기
PyTorch 는 macOS 에서 설치 및 사용할 수 있습니다. PyTorch를 설치할 시스템과 사용할 수 있는 GPU 에 따라, Mac에서의 처리 속도 측면에서의 PyTorch 사용 경험은 사람마다 다를 수 있습니다.
요구 사항
macOS 버전
PyTorch는 macOS 10.15 (Catalina) 이후 macOS에서 설치할 수 있습니다.
Python
Python 3.8 ~ 3.11 사이의 버전을 사용하기를 권장합니다. 해당 버전은 아나콘다 패키지 관리자 (아래 참조), HomeBrew, Python 웹사이트 에서 설치할 수 있습니다.
패키지 관리자
PyTorch 바이너리는 아나콘다 또는 pip 패키지 관리자를 통해 설치할 수 있습니다. Python과 PyTorch 설치 환경을 쉽게 격리할 수 있는 아나콘다를 권장하고 있습니다.
아나콘다
아나콘다는 그래픽 설치 인스톨러 또는 명령줄 인스톨러를 사용할 수 있습니다. 명령줄 인스톨러를 사용하는 경우, installer link를 복사하여 붙여넣거나 인텔 맥에서는 아래와 같이 실행할 수 있습니다.
# 설치 시기에 따라 아나콘다 버전은 다를 수 있습니다.
curl -O https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-x86_64.sh
sh Miniconda3-latest-MacOSX-x86_64.sh
# 프롬프트가 나오면 옵션을 선택합니다, 일반적으로 기본값을 사용합니다.
혹은 m1 맥의 경우 아래와 같이 실행할 수 있습니다.
# 설치 시기에 따라 아나콘다 버전은 다를 수 있습니다.
curl -O https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-arm64.sh
sh Miniconda3-latest-MacOSX-arm64.sh
# 프롬프트가 나오면 옵션을 선택합니다, 일반적으로 기본값을 사용합니다.
pip
Python 3
Homebrew나 Python 웹사이트에서 Python을 설치했다면, pip
도 같이 설치됩니다.
Python 3.x 를 설치했다면, pip3
를 사용합니다.
팁: 심볼릭 링크를 통해
pip3
대신pip
을 사용할 수도 있습니다.
설치
아나콘다
아나콘다를 사용할 경우, 아래와 같이 PyTorch를 설치할 수 있습니다.
conda install pytorch torchvision -c pytorch
pip
pip을 사용할 경우, 아래와 같이 설치할 수 있습니다.
# Python 3.x
pip3 install torch torchvision
검증
PyTorch가 제대로 설치되었는지 확인하기 위해, 아래와 같은 샘플 코드를 실행해 볼 수 있습니다. 아래는 무작위로 초기화된 tensor를 생성해보는 샘플 코드입니다.
import torch
x = torch.rand(5, 3)
print(x)
출력 결과는 아래와 비슷한 형태여야 합니다.
tensor([[0.3380, 0.3845, 0.3217],
[0.8337, 0.9050, 0.2650],
[0.2979, 0.7141, 0.9069],
[0.1449, 0.1132, 0.1375],
[0.4675, 0.3947, 0.1426]])
소스에서 빌드
대부분의 PyTorch 사용자들은, 패키지 관리자를 통해 사전에 빌드된 바이너리를 사용하는 것이 제일 좋습니다. 정식으로 릴리즈 되지 않은 최신 PyTorch 코드를 사용하려고 하거나, PyTorch core에 대한 테스트나 개발을 하는 경우에는 직접 PyTorch를 빌드해야합니다.
요구사항
- [선택사항] 아나콘다 설치
- https://github.com/pytorch/pytorch#from-source 참조하여 빌드 (영문)
위 섹션을 참조하여 잘 설치되었는지 검증 할 수 있습니다.
Installing on Linux
PyTorch can be installed and used on various Linux distributions. Depending on your system and compute requirements, your experience with PyTorch on Linux may vary in terms of processing time. It is recommended, but not required, that your Linux system has an NVIDIA or AMD GPU in order to harness the full power of PyTorch’s CUDA support or ROCm support.
Prerequisites
Supported Linux Distributions
PyTorch is supported on Linux distributions that use glibc >= v2.17, which include the following:
- Arch Linux, minimum version 2012-07-15
- CentOS, minimum version 7.3-1611
- Debian, minimum version 8.0
- Fedora, minimum version 24
- Mint, minimum version 14
- OpenSUSE, minimum version 42.1
- PCLinuxOS, minimum version 2014.7
- Slackware, minimum version 14.2
- Ubuntu, minimum version 13.04
The install instructions here will generally apply to all supported Linux distributions. An example difference is that your distribution may support
yum
instead ofapt
. The specific examples shown were run on an Ubuntu 18.04 machine.
Python
Python 3.8-3.11 is generally installed by default on any of our supported Linux distributions, which meets our recommendation.
Tip: By default, you will have to use the command
python3
to run Python. If you want to use just the commandpython
, instead ofpython3
, you can symlinkpython
to thepython3
binary.
However, if you want to install another version, there are multiple ways:
- APT
- Python website
If you decide to use APT, you can run the following command to install it:
sudo apt install python
If you use Anaconda to install PyTorch, it will install a sandboxed version of Python that will be used for running PyTorch applications.
Package Manager
To install the PyTorch binaries, you will need to use one of two supported package managers: Anaconda or pip. Anaconda is the recommended package manager as it will provide you all of the PyTorch dependencies in one, sandboxed install, including Python.
Anaconda
To install Anaconda, you will use the command-line installer. Right-click on the 64-bit installer link, select Copy Link Location
, and then use the following commands:
# The version of Anaconda may be different depending on when you are installing`
curl -O https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
sh Miniconda3-latest-Linux-x86_64.sh
# and follow the prompts. The defaults are generally good.`
You may have to open a new terminal or re-source your
~/.bashrc
to get access to theconda
command.
pip
Python 3
While Python 3.x is installed by default on Linux, pip
is not installed by default.
sudo apt install python3-pip
Tip: If you want to use just the command
pip
, instead ofpip3
, you can symlinkpip
to thepip3
binary.
Installation
Anaconda
No CUDA/ROCm
To install PyTorch via Anaconda, and do not have a CUDA-capable or ROCm-capable system or do not require CUDA/ROCm (i.e. GPU support), in the above selector, choose OS: Linux, Package: Conda, Language: Python and Compute Platform: CPU. Then, run the command that is presented to you.
With CUDA
To install PyTorch via Anaconda, and you do have a CUDA-capable system, in the above selector, choose OS: Linux, Package: Conda and the CUDA version suited to your machine. Often, the latest CUDA version is better. Then, run the command that is presented to you.
With ROCm
PyTorch via Anaconda is not supported on ROCm currently. Please use pip instead.
pip
No CUDA
To install PyTorch via pip, and do not have a CUDA-capable or ROCm-capable system or do not require CUDA/ROCm (i.e. GPU support), in the above selector, choose OS: Linux, Package: Pip, Language: Python and Compute Platform: CPU. Then, run the command that is presented to you.
With CUDA
To install PyTorch via pip, and do have a CUDA-capable system, in the above selector, choose OS: Linux, Package: Pip, Language: Python and the CUDA version suited to your machine. Often, the latest CUDA version is better. Then, run the command that is presented to you.
With ROCm
To install PyTorch via pip, and do have a ROCm-capable system, in the above selector, choose OS: Linux, Package: Pip, Language: Python and the ROCm version supported. Then, run the command that is presented to you.
Verification
To ensure that PyTorch was installed correctly, we can verify the installation by running sample PyTorch code. Here we will construct a randomly initialized tensor.
import torch
x = torch.rand(5, 3)
print(x)
The output should be something similar to:
tensor([[0.3380, 0.3845, 0.3217],
[0.8337, 0.9050, 0.2650],
[0.2979, 0.7141, 0.9069],
[0.1449, 0.1132, 0.1375],
[0.4675, 0.3947, 0.1426]])
Additionally, to check if your GPU driver and CUDA/ROCm is enabled and accessible by PyTorch, run the following commands to return whether or not the GPU driver is enabled (the ROCm build of PyTorch uses the same semantics at the python API level (https://github.com/pytorch/pytorch/blob/master/docs/source/notes/hip.rst#hip-interfaces-reuse-the-cuda-interfaces), so the below commands should also work for ROCm):
import torch
torch.cuda.is_available()
Building from source
For the majority of PyTorch users, installing from a pre-built binary via a package manager will provide the best experience. However, there are times when you may want to install the bleeding edge PyTorch code, whether for testing or actual development on the PyTorch core. To install the latest PyTorch code, you will need to build PyTorch from source.
Prerequisites
- Install Anaconda or Pip
- If you need to build PyTorch with GPU support a. for NVIDIA GPUs, install CUDA, if your machine has a CUDA-enabled GPU. b. for AMD GPUs, install ROCm, if your machine has a ROCm-enabled GPU
- Follow the steps described here: https://github.com/pytorch/pytorch#from-source
You can verify the installation as described above.
Installing on Windows
PyTorch can be installed and used on various Windows distributions. Depending on your system and compute requirements, your experience with PyTorch on Windows may vary in terms of processing time. It is recommended, but not required, that your Windows system has an NVIDIA GPU in order to harness the full power of PyTorch’s CUDA support.
Prerequisites
Supported Windows Distributions
PyTorch is supported on the following Windows distributions:
- Windows 7 and greater; Windows 10 or greater recommended.
- Windows Server 2008 r2 and greater
The install instructions here will generally apply to all supported Windows distributions. The specific examples shown will be run on a Windows 10 Enterprise machine
Python
Currently, PyTorch on Windows only supports Python 3.8-3.10; Python 2.x is not supported.
As it is not installed by default on Windows, there are multiple ways to install Python:
If you use Anaconda to install PyTorch, it will install a sandboxed version of Python that will be used for running PyTorch applications.
If you decide to use Chocolatey, and haven’t installed Chocolatey yet, ensure that you are running your command prompt as an administrator.
For a Chocolatey-based install, run the following command in an administrative command prompt:
choco install python
Package Manager
To install the PyTorch binaries, you will need to use at least one of two supported package managers: Anaconda and pip. Anaconda is the recommended package manager as it will provide you all of the PyTorch dependencies in one, sandboxed install, including Python and pip.
Anaconda
To install Anaconda, you will use the 64-bit graphical installer for PyTorch 3.x. Click on the installer link and select Run
. Anaconda will download and the installer prompt will be presented to you. The default options are generally sane.
pip
If you installed Python by any of the recommended ways above, pip will have already been installed for you.
Installation
Anaconda
To install PyTorch with Anaconda, you will need to open an Anaconda prompt via Start | Anaconda3 | Anaconda Prompt
.
No CUDA
To install PyTorch via Anaconda, and do not have a CUDA-capable system or do not require CUDA, in the above selector, choose OS: Windows, Package: Conda and CUDA: None. Then, run the command that is presented to you.
With CUDA
To install PyTorch via Anaconda, and you do have a CUDA-capable system, in the above selector, choose OS: Windows, Package: Conda and the CUDA version suited to your machine. Often, the latest CUDA version is better. Then, run the command that is presented to you.
pip
No CUDA
To install PyTorch via pip, and do not have a CUDA-capable system or do not require CUDA, in the above selector, choose OS: Windows, Package: Pip and CUDA: None. Then, run the command that is presented to you.
With CUDA
To install PyTorch via pip, and do have a CUDA-capable system, in the above selector, choose OS: Windows, Package: Pip and the CUDA version suited to your machine. Often, the latest CUDA version is better. Then, run the command that is presented to you.
Verification
To ensure that PyTorch was installed correctly, we can verify the installation by running sample PyTorch code. Here we will construct a randomly initialized tensor.
From the command line, type:
python
then enter the following code:
import torch
x = torch.rand(5, 3)
print(x)
The output should be something similar to:
tensor([[0.3380, 0.3845, 0.3217],
[0.8337, 0.9050, 0.2650],
[0.2979, 0.7141, 0.9069],
[0.1449, 0.1132, 0.1375],
[0.4675, 0.3947, 0.1426]])
Additionally, to check if your GPU driver and CUDA is enabled and accessible by PyTorch, run the following commands to return whether or not the CUDA driver is enabled:
import torch
torch.cuda.is_available()
Building from source
For the majority of PyTorch users, installing from a pre-built binary via a package manager will provide the best experience. However, there are times when you may want to install the bleeding edge PyTorch code, whether for testing or actual development on the PyTorch core. To install the latest PyTorch code, you will need to build PyTorch from source.
Prerequisites
- Install Anaconda
- Install CUDA, if your machine has a CUDA-enabled GPU.
- If you want to build on Windows, Visual Studio with MSVC toolset, and NVTX are also needed. The exact requirements of those dependencies could be found out here.
- Follow the steps described here: https://github.com/pytorch/pytorch#from-source
You can verify the installation as described above.