Installation

To get the most out of this book, you should run the code, modify it, and experiment with it first-hand. Every chapter is available as an executable Jupyter notebook. To make local setup as frictionless as possible, we distribute a self-contained .zip bundle for each supported framework (available from the Notebooks menu at the top of the page). Each bundle includes the notebooks, already populated with their saved outputs and figures so you can browse them immediately, along with the matching d2l library source code and pinned uv lock files for both CPU and GPU environments.

While you can read the pre-computed outputs in the notebooks without installing any deep learning libraries, executing and modifying the code requires Python, JupyterLab, and your chosen framework. The included uv lock files install a complete, tested environment in a couple of commands.

Choosing a Framework#

If you do not already have a strong preference, we recommend starting with PyTorch, which is the one framework that covers every chapter of the book. The Basics (Chapter 1 through Chapter 8), Image Models (Chapter 20), Mathematics for Deep Learning (Chapter 21 through Chapter 26), and Tools for Deep Learning (Chapter 27) parts are implemented in all four frameworks (PyTorch, JAX, TensorFlow, and MXNet). The Advanced part (Chapter 9 through Chapter 17) is implemented in PyTorch and JAX.

You do not need specialized hardware to begin: the first several chapters run comfortably on any modern laptop CPU. A GPU becomes valuable later on when we train larger convolutional networks, transformers, and generative models.

The downloadable notebook bundles match the published edition of the book on this website. If you want to explore the bleeding-edge source files, build scripts, or development environments, you can also clone the repository directly from GitHub:

git clone https://github.com/smolix/d2l-neu.git
cd d2l-neu

For simply reading and running the notebooks, however, the framework-specific .zip bundle is much smaller and requires far fewer build dependencies.

Installing uv#

We use uv to manage both the Python interpreter and the package dependencies for the notebooks, so you do not need Conda or a pre-existing Python installation. On macOS or Linux, install uv using its standalone installer (or your system package manager):

curl -LsSf https://astral.sh/uv/install.sh | sh

On Windows PowerShell, run:

powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"

If the installer updates your PATH, open a fresh terminal window and verify that uv is available:

uv --version

Our notebook environments target Python 3.12; if Python 3.12 is not already present on your machine, uv will download and manage an isolated interpreter automatically.

Downloading and Unpacking the Notebooks#

Download the archive for your chosen framework from the Notebooks menu at the top of the page and extract it. For example, to unpack the PyTorch bundle on macOS or Linux, run the following commands in your download directory:

unzip d2l-pytorch.zip
cd d2l-pytorch

On Windows, you can extract the .zip archive using File Explorer or PowerShell. Run all subsequent commands from inside the extracted folder, which contains pyproject.toml, the d2l/ package source, and the uv lock files.

Setting Up a CPU Environment#

We recommend starting with the CPU environment to verify your setup quickly. Inside the extracted notebook directory, create an isolated Python 3.12 virtual environment and synchronize it with the pinned CPU lock file:

uv venv --python 3.12
uv pip sync pylock.cpu.toml

If you are running MXNet on an Apple Silicon Mac, synchronize against its dedicated macOS lock file instead:

uv pip sync pylock.cpu-macos.toml

For MXNet on Linux, pylock.cpu.toml uses the Linux x86-64 wheel distributed with our maintained MXNet 2.0 release. Note that MXNet builds are available only for Linux (x86-64) and macOS (Apple Silicon); on Windows, please use PyTorch, JAX, or TensorFlow, or run the notebooks in a hosted cloud environment. For PyTorch, JAX, and TensorFlow, pylock.cpu.toml automatically selects the appropriate packages for your operating system and architecture.

Once the environment is synchronized, launch JupyterLab:

uv run --no-sync jupyter lab

JupyterLab will normally open a tab in your default web browser automatically; if it does not, copy and paste the local http://localhost:... URL printed in your terminal. Passing --no-sync tells uv to run directly inside the exact CPU or GPU environment you just synchronized with uv pip sync.

