06. Setup

Before writing any Lua, four things need to be on the computer:

  1. The book's files (the chapters and the example code).
  2. The Lua interpreter (the program that runs .lua files).
  3. A code editor.
  4. A terminal for typing commands.

This chapter sets up all four on Windows 10 or 11.

1. Get the book files

Every chapter has matching code in exercises/, and the mini-projects live in projects/. To run any of it, the whole folder needs to be on the computer.

There are two ways to get it. Use whichever your parent set up.

Method A: copy from a USB stick or a shared cloud folder

Your parent gives you a folder called Learning-Lua-Book-101 on a USB stick, in OneDrive, or another shared folder.

  1. Copy the whole folder to Documents. The final path should look like C:\Users\<your-name>\Documents\Learning-Lua-Book-101.
  2. Open it in File Explorer and confirm it contains a book folder, a book-src folder, an exercises folder, and a README.md file.

Method B: clone from GitHub with Git

If your parent put the book on GitHub and gave you the URL:

  1. Install Git for Windows from https://git-scm.com/download/win, accepting all the defaults.

  2. Open PowerShell (Windows key, type PowerShell, press Enter).

  3. Change into your Documents folder:

     cd $HOME\Documents
  4. Clone the repository (use the URL your parent gave you):

     git clone <the-url-your-parent-gave-you>
  5. You now have the book's files in a folder under Documents.

Either way, when this chapter says "open the book's folder", it means the one you just placed in Documents.

2. Install the Lua interpreter

There is more than one way to install Lua on Windows. Pick one of the two methods below. If the first fails, try the second.

Windows 10 and 11 ship with winget, a tool that installs software from the command line. To use it:

  1. Press the Windows key, type PowerShell, and open it.

  2. Type:

     winget search lua

    A table appears listing packages whose name or description contains "lua". Look for a package called Lua published by Lua.org or DEVCOM. Copy the value in the Id column for that row.

  3. Install it with:

     winget install --id <paste-the-id-here>

    For example, if the Id is DEVCOM.Lua, the command is winget install --id DEVCOM.Lua.

  4. Close PowerShell and open a new window. This matters: the new window picks up the PATH change the installer made.

  5. Verify the install:

     lua -v

    You should see a line that starts with Lua 5. followed by a version number. If you see that, Lua is installed. Skip to step 3.

If lua -v prints 'lua' is not recognised as the name of a cmdlet, function, script file, or operable program, the installer did not add Lua to PATH. Use Method B instead.

Method B: portable Lua from LuaBinaries

If winget did not work, install Lua manually:

  1. Search the web for LuaBinaries Windows x64 and open the SourceForge page.
  2. Download the file for Lua 5.4 ending in Win64_bin.zip.
  3. Make a folder called C:\lua and extract the zip into it. It should now contain lua54.exe.
  4. Rename lua54.exe to lua.exe (or leave it named lua54.exe and adjust later commands to match).
  5. Add C:\lua to your PATH:
    • Press Windows key, type environment variables, and open Edit the system environment variables.
    • Click Environment Variables....
    • Under User variables, select Path and click Edit....
    • Click New, type C:\lua, click OK on each window.
  6. Open a new PowerShell window and run lua -v. You should see the Lua version.

3. Install a code editor

Use Visual Studio Code (VS Code for short). It is free, runs on Windows, and is the same editor most professionals use.

  1. Open https://code.visualstudio.com and download the installer.
  2. Run it. On the Select Additional Tasks screen, tick Add to PATH and Register Code as an editor for supported file types.
  3. Open VS Code once the install finishes.
  4. Install the Lua language extension:
    • Click the Extensions icon in the left bar (or press Ctrl + Shift + X).
    • Type lua in the search box.
    • Find the extension by sumneko (the publisher's name is shown under the title; it is sometimes listed as Lua Language Server).
    • Click Install.

This adds syntax colouring, autocomplete, and inline error hints for .lua files.

4. The terminal

VS Code has a built in terminal. Open it with **Ctrl + ** (the backtick key, usually under Esc`).

This is where you type lua some-file.lua to run a script; its output appears in the same terminal.

5. Verify everything works

Open VS Code. Choose File → Open Folder... and pick the folder where you cloned this book. The folder tree appears on the left.

In the tree, expand exercises/07/ and click 01-hello.lua. Its contents appear in the editor.

Open the terminal (Ctrl + `) and type:

lua exercises/07/01-hello.lua

You should see this line in the terminal:

Hello, world!

If you do, the whole setup is working. Move on to Chapter 7.

If you see cannot open exercises/07/01-hello.lua, the terminal is pointed at the wrong folder. It should be open inside the book's folder. If not, close the terminal and re-open the folder with File → Open Folder....

How the example code is organised

From here on, chapters will tell you to "open exercises/..." files, so it helps to know how that folder is laid out.

Every chapter with code gets a matching folder under exercises/, numbered to match the chapter: Chapter 7's code is in exercises/07/, Chapter 20's is in exercises/20/, and so on. (Part 1 is all on paper, so the code begins at Chapter 7.) The mini-projects are separate, in projects/.

Each chapter's folder holds three kinds of file:

  • Example files at the top level, like exercises/08/01-comment-styles.lua. These are the small programs the chapter's Try this boxes ask you to run.
  • A homework/ folder with a starter file for each homework problem, like exercises/08/homework/01-annotate.lua.
  • A homework/solutions/ folder with a worked answer for every homework problem. Try the homework yourself first — the answers are there for when you get stuck or want to check your work.

So when Chapter 8 says "Open exercises/08/homework/01-annotate.lua", that is the file at exactly that path inside the book folder you opened.

What this chapter did not cover

  • Roblox Studio. Roblox uses its own Lua, called Luau, and its scripts run inside Studio, not in the terminal. The final chapter covers Studio. For now, everything runs with the regular lua interpreter.
  • Other editors. Others work fine (Notepad++, Sublime Text, ZeroBrane Studio), but the book assumes VS Code from here on. With a different editor the keyboard shortcuts change, but the code is identical.