How to Install PyTorch Using Pip
James Reed
Infrastructure Engineer · Leapcell

Key Takeaways
- PyTorch can be easily installed using pip with simple commands.
- Choose the correct installation command based on your system and GPU support.
- Always verify the installation to ensure PyTorch works as expected.
Introduction
PyTorch is a popular open‑source deep learning framework developed by Meta AI and maintained by the PyTorch Foundation . It supports tensor computations, dynamic neural networks, GPU acceleration via CUDA or ROCm, and is widely used in research and industry.
1. Prerequisites
Before installing PyTorch via pip
, make sure you have:
-
Python (version 3.9 or later recommended)
-
pip installed and up‑to‑date:
python -m pip install --upgrade pip
-
(Optional) A CUDA-capable NVIDIA GPU or ROCm-capable AMD GPU if you want GPU acceleration
2. Choosing the Right pip install
Command
Head to the official “Get Started” page to pick your OS, Python version, and compute platform . Here are common installation commands based on your setup:
A. CPU‑only installation (no GPU)
pip3 install torch torchvision torchaudio
This installs the latest stable CPU version. Easy and fast for systems without GPUs.
B. With CUDA support
Replace cu118
with whatever CUDA version you have (for example, cu120
or cu117
):
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
The --index-url
option points pip
to PyTorch’s CUDA-enabled binary repository .
C. Earlier PyTorch versions (optional)
If you need a specific past version (e.g., v2.6.0):
pip install torch==2.6.0 torchvision==0.21.0 torchaudio==2.6.0 --index-url https://download.pytorch.org/whl/cu118
See the “previous versions” section for more options .
3. Common Installation Issues
-
Unsupported Python version PyTorch requires Python 3.9+. If you use 3.10+ or an unsupported OS, you might see
No matching distribution found
. Downgrading Python or installing via Conda can help. -
Cache-related problems If installations fail, try forcing a fresh install:
pip install torch --upgrade --force-reinstall --no-cache-dir --index-url https://download.pytorch.org/whl/cu118
This can fix issues related to corrupted or outdated cached packages .
-
Virtual environment mistakes Always activate your virtual environment before running
pip
. Otherwise PyTorch may install into the system Python instead .
4. Verifying the Installation
After installation, confirm that everything works:
-
Check package info
pip3 show torch
-
Launch Python and test:
import torch x = torch.rand(5, 3) print(x) print(torch.cuda.is_available())
- You should see a random tensor printed
torch.cuda.is_available()
returnsTrue
if CUDA is correctly enabled
5. Optional: Uninstalling PyTorch
To remove PyTorch at any time:
pip uninstall torch torchvision torchaudio
6. Summary Table
Scenario | Command |
---|---|
CPU-only | pip3 install torch torchvision torchaudio |
CUDA GPU | pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cuXXX |
Specific version | pip install torch==x.y.z torchvision==… --index-url … |
Force reinstall | Add --upgrade --force-reinstall --no-cache-dir |
Verify | pip3 show torch , then test with Python script |
Uninstall | pip uninstall torch torchvision torchaudio |
7. Conclusion
Installing PyTorch with pip is straightforward:
- Ensure you have Python (3.9+) and pip
- Choose the correct
pip install
command for CPU or CUDA - Activate your environment
- Verify the installation using a simple script
With PyTorch set up, you’re ready to dive into model building, training, and GPU-accelerated research. Happy coding!
FAQs
Use pip with the official command that matches your Python and system requirements.
Import torch in Python and check with torch.cuda.is_available().
Try upgrading pip, using a virtual environment, or adding --no-cache-dir and the correct index URL.
We are Leapcell, your top choice for hosting backend projects.
Leapcell is the Next-Gen Serverless Platform for Web Hosting, Async Tasks, and Redis:
Multi-Language Support
- Develop with Node.js, Python, Go, or Rust.
Deploy unlimited projects for free
- pay only for usage — no requests, no charges.
Unbeatable Cost Efficiency
- Pay-as-you-go with no idle charges.
- Example: $25 supports 6.94M requests at a 60ms average response time.
Streamlined Developer Experience
- Intuitive UI for effortless setup.
- Fully automated CI/CD pipelines and GitOps integration.
- Real-time metrics and logging for actionable insights.
Effortless Scalability and High Performance
- Auto-scaling to handle high concurrency with ease.
- Zero operational overhead — just focus on building.
Explore more in the Documentation!
Follow us on X: @LeapcellHQ