Skip to content
Module 1 · Foundations
Chapter 1 of 17
Run it in your browser

Chapter 1: Environment Setup

The map with Text highlighted Figure 1.1: Before you build, you set up your tools.

Your environment is the tools used to run the code. Installing your environment gives you everything you need to build and execute the model (Figure 1.1). After installation, the code functions in the same manner for you as it does here.

In this chapter you will:

  • Install Python and open a terminal to run commands.
  • Download the project code using Git.
  • Set up an isolated workspace for your packages.
  • Download the training dataset.

Words to Know

  • Terminal: a text-based window where you type commands.
  • Repository: a folder of code stored online.
  • Virtual Environment: a separate toolbox for one project, so what you install for it stays out of your other projects.
  • Package Manager: a tool that downloads and installs packages, the ready-made pieces of code a project uses.

In Business

Engineering teams agree on common standards for dev environments so they never have to deal with the "but it works on my machine" issue. If a team was building a house-style AI assistant to write emails, they would do the same. Thanks to virtual environments and locked-down package versions, new employees can begin contributing to the assistant right away, and code runs the same on laptops as it does on production servers.

Two Ways to Follow Along

This will work in your browser or on your local machine. Pick one below and you can always change later.

In your browser, no installation required. Each chapter page will have a "Run it in your browser" link at the top. This will launch the chapter as a Google Colab notebook. Colab allows you to run Python code using Google's computers for free. All you need is a Google account. Run the first cell to download the code for this project and the Shakespeare text. Then you can run each cell below it in order. If you use this option, skip Steps 1 to 6 and look over Steps 7 and 8 to see what your output should look like. Then continue on to Chapter 2.

On your local machine. Complete Steps 1 to 8 below. Once installed you will be able to run all the scripts in this book with one command whether you are connected to the internet or not.

Step 1: Install Python

Python is the programming language this entire tutorial uses, and we need version 3.10 or newer. You can download it from the official website (Figure 1.2).

A generic browser window showing the python.org downloads page with a download button Figure 1.2: Download the Python 3.13 installer.

Windows:

  1. Open your web browser and go to python.org/downloads
  2. Click the button to download Python 3.13.x.
  3. Run the downloaded .exe installer.
  4. Critical: On the first screen of the installer, tick the checkbox labeled "Add Python to PATH" at the bottom. PATH is a list of directories for the computer to search when running commands. If you leave the box unticked, Python will not be recognized when you type commands.
  5. Click "Install Now".

macOS:

  1. Open your web browser and go to python.org/downloads
  2. Click the button to download Python 3.13.x.
  3. Run the downloaded .pkg installer and follow the prompts.

Step 2: Open a Terminal

The terminal is where you will run your code.

Windows: Press Win + R, type cmd, and press Enter. A black window appears with a blinking cursor.

macOS: Press Cmd + Space to open Spotlight, type Terminal, and press Enter.

Verify your Python installation by running this command:

Terminal
$ python --version

You should see Python 3.10 or newer printed to the screen. (On macOS, if python is not found, type python3 --version instead.)

Step 3: Install Git

Git downloads the project code from the internet. Figure 1.3 shows the download page.

A generic browser window showing the Git download page Figure 1.3: Download Git to get the project files.

Windows:

  1. Go to git-scm.com/download/win
  2. Download the "64-bit Git for Windows Setup".
  3. Run the installer and click "Next" through every screen.

macOS: Open your terminal and type git --version. If it is not installed, macOS will ask if you want to install the "Command Line Developer Tools". Click Install.

Verify Git is installed (open a new terminal window on Windows):

Terminal
$ git --version

You should see a message confirming your Git version.

Step 4: Download the Project

Now download the project code with a single command.

Terminal
$ git clone https://github.com/jackluucoding/build-llm-from-zero.git

Then move into the project folder:

Terminal
$ cd build-llm-from-zero

Step 5: Create a Virtual Environment

Different projects need different versions of the same library, so a virtual environment gives this project its own isolated box of packages (Figure 1.4).

A dashed box representing a virtual environment containing Python and PyTorch Figure 1.4: A virtual environment isolates your project's libraries.

Make sure your terminal is inside the build-llm-from-zero folder, then run:

