How to install PyTorch
Quick answer
Install
PyTorch by running the official command from pytorch.org tailored to your OS, Python version, and CUDA support. Use pip install torch torchvision torchaudio for CPU-only or add CUDA version tags for GPU support.PREREQUISITES
Python 3.8+pip or conda installedInternet connection
Setup
Ensure you have Python 3.8 or higher installed. Use pip or conda as your package manager. Verify your Python version with python --version and pip with pip --version.
python --version
pip --version output
Python 3.10.12 pip 23.1.2 from /usr/local/lib/python3.10/site-packages/pip (python 3.10)
Step by step
Run the official PyTorch install command from pytorch.org. For CPU-only installation, use the following command:
pip install torch torchvision torchaudio output
Collecting torch Collecting torchvision Collecting torchaudio Successfully installed torch-2.1.0 torchvision-0.16.0 torchaudio-2.1.0
Common variations
For GPU support, specify the CUDA version. For example, for CUDA 11.8, run:
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118 output
Collecting torch Collecting torchvision Collecting torchaudio Successfully installed torch-2.1.0+cu118 torchvision-0.16.0+cu118 torchaudio-2.1.0
Troubleshooting
If you encounter permission errors, add --user to the pip command or use a virtual environment. For missing CUDA errors, verify your GPU drivers and CUDA toolkit installation.
pip install torch torchvision torchaudio --user output
Collecting torch Collecting torchvision Collecting torchaudio Successfully installed torch torchvision torchaudio
Key Takeaways
- Use the official PyTorch install command from pytorch.org tailored to your system.
- Specify CUDA version in the pip command for GPU acceleration support.
- Use virtual environments or --user flag to avoid permission issues during installation.