Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. Lua Dictionary
  3. [Setup] Lua Runtime Environment

[Setup] Lua Runtime Environment

'Lua' is a lightweight, fast, and easily embeddable scripting language. It is widely used in game engines (LOVE, Corona SDK (Software Development Kit — a set of tools for building apps on a specific platform)) and embedded systems. This page covers how to install Lua on each OS, and how to use the interactive REPL (Read-Eval-Print Loop, an interactive execution environment) and run script files.

Syntax

# -----------------------------------------------
# Install on Ubuntu / Debian
# -----------------------------------------------
sudo apt update
sudo apt install lua5.4        # Install Lua 5.4

# -----------------------------------------------
# Install on macOS (Homebrew)
# -----------------------------------------------
brew install lua               # Install the latest Lua

# -----------------------------------------------
# Install on Windows (Scoop)
# -----------------------------------------------
scoop install lua              # Install via Scoop package manager

# -----------------------------------------------
# Check version
# -----------------------------------------------
lua -v                         # Show the installed Lua version

# -----------------------------------------------
# Start the interactive REPL
# -----------------------------------------------
lua                            # Running without arguments starts the REPL
# Lua 5.4.6  Copyright (C) 1994-2023 Lua.org, PUC-Rio
# The > prompt accepts Lua expressions
# To exit the REPL, use os.exit() or Ctrl+D

# -----------------------------------------------
# Run a script file
# -----------------------------------------------
lua hello.lua                  # Run the specified script file
lua -e "print('hello')"        # Run a string directly with the -e option

Command Reference

Command / OperationDescription
lua -vShows the installed Lua version.
lua (no arguments)Starts the interactive REPL. Lua expressions and statements entered at the > prompt are evaluated immediately.
lua filename.luaRuns the specified script file. This is the most common way to execute Lua code.
lua -e "code"Runs a string on the command line directly as Lua code.
os.exit()Exits the REPL or the currently running script. In the REPL, Ctrl+D (Unix) also works.
print(expr)The basic function for inspecting values in the REPL. Prints the result of evaluating the expression to standard output.
luac -vShows the Lua compiler version. luac is a tool that compiles Lua scripts to bytecode.

Sample Code

kof_setup.lua
-- kof_setup.lua
-- Uses KOF (The King of Fighters) characters to demonstrate
-- variables, tables, functions, and loops.

-- -----------------------------------------------
-- Variables and print
-- -----------------------------------------------

local title = "THE KING OF FIGHTERS"
local version = _VERSION  -- _VERSION is a global holding the version string (e.g., "Lua 5.4")

print("=== " .. title .. " ===")
print("Runtime: " .. version)
print("")

-- -----------------------------------------------
-- Tables (Lua's combined array and dictionary)
-- -----------------------------------------------

local fighters = {
    { name = "Terry Bogard",  team = "Fatal Fury Team", power = 95 },
    { name = "Andy Bogard",   team = "Fatal Fury Team", power = 90 },
    { name = "Mai Shiranui",  team = "Fatal Fury Team", power = 88 },
    { name = "Joe Higashi",   team = "Fatal Fury Team", power = 85 },
    { name = "Ralf Jones",    team = "Ikari Team",      power = 93 },
}

-- -----------------------------------------------
-- Function definition
-- -----------------------------------------------

local function evaluate(power)
    if power >= 95 then
        return "S Rank"
    elseif power >= 90 then
        return "A Rank"
    else
        return "B Rank"
    end
end

-- -----------------------------------------------
-- Iterating a table with a for loop
-- -----------------------------------------------

print("--- Fighter List ---")
-- ipairs iterates in order with an index
for i, fighter in ipairs(fighters) do
    local rank = evaluate(fighter.power)
    -- string.format produces formatted output
    print(string.format("  %d. %s (%s) Power: %d [%s]",
        i, fighter.name, fighter.team, fighter.power, rank))
end
print("")

-- -----------------------------------------------
-- while loop example
-- -----------------------------------------------

print("--- Countdown ---")
local count = 3
while count > 0 do
    print("  " .. count .. "...")
    count = count - 1  -- Lua has no -= operator; use assignment instead
end
print("  FIGHT!")
print("")

-- -----------------------------------------------
-- String operations
-- -----------------------------------------------

local hero = "Terry Bogard"
print("--- String Operations ---")
print("  Name: " .. hero)
print("  Length (bytes): " .. #hero)             -- # operator returns byte length
print("  Uppercase: " .. string.upper("terry"))  -- Only ASCII characters are uppercased
print("")

-- -----------------------------------------------
-- Closing message
-- -----------------------------------------------

print("Setup complete. Lua is running correctly.")
lua kof_setup.lua
=== THE KING OF FIGHTERS ===
Runtime: Lua 5.4

--- Fighter List ---
  1. Terry Bogard (Fatal Fury Team) Power: 95 [S Rank]
  2. Andy Bogard (Fatal Fury Team) Power: 90 [A Rank]
  3. Mai Shiranui (Fatal Fury Team) Power: 88 [B Rank]
  4. Joe Higashi (Fatal Fury Team) Power: 85 [B Rank]
  5. Ralf Jones (Ikari Team) Power: 93 [A Rank]

--- Countdown ---
  3...
  2...
  1...
  FIGHT!

--- String Operations ---
  Name: Terry Bogard
  Length (bytes): 12
  Uppercase: TERRY

Setup complete. Lua is running correctly.

Overview

'Lua' is a lightweight scripting language implemented in C. It can be installed on any OS using the standard package manager (apt, Homebrew, or Scoop). Use lua -v to check the version, run lua alone to start the interactive REPL and evaluate expressions on the spot, and run lua filename.lua to execute a script file. Because Lua variables are global by default, attaching local to variable declarations is the standard practice for limiting scope. The 'table' — which unifies arrays and dictionaries — is the central data structure in Lua. See also Table Basics.

If you find any errors or copyright issues, please .