Installation
PyTorch can be installed via package managers or built from source. Choose the installation method that best fits your needs.Prerequisites
Before installing PyTorch, ensure you have:- Python 3.10 or later
- pip or conda package manager
- (Optional) CUDA-capable GPU for GPU acceleration
Quick Install
Install with pip
Install with conda
Verify Installation
After installation, verify that PyTorch is working correctly:Building from Source
Prerequisites for Building
If you are installing from source, you will need:- Python 3.10 or later
- A C++20 compatible compiler (gcc 11.3.0+ on Linux, Clang on macOS)
- At least 10 GB of free disk space
- 30-60 minutes for the initial build
On Windows, you’ll need Visual Studio 2022 or Visual Studio Build Tools. The build tools can be downloaded from https://visualstudio.microsoft.com/visual-cpp-build-tools/
Step 1: Set up environment
Step 2: Clone PyTorch source
Step 3: Install dependencies
Step 4: Build PyTorch
Build Options
You can customize the build with environment variables:| Variable | Description | Default |
|---|---|---|
USE_CUDA | Enable CUDA support | 1 (if CUDA detected) |
USE_ROCM | Enable ROCm support | 0 |
USE_XPU | Enable Intel GPU support | 0 |
USE_DISTRIBUTED | Enable distributed training | 1 |
BUILD_TEST | Build C++ tests | 1 |
USE_MKLDNN | Use oneDNN for CPU acceleration | 1 |
Docker Installation
You can also use pre-built Docker images:PyTorch uses shared memory for multiprocessing. Use
--ipc=host or --shm-size to increase shared memory size when using Docker.Platform-Specific Notes
NVIDIA Jetson Platforms
Python wheels for Jetson Nano, TX1/TX2, Xavier NX/AGX, and AGX Orin are available at the NVIDIA Developer Forums. They require JetPack 4.2 and above.Windows with Visual Studio
On Windows, PyTorch supports Visual Studio 2019/2022 and Ninja as build generators. Ifninja.exe is detected in PATH, Ninja will be used as the default generator.
Troubleshooting
ImportError: cannot import name '_initExtension'
ImportError: cannot import name '_initExtension'
This typically means PyTorch’s C extensions failed to build or install correctly. Try:
- Reinstalling with
pip install --force-reinstall torch - Building from source with verbose output:
python -m pip install --no-build-isolation -v -e . - Checking that all dependencies are installed
CUDA out of memory errors
CUDA out of memory errors
If you encounter out-of-memory errors:
- Reduce batch size
- Use gradient checkpointing for large models
- Enable mixed precision training with
torch.cuda.amp - Clear cache with
torch.cuda.empty_cache()
Slow build from source
Slow build from source
Initial builds take 30-60 minutes. Subsequent rebuilds are much faster. To speed up:
- Use
MAX_JOBSenvironment variable to control parallelism:export MAX_JOBS=4 - Disable tests if not needed:
export BUILD_TEST=0 - Use
ccachefor C++ compilation caching
Next Steps
Quick Start Guide
Now that PyTorch is installed, learn the basics with our hands-on quick start guide