Windows:

Terminal
$ python -m venv venv
$ venv\Scripts\activate

macOS:

Terminal
$ python3 -m venv venv
$ source venv/bin/activate

You will see (venv) appear at the start of your terminal prompt. You must run the activate command every time you open a new terminal window.

Step 6: Install PyTorch and Dependencies

With the virtual environment active, install the required libraries using pip, Python's package manager.

First, install PyTorch (we use a CPU-only version):

Terminal
$ pip install torch==2.6.0 --index-url https://download.pytorch.org/whl/cpu

Then install the other libraries:

Terminal
$ pip install numpy requests matplotlib

Step 7: Download the Shakespeare Dataset

We will train our model to write in a specific style using a selection of Shakespeare's plays. (Dataset credit: the Tiny Shakespeare text comes from Andrej Karpathy's char-rnn project, https://github.com/karpathy/char-rnn)

Terminal
$ python src/utils/download_data.py
Dataset ready: src/data/shakespeare.txt (1089 KB)

Dataset statistics:
  Total characters : 1,115,394
  Unique characters: 65

First 200 characters:
----------------------------------------
First Citizen:
Before we proceed any further, hear me speak.

All:
Speak, speak.
...

The text ships with the project, so this step checks it rather than downloading it. If the file were missing, the script would download it and check that it is exactly the same text the book was built from.

Step 8: Verify Everything Works

Run the setup verification script to confirm all pieces are ready:

Terminal
$ python src/ch00_setup_check.py
Chapter 1: Checking your environment...

[OK] Python: 3.13.13
[OK] PyTorch: 2.6.0+cpu
[OK] NumPy: 2.5.3
[OK] Requests: 2.34.2
[OK] Shakespeare dataset found: 1,115,394 characters
[OK] Quick tensor test passed

All checks passed! You are ready to start Chapter 2.

If you see this, you are ready to begin.

Watch Out

If a script fails with ModuleNotFoundError: No module named 'torch', your virtual environment is not active. Run the activate command again (venv\Scripts\activate on Windows, source venv/bin/activate on macOS).

Key Takeaways

  • You installed Python (the language), Git (code downloader) and PyTorch (math library), and you opened a terminal.
  • A virtual environment keeps this project's packages isolated.
  • Always activate your virtual environment before working on this project.
  • Once the setup check passes, your environment is ready for the next step in our map.

Check Your Understanding

  1. What does a virtual environment do?
  2. How do you know if your virtual environment is active?
  3. What is the command to move into the project folder?

Assignment

Assignment 1: Read your setup check

About 15 minutes. You run one script and write no code. Work in this chapter's Colab notebook or on your own laptop.

  1. Run it. Run src/ch00_setup_check.py as it is. Copy everything it prints.
  2. Read it. Count the lines that start with [OK]. Next to each one, write a few words that say what the script checked.
  3. Compare. Put your output beside the output printed in Step 8. Which version numbers are different on your computer? Which numbers are exactly the same as in the book?
  4. Explain it. In two or three sentences a manager could follow, say why a team runs a check like this before anyone starts work.

Hand in: your output, your note on each [OK] line, your answers to step 3, and your sentences.

Further Reading

The library you are typing into. This paper argues for the design of the tool this book uses, where you write the model as ordinary Python that runs line by line, so you can print a tensor or stop in a debugger, and still get the speed of compiled code underneath. That design is why the code in this book can be read top to bottom and still trains a real model.

Paszke, A., Gross, S., Massa, F., Lerer, A., Bradbury, J., Chanan, G., Killeen, T., Lin, Z., Gimelshein, N., Antiga, L., Desmaison, A., Köpf, A., Yang, E., DeVito, Z., Raison, M., Tejani, A., Chilamkurthy, S., Steiner, B., Fang, L., ... Chintala, S. (2019). PyTorch: An imperative style, high-performance deep learning library (arXiv:1912.01703). arXiv. https://doi.org/10.48550/arXiv.1912.01703

Note. The Tiny Shakespeare text used throughout the book comes from Andrej Karpathy's char-rnn project: https://github.com/karpathy/char-rnn{ .note }

Next up: Chapter 2: What Is an LLM?