Setting Up a GPU Environment#

The included GPU lock files reproduce the hardware-accelerated environments used to execute this book. They target Linux (x86-64) machines equipped with an NVIDIA GPU. Before installing the GPU environment, confirm that your NVIDIA driver is active and visible:

nvidia-smi

From the extracted notebook directory, create the virtual environment (if you have not already) and synchronize it with the GPU lock file:

uv venv --python 3.12
uv pip sync pylock.gpu.toml
uv run --no-sync jupyter lab

You do not need a separate virtual environment to switch from CPU to GPU: running uv pip sync pylock.gpu.toml in an existing environment cleanly replaces the CPU packages with their GPU counterparts (and similarly, VIRTUAL_ENV=.venv-lm uv pip sync pylock.lm-gpu.toml upgrades the Language Models environment in the PyTorch bundle). The GPU lock files pin the following accelerator stacks:

  • PyTorch: CUDA 12.8 wheels.
  • JAX: the CUDA 12 pip distribution.
  • TensorFlow: the and-cuda distribution with its bundled CUDA libraries.
  • MXNet: the maintained CUDA 13.3 build from smolix/mxnet.

Because GPU software stacks depend on your operating system, kernel driver, and GPU architecture, the pinned lock file may occasionally differ from what your specific machine requires. If pylock.gpu.toml does not match your hardware or OS, consult the official installation guide for PyTorch, JAX, or TensorFlow. You can always use the CPU lock file for the early chapters and switch to a hosted notebook (Section 27.2) or a cloud GPU instance (Section 27.3) when you reach the more compute-intensive chapters later in the book.

Verifying the Installation#

You can verify that your framework is installed properly and check whether it detects a GPU accelerator with a one-line command:

uv run --no-sync python -c "import torch; print(torch.__version__); print(torch.cuda.is_available())"

On a CPU-only installation, the second line prints False; when an NVIDIA GPU is available, it prints True.

The Standard Imports#

Every notebook in this book begins with a standard preamble cell that imports Python's built-in math module for scalar functions and constants, NumPy under its standard alias np for CPU arrays, the deep learning framework itself, and the matching backend of our companion library as d2l:

import math
import numpy as np
import torch
from torch import nn
from torch.nn import functional as F
from d2l import torch as d2l

Here torch is the core PyTorch package. Its nn module provides neural network layers and loss functions as stateful classes, while F (torch.nn.functional) exposes the corresponding operations as stateless functions such as F.relu and F.cross_entropy.

The final import binds the matching backend of the book's companion package to the uniform name d2l. Whenever a section defines and explains a reusable helper function or class marked with #@save, it is exported into d2l so later chapters can call it cleanly without repeating the definition.

To keep the book uncluttered, we print this standard import cell only once, right here. The downloadable and hosted Jupyter notebooks include it at the top of every file so that each notebook runs standalone, whereas the web and PDF editions begin each section directly with the code specific to that topic. Whenever a notebook requires an additional library (such as Gymnasium in the reinforcement learning chapters) or needs to configure compiler flags before initializing the framework, we display that extra setup cell explicitly. Plotting requires no special boilerplate: Jupyter renders figures inline by default.

Data, Models, and Disk Space#

While the notebook .zip archive includes all of the notebooks, pre-computed cell outputs, and figures, it does not bundle large training datasets or pretrained model weights. Instead, notebooks download those assets automatically on first use and store them in a local cache directory. As a result, you can read the notebooks and inspect their saved outputs completely offline, but executing data-dependent notebooks requires an active internet connection and additional disk space.

Finally, keep in mind that some advanced chapters later in the book require more memory or compute than a typical laptop provides. Start with the CPU environment for the foundational chapters, and transition to a local GPU or a cloud machine once the workloads demand it. If you are considering purchasing dedicated hardware for deep learning, Section 27.4 offers practical guidance on what to look for.