Chapter 0 · 12 min
Before you start
Set up Python 3.11+, a virtual environment, and PyTorch in 10 minutes. Mac, Windows, Linux. The toolchain for the rest of the course.
This chapter prepares the local environment you will use from chapter 11 onward — Python, a virtual environment, PyTorch. Chapters 1–10 only run small JavaScript snippets in the browser, so you can technically skip this page until you reach the Python parts. But if you have never set up a Python project before, doing it now is calmer than doing it under pressure later.
Skip ahead if python3 -m venv .venv && source .venv/bin/activate && pip install torch is muscle memory. You will not learn anything new on this page.
The whole setup takes 10–20 minutes, mostly waiting for the PyTorch download.
1. Install Python 3.11 or newer
The book uses type-hint features that need Python 3.11+. 3.12 or 3.13 is recommended.
brew install python@3.13# Install from https://www.python.org/downloads/ and check 'Add Python to PATH' during install.sudo apt update && sudo apt install python3.13 python3.13-venv python3-pipCheck that the install worked:
python3 --versionpy --versionpython3 --versionYou should see Python 3.13.x (or 3.11.x / 3.12.x — anything from 3.11 up is fine).
2. Create the project folder
The local artifact you build across the book lives in my-llm/. You can create it now or wait for chapter 1 — the commands are identical. Either way:
mkdir -p my-llm/llm my-llm/scripts my-llm/data && cd my-llmmkdir my-llm; mkdir my-llm\\llm,my-llm\\scripts,my-llm\\data; cd my-llmmkdir -p my-llm/llm my-llm/scripts my-llm/data && cd my-llm3. Create and activate the virtual environment
A venv isolates this project's package versions from your system Python. A clean rm -rf .venv (or Remove-Item .venv) is enough to undo every install.
python3 -m venv .venv && source .venv/bin/activatepy -m venv .venv; .\\.venv\\Scripts\\Activate.ps1python3 -m venv .venv && source .venv/bin/activateAfter activation your shell prompt should show (.venv) somewhere. From now on, every python and pip command in this book assumes the venv is active. If you open a new terminal, you have to activate again.
4. Install PyTorch
Chapters 11–20 need PyTorch. Install the CPU build now — it is around 200 MB:
pip install torchpip install torchpip install torchThis is the longest single download in the book. Have a coffee.
If you have a recent NVIDIA GPU and want CUDA, see the PyTorch install selector. The CPU build is enough for every chapter; CUDA / MPS just make faster.
5. Verify hardware acceleration
Run a quick check from the activated venv:
python -c "import torch; print(torch.__version__); print(f\"mps={torch.backends.mps.is_available()} cuda={torch.cuda.is_available()}\")"python -c "import torch; print(torch.__version__); print(f\"mps={torch.backends.mps.is_available()} cuda={torch.cuda.is_available()}\")"python -c "import torch; print(torch.__version__); print(f\"mps={torch.backends.mps.is_available()} cuda={torch.cuda.is_available()}\")"Expected result depends on your machine:
- Apple Silicon Mac (M1 through M5):
mps=True cuda=False. The book uses MPS — 5-20× faster than CPU on matrix multiplies. - NVIDIA GPU on Linux/Windows with the CUDA PyTorch build:
mps=False cuda=True. Fastest option. - Anything else:
mps=False cuda=False. CPU. Every chapter still works; in chapter 13 takes ~10 minutes instead of ~2.
6. Common errors and what to do
| Symptom | Fix |
|---|---|
python: command not found | Use python3 (mac/linux) or py (Windows). Some installers register only one. |
ModuleNotFoundError: No module named 'torch' right after install | The venv is not active. Look for (.venv) in your prompt; activate again if missing. |
PowerShell: Activate.ps1 cannot be loaded | Run Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned once, then retry. |
SSL: CERTIFICATE_VERIFY_FAILED during pip install | On macOS, run Install Certificates.command from /Applications/Python 3.13/. On corporate networks, configure pip to use your proxy. |
pip install torch hangs for ages | This is normal. ~200 MB. If it errors out, retry with pip install --index-url https://download.pytorch.org/whl/cpu torch. |
| Wrong Python version detected | Make sure the venv was created with the new Python: python3.13 -m venv .venv, not python3 -m venv .venv if your default python3 points to an older version. |
7. Time budget for the rest of the book
Rough wall-clock estimate per part, assuming you also run and inspect the code:
- Part I — Start the project (chapters 1–4): ~1.5 hours. Browser only, no Python.
- Part II — Make it learn (5–7): ~1.5 hours. Browser only.
- Part III — Build the transformer (8–10): ~1.5 hours. Browser only.
- Part IV — Train and use the LLM (11–16): ~4 hours. Python kicks in. The training run in chapter 13 is ~10 minutes on CPU, ~2 on MPS/CUDA. Chapter 15 downloads ~500 MB of GPT-2 weights.
- Part V — Make it useful, cheaper, and usable (17–21): ~4 hours. The SFT run in chapter 17 is ~2 minutes; the chat REPL in chapter 20 is interactive; the chapter 21 capstone fine-tune is ~20 minutes on CPU.
- Part VI — Appendices (optional): ~30 minutes if you read the backprop derivation and the RLHF/DPO conceptual walk-through. Skippable.
Total: ~13 hours including waits. You can do it in one weekend, or one chapter per evening for two weeks.
Recap
- Python 3.11+ with a working
pythonorpython3command. my-llm/folder created withllm/,scripts/,data/subfolders..venvactivated, prompt shows(.venv).pip install torchran cleanly,import torchworks.- You know whether you have MPS, CUDA, or just CPU.
- PowerShell execution policy set if you are on Windows.
Going further
- Python's official venv tutorial — the canonical reference if you want to understand virtual environments more deeply.
- PyTorch install selector — pick CPU / MPS / CUDA / specific Python version and get the right
pipcommand.
Next up: chapter 1 — the dumbest model that exists. Start building.