Setting up deep learning frameworks on your local machine represents the crucial first step in your machine learning journey. While cloud platforms offer convenience, local installations provide complete control, cost-free experimentation, and the ability to work offline with full access to your hardware. However, the installation process frequently becomes a frustrating maze of dependency conflicts, CUDA version mismatches, and cryptic error messages that discourage newcomers and waste time for experienced practitioners.
TensorFlow and PyTorch stand as the two dominant deep learning frameworks, each with distinct installation requirements and common pitfalls. TensorFlow, developed by Google, offers comprehensive production-ready tools and extensive deployment options. PyTorch, backed by Meta, provides intuitive Python-first design and dynamic computation graphs favored by researchers. Most practitioners eventually need both frameworks, making proper installation of each essential for a productive machine learning environment.
This guide walks through complete local installation of both frameworks across different operating systems, covering GPU acceleration setup, virtual environment management, common troubleshooting scenarios, and verification that your installation actually works. Whether you’re running Windows, macOS, or Linux, this comprehensive walkthrough ensures you’ll have functional TensorFlow and PyTorch installations ready for actual deep learning work.
Understanding What You’re Installing
Before running installation commands, understanding what these frameworks do and their system requirements helps prevent issues and choose appropriate configurations.
TensorFlow: Google’s Production-Ready Framework
TensorFlow provides a complete ecosystem for developing and deploying machine learning models at scale. The core library handles tensor operations and automatic differentiation, while complementary tools like TensorBoard for visualization, TensorFlow Serving for model deployment, and TensorFlow Lite for mobile/embedded devices create an integrated workflow.
Key components you’ll install:
- TensorFlow core library for building and training models
- Keras API deeply integrated as TensorFlow’s high-level interface
- CPU or GPU support depending on your configuration
- Various optimization libraries for performance
TensorFlow 2.x simplified installation significantly compared to 1.x versions, consolidating previously separate packages and providing better dependency management. Most users should install TensorFlow 2.15 or later for the best experience.
PyTorch: The Research-Friendly Framework
PyTorch emphasizes pythonic design and dynamic computation graphs that feel natural to Python developers. Its eager execution model means you can debug with standard Python tools, making development intuitive. Strong adoption in research communities means cutting-edge architectures often appear in PyTorch first.
Key components you’ll install:
- PyTorch core with tensor operations and autograd
- torchvision for computer vision utilities and datasets
- torchaudio for audio processing (optional)
- CUDA support if you have NVIDIA GPUs
PyTorch 2.0+ introduces compilation capabilities that dramatically improve performance while maintaining the ease of use that made PyTorch popular.
CPU vs GPU Considerations
Both frameworks support CPU-only and GPU-accelerated installations:
CPU-only installations work on any system, require no special hardware, but run 10-100x slower for deep learning workloads. CPU-only makes sense for:
- Learning fundamentals with small models
- Testing code before GPU training
- Deployment on CPU servers
- Systems without NVIDIA GPUs (AMD GPUs lack robust support)
GPU installations require NVIDIA GPUs and additional software (CUDA, cuDNN) but provide massive speedups. Essential for:
- Training real models on realistic datasets
- Computer vision and image generation
- Large language models and transformers
- Professional or research work
If you have an NVIDIA GPU (even an older GTX 1060), GPU installation is worth the extra setup complexity.
Pre-Installation Requirements
Proper preparation prevents most installation headaches. Complete these steps before installing frameworks.
Check Your Python Version
Both frameworks require Python 3.8 or later, with Python 3.10 or 3.11 recommended for best compatibility:
# Check Python version
python --version # or python3 --version on Linux/macOS
# If you need to install Python:
# Windows: Download from python.org
# macOS: brew install python@3.11
# Ubuntu/Debian: sudo apt install python3.11 python3.11-venv
Avoid Python 3.12 for now—newest Python releases sometimes have compatibility issues with deep learning packages for several months after release.
GPU Setup: CUDA and cuDNN
If you have an NVIDIA GPU and want GPU acceleration, install CUDA and cuDNN before installing frameworks:
For TensorFlow 2.15:
- CUDA 11.8
- cuDNN 8.9
For PyTorch 2.1+:
- CUDA 11.8 or 12.1
- cuDNN 8.x
Installing CUDA on Windows:
- Download CUDA Toolkit from developer.nvidia.com/cuda-downloads
- Run installer, choose Express Installation
- Restart system
- Verify:
nvcc --version
Installing CUDA on Ubuntu:
# Add NVIDIA package repository
wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-keyring_1.0-1_all.deb
sudo dpkg -i cuda-keyring_1.0-1_all.deb
sudo apt-get update
# Install CUDA 11.8
sudo apt-get install cuda-11-8
# Add to PATH (add to ~/.bashrc)
export PATH=/usr/local/cuda-11.8/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda-11.8/lib64:$LD_LIBRARY_PATH
Installing cuDNN:
- Create NVIDIA Developer account at developer.nvidia.com
- Download cuDNN for your CUDA version
- Extract and copy files to CUDA installation directory
- On Linux:
sudo cp cuda/include/cudnn*.h /usr/local/cuda/includeandsudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64
This process is tedious but only needs doing once. Package managers like conda can handle CUDA/cuDNN automatically, simplifying GPU setup.
Virtual Environment Setup
Always use virtual environments to isolate project dependencies and prevent conflicts:
Using venv (built into Python):
# Create virtual environment
python -m venv ml-env
# Activate (Windows)
ml-env\Scripts\activate
# Activate (Linux/macOS)
source ml-env/bin/activate
# Deactivate when done
deactivate
Using conda (recommended for deep learning):
# Create environment with Python 3.11
conda create -n ml-env python=3.11
# Activate
conda activate ml-env
# Deactivate
conda deactivate
Conda environments provide better GPU package management and often avoid CUDA/cuDNN installation complexity, making them preferred for beginners.
Installation Decision Tree
- You have Python experience and prefer pip
- Installing CPU-only versions
- You already have CUDA/cuDNN configured
- Working on lightweight projects
- You need GPU support and don’t have CUDA installed
- You’re new to deep learning setup
- You want simplified dependency management
- You need multiple Python versions for different projects
- You need reproducible environments for team collaboration
- Deploying to production servers
- Working with complex multi-framework setups
- You’re comfortable with containerization
Installing TensorFlow
With prerequisites complete, installing TensorFlow becomes straightforward. We’ll cover both pip and conda approaches.
TensorFlow Installation with pip
The pip method works well when you have CUDA/cuDNN already configured or only need CPU support:
# Activate your virtual environment first
source ml-env/bin/activate # or ml-env\Scripts\activate on Windows
# Install TensorFlow (automatically includes GPU support if CUDA detected)
pip install tensorflow==2.15.0
# Alternatively, explicitly specify CPU-only
pip install tensorflow-cpu==2.15.0
# Install additional tools
pip install tensorboard # Usually included but sometimes needs explicit install
pip install tensorflow-datasets # Pre-packaged datasets
TensorFlow 2.10+ automatically detects CUDA and enables GPU support if available. Earlier versions required separate tensorflow-gpu packages, but this is no longer necessary.
TensorFlow Installation with Conda
Conda simplifies GPU setup by bundling CUDA/cuDNN, avoiding manual installation:
# Create new environment with TensorFlow
conda create -n tf-env python=3.11
conda activate tf-env
# Install TensorFlow with GPU support (includes CUDA/cuDNN)
conda install -c conda-forge tensorflow-gpu
# Or CPU-only
conda install -c conda-forge tensorflow
# Install common complementary packages
conda install -c conda-forge tensorboard jupyter matplotlib pandas
The -c conda-forge flag uses the community-maintained conda-forge channel, which typically has more up-to-date packages than default Anaconda channels.
Verifying TensorFlow Installation
Always verify your installation actually works and detects GPUs if present:
import tensorflow as tf
# Check TensorFlow version
print(f"TensorFlow version: {tf.__version__}")
# Check GPU availability
print(f"GPU Available: {tf.config.list_physical_devices('GPU')}")
print(f"CUDA Version: {tf.sysconfig.get_build_info()['cuda_version']}")
# Quick computation test
tensor = tf.constant([[1, 2], [3, 4]])
result = tf.matmul(tensor, tensor)
print(f"\nTest computation:\n{result}")
# If GPU available, verify it's actually being used
if tf.config.list_physical_devices('GPU'):
with tf.device('/GPU:0'):
a = tf.constant([[1.0, 2.0], [3.0, 4.0]])
b = tf.constant([[5.0, 6.0], [7.0, 8.0]])
c = tf.matmul(a, b)
print(f"\nGPU computation successful:\n{c}")
If this runs without errors and shows your GPU, TensorFlow is properly installed and configured.
Installing PyTorch
PyTorch installation requires choosing between CUDA versions and includes additional libraries for specific domains.
PyTorch Installation with pip
PyTorch provides an excellent configuration selector at pytorch.org that generates exact pip commands for your system:
# Activate your virtual environment
source ml-env/bin/activate # or ml-env\Scripts\activate on Windows
# For GPU (CUDA 11.8) - most common for current systems
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
# For GPU (CUDA 12.1) - newer systems
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121
# For CPU only
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu
# For macOS (MPS acceleration for Apple Silicon)
pip install torch torchvision torchaudio
The --index-url flag tells pip to download PyTorch from the official PyTorch repository rather than PyPI, ensuring you get the correct CUDA version.
Important: The CUDA version in the pip command doesn’t need to exactly match your system CUDA. PyTorch bundles necessary CUDA libraries. However, your NVIDIA driver must support the CUDA version (driver 450+ for CUDA 11.x, driver 525+ for CUDA 12.x).
PyTorch Installation with Conda
Conda makes PyTorch installation even simpler:
# Create environment
conda create -n pytorch-env python=3.11
conda activate pytorch-env
# Install PyTorch with GPU support (CUDA 11.8)
conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia
# Or CUDA 12.1
conda install pytorch torchvision torchaudio pytorch-cuda=12.1 -c pytorch -c nvidia
# Or CPU only
conda install pytorch torchvision torchaudio cpuonly -c pytorch
# Install common packages
conda install jupyter matplotlib pandas scikit-learn
Verifying PyTorch Installation
Test that PyTorch works correctly and detects your GPU:
import torch
# Check PyTorch version and CUDA availability
print(f"PyTorch version: {torch.__version__}")
print(f"CUDA available: {torch.cuda.is_available()}")
if torch.cuda.is_available():
print(f"CUDA version: {torch.version.cuda}")
print(f"GPU Device: {torch.cuda.get_device_name(0)}")
print(f"Number of GPUs: {torch.cuda.device_count()}")
# Test GPU computation
device = torch.device('cuda')
x = torch.randn(1000, 1000, device=device)
y = torch.randn(1000, 1000, device=device)
z = torch.matmul(x, y)
print(f"\nGPU computation successful. Result shape: {z.shape}")
else:
print("Running on CPU")
# Test basic operations
tensor = torch.tensor([[1, 2], [3, 4]])
print(f"\nTest tensor:\n{tensor}")
print(f"Sum: {tensor.sum()}")
If you see your GPU listed and computations complete without errors, PyTorch is correctly installed.
Installing Both Frameworks Together
Many projects require both TensorFlow and PyTorch. You can install them in the same environment, though separate environments prevent potential conflicts.
Shared Environment Approach
Installing both in one environment simplifies switching between frameworks:
# Create environment
conda create -n dl-env python=3.11
conda activate dl-env
# Install PyTorch first (handles CUDA dependencies)
conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia
# Install TensorFlow
pip install tensorflow==2.15.0
# Install common packages
conda install jupyter matplotlib pandas scikit-learn numpy
# Additional useful packages
pip install transformers datasets accelerate # Hugging Face libraries
pip install opencv-python pillow # Image processing
pip install tensorboard # Works with both frameworks
This approach works well but occasionally leads to dependency conflicts, particularly with package versions that both frameworks depend on (numpy, protobuf, etc.). If you encounter conflicts, separate environments provide cleaner isolation.
Separate Environment Approach
Creating distinct environments prevents conflicts:
# TensorFlow environment
conda create -n tf-env python=3.11
conda activate tf-env
conda install tensorflow-gpu -c conda-forge
conda install jupyter matplotlib pandas
# PyTorch environment
conda create -n pytorch-env python=3.11
conda activate pytorch-env
conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia
conda install jupyter matplotlib pandas
# Switch between environments as needed
conda activate tf-env # For TensorFlow projects
conda activate pytorch-env # For PyTorch projects
The slight inconvenience of switching environments outweighs debugging obscure dependency issues.
Common Installation Issues & Solutions
Testing Your Complete Setup
With both frameworks installed, run comprehensive tests to ensure everything works correctly for actual machine learning work.
Complete Verification Script
This script tests TensorFlow, PyTorch, GPU availability, and basic operations:
import sys
print(f"Python version: {sys.version}\n")
# Test TensorFlow
try:
import tensorflow as tf
print("=" * 50)
print("TENSORFLOW STATUS")
print("=" * 50)
print(f"Version: {tf.__version__}")
gpus = tf.config.list_physical_devices('GPU')
print(f"GPUs available: {len(gpus)}")
for gpu in gpus:
print(f" - {gpu}")
# Quick computation
with tf.device('/GPU:0' if gpus else '/CPU:0'):
a = tf.random.normal([1000, 1000])
b = tf.random.normal([1000, 1000])
c = tf.matmul(a, b)
print("TensorFlow computation: ✓ Success\n")
except ImportError as e:
print(f"TensorFlow not installed: {e}\n")
# Test PyTorch
try:
import torch
print("=" * 50)
print("PYTORCH STATUS")
print("=" * 50)
print(f"Version: {torch.__version__}")
print(f"CUDA available: {torch.cuda.is_available()}")
if torch.cuda.is_available():
print(f"CUDA version: {torch.version.cuda}")
print(f"GPUs: {torch.cuda.device_count()}")
for i in range(torch.cuda.device_count()):
print(f" - {torch.cuda.get_device_name(i)}")
# GPU computation
device = torch.device('cuda')
x = torch.randn(1000, 1000, device=device)
y = torch.randn(1000, 1000, device=device)
z = torch.matmul(x, y)
print("PyTorch GPU computation: ✓ Success")
else:
# CPU computation
x = torch.randn(1000, 1000)
y = torch.randn(1000, 1000)
z = torch.matmul(x, y)
print("PyTorch CPU computation: ✓ Success")
print("\n" + "=" * 50)
print("ALL SYSTEMS OPERATIONAL")
print("=" * 50)
except ImportError as e:
print(f"PyTorch not installed: {e}")
Save this as test_setup.py and run it whenever you modify your installation to quickly verify everything still works.
Performance Benchmarking
Compare CPU vs GPU performance to ensure GPU acceleration is actually working:
import time
import tensorflow as tf
import torch
def benchmark_tensorflow():
"""Benchmark TensorFlow computation speed"""
# CPU test
with tf.device('/CPU:0'):
start = time.time()
a = tf.random.normal([5000, 5000])
b = tf.random.normal([5000, 5000])
c = tf.matmul(a, b)
cpu_time = time.time() - start
# GPU test (if available)
if tf.config.list_physical_devices('GPU'):
with tf.device('/GPU:0'):
start = time.time()
a = tf.random.normal([5000, 5000])
b = tf.random.normal([5000, 5000])
c = tf.matmul(a, b)
gpu_time = time.time() - start
print(f"TensorFlow CPU: {cpu_time:.4f}s")
print(f"TensorFlow GPU: {gpu_time:.4f}s")
print(f"Speedup: {cpu_time/gpu_time:.2f}x")
else:
print(f"TensorFlow CPU only: {cpu_time:.4f}s")
def benchmark_pytorch():
"""Benchmark PyTorch computation speed"""
# CPU test
start = time.time()
a = torch.randn(5000, 5000)
b = torch.randn(5000, 5000)
c = torch.matmul(a, b)
cpu_time = time.time() - start
# GPU test (if available)
if torch.cuda.is_available():
device = torch.device('cuda')
start = time.time()
a = torch.randn(5000, 5000, device=device)
b = torch.randn(5000, 5000, device=device)
c = torch.matmul(a, b)
torch.cuda.synchronize() # Wait for GPU to finish
gpu_time = time.time() - start
print(f"\nPyTorch CPU: {cpu_time:.4f}s")
print(f"PyTorch GPU: {gpu_time:.4f}s")
print(f"Speedup: {cpu_time/gpu_time:.2f}x")
else:
print(f"\nPyTorch CPU only: {cpu_time:.4f}s")
benchmark_tensorflow()
benchmark_pytorch()
Expect 10-50x speedups on GPU depending on your hardware. If GPU times are similar to CPU, something is wrong with your GPU installation.
Maintaining Your Installation
Deep learning packages update frequently. Maintaining your installation ensures access to latest features and bug fixes.
Updating Packages
Regular updates keep your environment current:
# Update with pip
pip install --upgrade tensorflow torch torchvision torchaudio
# Update with conda
conda update tensorflow pytorch torchvision torchaudio
# Update all packages in environment
conda update --all
Be cautious with major version updates (e.g., TensorFlow 2.x to 3.x when released) as they may break existing code. Test in separate environments first.
Managing Multiple Environments
As projects accumulate, managing environments becomes important:
# List all conda environments
conda env list
# Remove unused environment
conda env remove -n old-env
# Export environment for reproduction
conda env export > environment.yml
# Create environment from file
conda env create -f environment.yml
# Clone existing environment
conda create --name new-env --clone existing-env
Troubleshooting Installation Problems
When issues arise, systematic debugging helps:
1. Verify Python version: Both frameworks require Python 3.8+ 2. Check virtual environment: Ensure you activated the correct environment 3. Review error messages: They usually point to the problem (missing CUDA, wrong versions, etc.) 4. Check CUDA compatibility: Ensure your NVIDIA driver supports the CUDA version 5. Try clean reinstall: Remove and reinstall packages: pip uninstall tensorflow torch then reinstall 6. Check disk space: Insufficient space causes cryptic installation failures 7. Search GitHub issues: Someone likely encountered your exact problem before
Conclusion
Successfully installing TensorFlow and PyTorch locally unlocks the full potential of modern deep learning frameworks without ongoing cloud costs or privacy concerns. While the initial setup requires navigating CUDA versions, virtual environments, and framework-specific installation procedures, following systematic approaches using either pip with venv or conda environments ensures reliable installations. The key is choosing the appropriate method for your system configuration, properly verifying GPU detection when applicable, and maintaining environments through regular updates and testing.
Once properly configured, your local setup provides unlimited experimentation, complete control over your environment, and the foundation for serious deep learning work. Whether you’re training computer vision models with TensorFlow, exploring natural language processing with PyTorch, or switching between frameworks as projects demand, a properly installed local environment becomes your most valuable tool. Invest the time upfront to do installations correctly, use the verification scripts provided to confirm everything works, and maintain your environments through updates—the productivity gains and learning opportunities far outweigh the initial configuration effort.