Beginner

Hello World in Lua

Your first Lua program - the classic Hello World example with Docker setup

Lua is designed to be simple, and Hello World demonstrates this beautifully. In Lua, printing to the console is just one line—no imports, no classes, no boilerplate.

The Code

Create a file named hello.lua:

1
print("Hello, World!")

That’s it. One line, one function, one string. This simplicity is intentional—Lua was designed to be learnable in hours, not days.

Understanding the Code

  • print() - A built-in function that outputs text to the standard output
  • "Hello, World!" - A string literal enclosed in double quotes
  • No semicolons - Statement terminators are optional in Lua (newlines work)
  • No includes needed - print is available in the global scope by default

Running with Docker

The easiest way to run Lua without installing it locally:

1
2
3
4
5
# Pull the Lua Alpine image (lightweight)
docker pull nickblah/lua:5.4-alpine

# Run the program
docker run --rm -v $(pwd):/app -w /app nickblah/lua:5.4-alpine lua hello.lua

Running Locally

If you have Lua installed on your system:

1
2
3
4
5
# Run with Lua interpreter
lua hello.lua

# Or with Lua 5.4 specifically (if multiple versions installed)
lua5.4 hello.lua

Expected Output

Hello, World!

Alternative Syntax

Lua offers several ways to write strings and call functions:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
-- Double quotes
print("Hello, World!")

-- Single quotes work too
print('Hello, World!')

-- Long strings with [[ ]]
print([[Hello, World!]])

-- Parentheses optional for single string/table arguments
print "Hello, World!"

All of these produce the same output!

Key Concepts

  1. Lua is interpreted - No compilation step; the interpreter reads and executes source directly
  2. Dynamic typing - Variables don’t have fixed types
  3. Automatic memory management - Lua handles garbage collection
  4. 1-indexed arrays - Unlike most languages, Lua arrays start at index 1
  5. Tables everywhere - Almost everything in Lua is built on tables

Interactive Mode (REPL)

Lua has an interactive mode for experimentation:

1
docker run --rm -it nickblah/lua:5.4-alpine lua

Then type:

1
2
3
> print("Hello, World!")
Hello, World!
> os.exit()

The > prompt indicates you’re in the Lua REPL (Read-Eval-Print Loop).

Why Lua for Beginners?

FeatureBenefit
Minimal syntaxLess to memorize
No type declarationsFocus on logic, not types
Fast feedbackInterpreted, no compile step
Small standard libraryLess overwhelming
Consistent rulesFew edge cases

A Slightly Longer Example

Here’s a preview of what’s coming in future tutorials:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
-- Variables
local name = "Lua"
local year = 1993

-- String concatenation uses ..
print("Hello from " .. name .. "!")
print("Created in " .. year)

-- Simple table (like arrays/dicts in other languages)
local languages = {"Lua", "Python", "JavaScript"}
print("First language: " .. languages[1])  -- Note: 1-indexed!

Next Steps

Continue to Variables and Data Types to learn about Lua’s type system and how to store and manipulate data.

Running Today

All examples can be run using Docker:

docker pull nickblah/lua:5.4-alpine
Last updated: