06. Setup

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

  1. The Lua interpreter (the program that runs .lua files).
  2. A code editor.
  3. A terminal for typing commands.

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

1. 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.

2. 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.

3. 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.

4. Verify everything works

Open VS Code and create a working folder:

  1. Choose File → Open Folder..., click New Folder, name it lua-work, click Select Folder.

  2. Press Ctrl + N to create a new file.

  3. Type:

     print("Hello, world!")
  4. Save it as hello.lua inside the folder (Ctrl + S).

  5. Open the terminal (Ctrl + `) and type:

     lua hello.lua

You should see:

Hello, world!

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

How the example code is organised

All exercise files are in the Exercises section of this book — browse them online or download any file to run it locally.

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.

When a chapter says "open exercises/08/homework/01-annotate.lua", find that file in the Exercises section, download it, and save it in the same path inside your lua-work folder.

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.