06. Setup
Before writing any Lua, four things need to be on the computer:
- The book's files (the chapters and the example code).
- The Lua interpreter (the program that runs
.luafiles). - A code editor.
- 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.
- Copy the whole folder to Documents. The final path
should look like
C:\Users\<your-name>\Documents\Learning-Lua-Book-101. - Open it in File Explorer and confirm it contains a
bookfolder, abook-srcfolder, anexercisesfolder, and aREADME.mdfile.
Method B: clone from GitHub with Git
If your parent put the book on GitHub and gave you the URL:
Install Git for Windows from https://git-scm.com/download/win, accepting all the defaults.
Open PowerShell (Windows key, type
PowerShell, press Enter).Change into your Documents folder:
cd $HOME\DocumentsClone the repository (use the URL your parent gave you):
git clone <the-url-your-parent-gave-you>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.
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.
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.
- 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.
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, 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.
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
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.