Chapter 1: Environment Setup¶
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).
Figure 1.2: Download the Python 3.13 installer.
Windows:
- Open your web browser and go to python.org/downloads
- Click the button to download Python 3.13.x.
- Run the downloaded
.exeinstaller. - 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.
- Click "Install Now".
macOS:
- Open your web browser and go to python.org/downloads
- Click the button to download Python 3.13.x.
- Run the downloaded
.pkginstaller 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:
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.
Figure 1.3: Download Git to get the project files.
Windows:
- Go to git-scm.com/download/win
- Download the "64-bit Git for Windows Setup".
- 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):
You should see a message confirming your Git version.
Step 4: Download the Project¶
Now download the project code with a single command.
Then move into the project folder:
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).
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:
macOS:
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):
Then install the other libraries:
Step 7: Download the Shakespeare Dataset¶
We'll be using a few of Shakespeare's works to train our model to write like him. (The Tiny Shakespeare dataset was taken from Andrej Karpathy's char-rnn project on GitHub: https://github.com/karpathy/char-rnn)
$ 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.
...
Since you are given the text with the project, this step just makes sure you have it. If you didn't have the file, this would download the file and verify that it's the same text that we created our book with.
Step 8: Verify Everything Works¶
Run the setup verification script to confirm all pieces are ready:
$ 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've installed Python (programming language), Git (code downloader), and PyTorch (mathematics library) and opened up a terminal.
- The virtual environment is what will keep the packages for this project isolated.
- Remember to activate your virtual environment whenever you're working on this project.
- When the setup check passes, you're ready to move on to the next step in our map.
Check Your Understanding¶
- What does a virtual environment do?
- How do you know if your virtual environment is active?
- 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.
- Run it. Execute
src/ch00_setup_check.pyas-is. Copy the output. - Read it. Count the lines beginning with
[OK]. Beside each, add a phrase or two describing what was checked. - Compare. Place your output next to the output printed in Step 8. What version numbers differ on your machine? What numbers are identical to those shown in the book?
- Explain it. In two or three sentences that a manager could understand, describe why a team runs a check like this before allowing any work to begin.
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 how the tool we will be using in this book was designed. You are able to write Python code that runs line by line and can print a tensor or step through your debugger, all while having the benefit of compiled speed under the hood. This allows the code we will write in this book to be followed line by line and still train a fairly realistic 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?