A friendly, hands-on guide to the Lua programming language โ from your first print to metatables and coroutines.
Quick start โข Chapters โข Cheatsheet โข Exercises โข Resources
Lua (Portuguese for moon) is a small, fast, embeddable scripting language created in 1993 at PUC-Rio in Brazil. The whole interpreter fits in a few hundred kilobytes, yet it powers:
| Where | What |
|---|---|
| ๐ฎ Games | Roblox (Luau), World of Warcraft add-ons, LรVE 2D, Defold, PICO-8 |
| ๐ ๏ธ Tools | Neovim config & plugins, Wireshark dissectors, Redis scripts |
| ๐ Web | OpenResty / Nginx, Kong API gateway |
| ๐ฆ Embedded | Any C/C++ app that needs a scripting layer |
If you can learn one language in a weekend, it's this one. Lua has only 8 types and ~22 keywords.
local function greet(name)
return ("Hello, %s! ๐"):format(name or "world")
end
print(greet("Lua")) --> Hello, Lua! ๐| OS | Command |
|---|---|
| macOS | brew install lua |
| Ubuntu / Debian | sudo apt install lua5.4 |
| Windows | winget install DEVCOM.Lua or scoop install lua |
| Anywhere (no install) | onecompiler.com/lua or the official demo |
Check it worked:
lua -v
# Lua 5.4.x Copyright (C) 1994-20xx Lua.org, PUC-Riogit clone https://github.com/shiprzq/learn-lua.git
cd learn-lua
lua examples/01_hello.lua$ lua
> 2 ^ 10
1024.0
> #"moon"
4Work through them in order โ each takes about 10โ20 minutes and ends with a runnable example.
| # | Chapter | You'll learn | Example |
|---|---|---|---|
| 1 | Getting started | Running Lua, comments, print |
01_hello.lua |
| 2 | Variables & types | local, the 8 types, nil, truthiness |
02_types.lua |
| 3 | Operators & strings | Math, .., string library, patterns |
03_strings.lua |
| 4 | Control flow | if, while, repeat, numeric & generic for |
04_control_flow.lua |
| 5 | Functions | Multiple returns, varargs, closures | 05_functions.lua |
| 6 | Tables | Arrays, dictionaries, ipairs/pairs, table.* |
06_tables.lua |
| 7 | Metatables & OOP | __index, operators, classes, inheritance |
07_oop.lua |
| 8 | Modules | require, writing your own module |
08_modules.lua |
| 9 | Error handling | error, pcall, assert |
09_errors.lua |
| 10 | Coroutines | yield/resume, generators |
10_coroutines.lua |
| 11 | Gotchas & best practices | 1-based indexing, globals, # with holes |
โ |
๐ก Tip: keep the Cheatsheet open in another tab while you read.
Ten bite-sized exercises โ FizzBuzz to a tiny class system โ each with a solution.
lua exercises/solutions/01_fizzbuzz.luaflowchart LR
A[Basics<br/>ch. 1-4] --> B[Functions<br/>ch. 5]
B --> C[Tables<br/>ch. 6]
C --> D[Metatables & OOP<br/>ch. 7]
C --> E[Modules<br/>ch. 8]
D --> F[Errors & Coroutines<br/>ch. 9-10]
E --> F
F --> G{Pick a path}
G --> H[๐ฎ LรVE / Roblox]
G --> I[๐ ๏ธ Neovim]
G --> J[๐ OpenResty]
- ๐ Programming in Lua โ the book, by Lua's creator Roberto Ierusalimschy
- ๐ Lua 5.4 Reference Manual
- ๐ฎ LรVE โ make 2D games in Lua
- ๐งฑ Roblox Creator Docs โ Luau, Roblox's Lua dialect
- โจ๏ธ Neovim Lua guide
- ๐ฆ LuaRocks โ Lua's package manager
Found a typo or want to add a chapter? See CONTRIBUTING.md. Every example is run automatically by GitHub Actions, so please keep them runnable.
MIT ยฉ shipr
If this helped you, leave a โญ โ it helps others find it!