How to Install PyTorch & torchvision in a Python Virtual Environment (Step-by-Step Guide)

  • Home
  • Blog
  • How to Install PyTorch & torchvision in a Python Virtual Environment (Step-by-Step Guide)
INSTALL PYTHON VENV
DateSep 3, 2025

Install PyTorch & torchvision in Python Virtual Env


Why Use a Virtual Environment for PyTorch?

Imagine having different projects: one uses TensorFlow 2.x, another PyTorch 2.x, and yet another running legacy PyTorch 1.10. Without virtual environments, your packages would collide like roommates arguing over fridge space.

A Python venv = A sandbox just for your project.

  • No dependency conflicts.

  • Easy to recreate setups with requirements.txt.

  • Perfect for experimenting with different PyTorch + CUDA versions.

If you’re starting fresh, always install PyTorch in a Python venv—it’ll save you headaches.



Prerequisites Before Installation

Before we roll up our sleeves, make sure you’ve got:

  • Python 3.8 – 3.12 (latest PyTorch may not support very old releases).

  • pip upgraded:bashpython -m pip install --upgrade pip

  • OS: Works on Windows, macOS, Linux.

  • Optionally: CUDA drivers if you’re using a GPU and want GPU acceleration.



Step 1 – Create a Python Virtual Environment

Windows

bashpython -m venv myenv
myenv\Scripts\activate

macOS/Linux

bashpython3 -m venv myenv
source myenv/bin/activate

✅ When activated, your terminal prompt will show (myenv)—you’re inside your sandbox.



Step 2 – How to Download and Install PyTorch

Head to PyTorch’s official installation selector (on their website). It asks about your:

  • Package manager (pip or conda—here we’ll focus on pip).

  • Compute platform (CPU-only or CUDA).

  • Python version.


Example (CPU-only):

bashpip install torch

Example (CUDA 12.1 GPU support):

bashpip install torch --index-url https://download.pytorch.org/whl/cu121


⚡ Linux Tip: If you see can’t install torch on Linux, try:

  • Upgrading pip: pip install --upgrade pip

  • Installing wheel support: pip install wheel

  • Explicitly specifying Python version compatibility.



Step 3 – Install torchvision Module

Why do you need torchvision?

  • Access to datasets like CIFAR-10, ImageNet.

  • Pretrained models (ResNet, VGG, etc.).

  • Practical transforms for image augmentation.

Install it with:

bashpip install torchvision

⚠️ Ensure torchvision matches PyTorch version. Usually, pip handles this, but if you hit “incompatible version”how to verify PyTorch is installed properly errors, manually check PyTorch’s compatibility table.



Step 4 – Verify PyTorch is Installed Properly

Now let’s run a sanity check. Inside Python REPL:

pythonimport torch
import torchvision

print(torch.__version__) # Should print installed version
print(torchvision.__version__) # Confirm torchvision installed

x = torch.rand(2, 2)
print(x)

print(torch.cuda.is_available()) # True if CUDA GPU is ready



If you see a random 2×2 tensor and CUDA detection works, congrats—you’ve installed PyTorch successfully! 🎉



Troubleshooting Common Errors

Even seasoned ML engineers get tripped up here. Some fixes:

  • No matching distribution found → Ensure Python version is supported (PyTorch may not support Python 3.7 anymore).

  • pip version outdated → python -m pip install --upgrade pip

  • Permission errors (Linux) → Always install inside venv. Avoid sudo pip install.

  • torchvision mismatch → Pin compatible version, e.g.:bashpip install torchvision==0.19.1



Best Practices for Managing PyTorch Environments

  • Keep a requirements.txt per project:bashpip freeze > requirements.txt

  • Don’t upgrade PyTorch blindly; check release notes first.

  • For GPU devs: Always match PyTorch with your CUDA driver.

  • Use separate virtual environments for research experiments vs production deployments.



Leave a Reply