Lua Get Started
You can run every example in this course right here in the browser. To use Lua on your own machine you need the interpreter, a text editor, and a terminal.
Installing Lua
| System | Command |
|---|---|
| Debian / Ubuntu | sudo apt install lua5.4 |
| Fedora | sudo dnf install lua |
| macOS (Homebrew) | brew install lua |
| Windows | winget install DEVCOM.Lua or a "LuaBinaries" download |
Check that it works:
$ lua -v
Lua 5.4.7 Copyright (C) 1994-2024 Lua.org, PUC-RioNote: Some systems name the program
lua5.4instead oflua. Games such as Roblox use their own dialect (Luau) and you write code inside their editor instead of installing anything.
Your first script
Save this as hello.lua:
local name = "Lua learner"
print("Hello, " .. name .. "!")
print("2 + 3 =", 2 + 3)Hello, Lua learner!
2 + 3 = 5
Run it from the terminal:
$ lua hello.lua
Hello, Lua learner!
2 + 3 = 5The second line shows that print puts a tab between its arguments. When you want full control over the spacing, build one string with .. or use string.format (covered later).
The interactive REPL
Typing lua without a file starts the REPL (read-eval-print loop). Since Lua 5.3 you can type an expression and see its value directly:
$ lua
Lua 5.4.7 Copyright (C) 1994-2024 Lua.org, PUC-Rio
> 7 * 6
42
> "moon" .. "light"
moonlight
> os.exit()Press Ctrl+D (or Ctrl+Z then Enter on Windows) to leave.
Running code from the command line
$ lua -e 'print(2^10)' # run a snippet
1024.0
$ lua -i hello.lua # run a file, then stay in the REPL
$ lua script.lua one two # arguments arrive in the global table argInside a script, the command-line arguments are in the table arg: arg[1] is the first argument and arg[0] is the script name.
A script with a bit more in it
local items = { "keyboard", "mouse", "monitor" }
local prices = { 45.5, 19.99, 189 }
local total = 0
for i = 1, #items do
print(items[i], prices[i])
total = total + prices[i]
end
print("Total:", total)keyboard 45.5
mouse 19.99
monitor 189
Total: 254.49
Don't worry about every detail yet: local creates a variable, { … } builds a table, #items is the length of the list, and for i = 1, #items do … end counts from 1 to 3.
Editors
Any text editor works. Popular choices with Lua support:
- VS Code with the "Lua" extension by sumneko (the Lua Language Server: completion, type hints, errors as you type).
- Neovim, which is itself configured in Lua.
- ZeroBrane Studio, a small Lua IDE with a debugger.
About this playground
The examples run on Compiler Explorer's Lua 5.4. Your code is saved there as a file called /app/output.s, so error messages that mention a position look like /app/output.s:3: attempt to …. On your machine the same message would name your own file, for example hello.lua:3:.
Test yourself
Which command runs a Lua script saved as game.lua?
lua game.luarun game.lualuac -r game.lualua --script game
What does print("a", "b") output?
aba b(one space)aandbseparated by a taba, b
Where does a Lua script find its command-line arguments?
- In
argv - In the global table
arg - In
os.args - In
...only