06. Setup
Before writing any Lua, three things need to be on the computer:
- The Lua interpreter (the program that runs
.luafiles). - A code editor.
- 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.
Method A: winget (recommended)
Windows 10 and 11 ship with winget, a tool that installs
software from the command line. To use it:
Press the Windows key, type
PowerShell, and open it.Type:
winget search luaA 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.
Install it with:
winget install --id <paste-the-id-here>For example, if the Id is
DEVCOM.Lua, the command iswinget install --id DEVCOM.Lua.Close PowerShell and open a new window. This matters: the new window picks up the
PATHchange the installer made.Verify the install:
lua -vYou 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:
- Search the web for LuaBinaries Windows x64 and open the SourceForge page.
- Download the file for Lua 5.4 ending in
Win64_bin.zip. - Make a folder called
C:\luaand extract the zip into it. It should now containlua54.exe. - Rename
lua54.exetolua.exe(or leave it namedlua54.exeand adjust later commands to match). - Add
C:\luato yourPATH:- 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.
- Press Windows key, type
- 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.
- Open https://code.visualstudio.com and download the installer.
- Run it. On the Select Additional Tasks screen, tick Add to PATH and Register Code as an editor for supported file types.
- Open VS Code once the install finishes.
- Install the Lua language extension:
- Click the Extensions icon in the left bar (or press Ctrl + Shift + X).
- Type
luain 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:
Choose File → Open Folder..., click New Folder, name it
lua-work, click Select Folder.Press Ctrl + N to create a new file.
Type:
print("Hello, world!")Save it as
hello.luainside the folder (Ctrl + S).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, likeexercises/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
luainterpreter. - 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.