Appendix A: Installation and Environment Setup for PyTorch

Below is the complete Appendix A: Installation and Environment Setup for PyTorch, written in a clean, structured, and comprehensive format suitable for inclusion in the book.


**Appendix A

Installation and Environment Setup for PyTorch**


A.1 Introduction

Before beginning any project in deep learning or artificial intelligence with PyTorch, it is essential to set up a stable programming environment. This appendix provides step-by-step instructions for installing PyTorch on various operating systems, configuring development tools, and verifying successful installation. The instructions cover CPU-only and GPU-enabled (CUDA) installations.


A.2 System Requirements

Operating System

  • Windows 10/11 (64-bit)

  • Linux (Ubuntu recommended)

  • macOS (Apple Silicon or Intel)

Hardware

  • Minimum: Dual-core CPU, 4 GB RAM

  • Recommended:

    • 8+ GB RAM

    • NVIDIA GPU with CUDA support (Compute Capability ≥ 3.5)

Software

  • Python 3.8 to 3.12

  • pip or conda package manager

  • Git (recommended)

  • VS Code / PyCharm (optional but recommended)


A.3 Installing Python

Windows

  1. Visit the official Python website.

  2. Download the latest stable version (≥3.8).

  3. Check "Add Python to PATH".

  4. Complete installation.

Linux (Ubuntu)

sudo apt update
sudo apt install python3 python3-pip

macOS

Python often comes pre-installed.
To install a newer version using Homebrew:

brew install python

A.4 Creating a Virtual Environment (Recommended)

Using venv

python -m venv pytorch_env

Activate:

  • Windows

    pytorch_env\Scripts\activate
    
  • Linux/macOS

    source pytorch_env/bin/activate
    

Using Conda (Optional)

conda create -n pytorch_env python=3.10
conda activate pytorch_env

A.5 Installing PyTorch

PyTorch offers CPU and GPU (CUDA) versions. Use the official selector at pytorch.org, but the common installation commands are listed below.


A.5.1 Install PyTorch (CPU Version)

pip

pip install torch torchvision torchaudio

Conda

conda install pytorch torchvision torchaudio cpuonly -c pytorch

A.5.2 Install PyTorch with CUDA Support

Note: Your system must have a CUDA-compatible NVIDIA GPU and the correct driver.


Check GPU Compatibility

nvidia-smi

CUDA Version Installation

For CUDA 12.1 (Most Common – Recommended)

pip

pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121

Conda

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

For CUDA 11.8

pip

pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118

If unsure, install the CPU version first. It works for all systems and requires no GPU or drivers.


A.6 Verifying Installation

Run the following in Python:

import torch

print("PyTorch version:", torch.__version__)
print("CUDA available:", torch.cuda.is_available())

if torch.cuda.is_available():
    print("GPU name:", torch.cuda.get_device_name(0))

Expected outcomes:

  • PyTorch version displays correctly.

  • If using CUDA, GPU is detected successfully.


A.7 Installing Development Tools

Code Editor

  • Visual Studio Code (recommended)

  • PyCharm (Community or Professional)

  • Jupyter Notebook / JupyterLab

VS Code Extensions

  • Python extension

  • Jupyter extension

  • Pylance for IntelliSense


Installing Jupyter Notebook

pip install notebook

Launch:

jupyter notebook

A.8 Installing Additional Useful Libraries

Common ML/DL Packages

pip install numpy pandas matplotlib seaborn scikit-learn pillow tqdm

Visualization Tools

pip install tensorboard

A.9 Troubleshooting Common Installation Issues

1. “torch not found”

Reinstall PyTorch:

pip install torch --upgrade

2. CUDA not detected

  • Ensure nvidia-smi works.

  • Reinstall the correct CUDA-supported PyTorch version.

  • Update NVIDIA drivers.

3. pip installation is slow

Use --trusted-host or install via conda for faster downloads.

4. Permission errors (Linux/Mac)

Use:

pip install --user torch

A.10 Updating PyTorch

To upgrade:

pip install --upgrade torch torchvision torchaudio

or using conda:

conda update pytorch torchvision torchaudio

A.11 Uninstalling PyTorch

pip uninstall torch torchvision torchaudio

or

conda remove pytorch torchvision torchaudio

A.12 Summary

This appendix covered:

  • Installing Python and preparing a virtual environment

  • Installing PyTorch with CPU or GPU support

  • Setting up IDEs and productivity tools

  • Verifying installation

  • Handling common issues

With PyTorch properly installed, you are ready to begin building deep learning, computer vision, NLP, reinforcement learning, and deployment projects using the chapters of this book.

Comments