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 1 onward — Python and a virtual environment right away, PyTorch a bit later. Every chapter has a browser half (JavaScript, nothing to install) and a local half that writes real files into your own my-llm/ project. The local half starts immediately: chapter 1 already has you running python -m scripts.train_bigram.
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.
In a hurry? Steps 1 to 3 (Python + venv, ~5 minutes) are all you need for chapters 1 to 10 — they run on the standard library alone. Step 4 installs PyTorch, which only becomes necessary at chapter 12. Doing it now costs you one coffee and saves you an interruption later.
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/. Chapter 1 repeats these exact commands, so creating it now or there makes no difference — just don't do it twice.
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 12–21 need PyTorch; chapters 1–10 do not (they use the standard library only). Install the CPU build now anyway — it is around 200 MB, and you will not want to wait for it mid-chapter:
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 cells plus plain-Python files in
my-llm/— no PyTorch yet. - Part II — Make it learn (5–7): ~1.5 hours. Same mix.
- Part III — Build the transformer (8–10): ~1.5 hours. Same mix.
- Part IV — Train and use the LLM (11–16): ~4 hours. PyTorch kicks in, and the centre of gravity shifts to your machine. 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.
Frequently asked questions
Do I need a GPU to follow this course?
No. Every chapter runs on a normal laptop CPU. The training run in chapter 13 takes about 10 minutes on CPU versus 2 on Apple Silicon MPS or a CUDA GPU, and the largest download is GPT-2's weights at roughly 500 MB. A GPU makes things faster; nothing here requires one.
Which Python version does the course need?
Python 3.11 or newer — the code uses type-hint syntax introduced in 3.11. Python 3.12 and 3.13 both work. Chapter 0 covers installation on macOS, Windows and Linux, including the PowerShell execution-policy fix Windows needs before a virtual environment will activate.
When do I actually need PyTorch?
From chapter 12 onward. Chapters 1 to 10 build everything with the Python standard library, so you can start immediately with just Python and a virtual environment. Installing PyTorch early only saves you an interruption later.
How much disk space does the whole course need?
About 2 GB. PyTorch's CPU build is roughly 200 MB, the GPT-2 weights around 500 MB, and training checkpoints account for most of the rest.