How to Install PyTorch with Conda: Step-by-Step Guide for Python 3.11 & Machine Learning

Introduction

This updated guide shows how to install PyTorch using conda with Python 3.11, the latest stable Python version supported by PyTorch.

Prerequisites

  • conda (Anaconda/Miniconda) installed
  • Python 3.11 compatibility check completed for your project
  • NVIDIA GPU drivers updated (if using CUDA)

Installation Steps

Step 1: Create a Python 3.11 Environment

conda create --name pytorch_env python=3.11

This creates an environment with Python 3.11. Verify Python version after activation with:

python --version

Step 2: Activate the Environment

For modern conda installations (v4.10+):

conda activate pytorch_env

If using older versions:

source activate pytorch_env  # Linux/macOS
activate pytorch_env         # Windows

Step 3: Install PyTorch 2.3+

Current recommended commands (check pytorch.org for latest):

CUDA 12.1 (NVIDIA GPUs with compute capability 3.5-9.0)

conda install pytorch torchvision torchaudio pytorch-cuda=12.1 -c pytorch -c nvidia

CPU-Only Installation

conda install pytorch torchvision torchaudio cpuonly -c pytorch

ROCm 5.6 (AMD GPUs)

conda install pytorch torchvision torchaudio pytorch-rocm=5.6 -c pytorch -c rocm

Enhanced Verification

Test both Python version and PyTorch functionality:

import torch, sys

print(f"Python version: {sys.version}")
print(f"PyTorch version: {torch.__version__}")
print(f"CUDA/cuDNN version: {torch.version.cuda}")
print(f"CUDA available: {torch.cuda.is_available()}")
print(f"Device count: {torch.cuda.device_count()}")

# Simple tensor operation test
x = torch.randn(3, 3)
print(f"\nTensor operation test:\n{x @ x.t()}")

Python 3.11 Specific Notes

  • Ensure all dependencies are compatible with Python 3.11
  • If using legacy packages, consider Python 3.10 instead
  • For Apple Silicon (M1/M2), use conda install pytorch::pytorch torchvision torchaudio -c pytorch

Additional Resources

  • PyTorch Python 3.11 release notes
  • conda environment management guide
  • Python 3.11 migration documentation

Category: PyTorch

Trending
Latest Articles