Last modified: Mar 25, 2025 By Alexander Williams
How to Install PyTorch in Python
PyTorch is a popular machine learning library. It is used for deep learning projects. This guide will help you install PyTorch step by step.
Prerequisites
Before installing PyTorch, ensure you have Python installed. You can check this by running python --version
in your terminal.
python --version
Python 3.8.5
If Python is not installed, download it from the official website. Also, ensure you have pip installed. Pip is Python's package manager.
Install PyTorch Using Pip
The easiest way to install PyTorch is using pip. Open your terminal and run the following command.
pip install torch torchvision torchaudio
This command installs PyTorch along with torchvision and torchaudio. These are useful for computer vision and audio tasks.
Verify the Installation
After installation, verify PyTorch is installed correctly. Open Python in your terminal and run the following code.
import torch
print(torch.__version__)
1.9.0
If you see the version number, PyTorch is installed. If you get an error, check our guide on How To Solve ModuleNotFoundError: No module named in Python.
Install PyTorch with CUDA Support
For GPU acceleration, install PyTorch with CUDA support. First, check if your system has an NVIDIA GPU. Then, run the following command.
pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu113
This installs PyTorch with CUDA 11.3 support. Replace cu113 with your CUDA version if needed.
Using Conda for Installation
If you use Anaconda, you can install PyTorch with conda. Run the following command in your terminal.
conda install pytorch torchvision torchaudio cudatoolkit=11.3 -c pytorch
This installs PyTorch with CUDA toolkit 11.3. Adjust the version as per your requirements.
Common Installation Issues
Sometimes, you may face issues during installation. Ensure your pip or conda is updated. Run pip install --upgrade pip
to update pip.
If you encounter dependency conflicts, create a virtual environment. This isolates your project dependencies. Learn more about virtual environments here.
Conclusion
Installing PyTorch is simple with pip or conda. Follow the steps above to set it up. Verify the installation to ensure everything works. Now you are ready to start your machine learning projects with PyTorch.