Lua - Installation & Setup

Overview

Estimated time: 10–15 minutes

This tutorial guides you through installing the Lua interpreter on your system. We'll cover installation on Windows, macOS, and Linux, and verify that everything is working correctly.

Learning Objectives

  • Install Lua on your operating system
  • Verify the installation works correctly
  • Understand different ways to run Lua code
  • Set up a basic development environment

Prerequisites

  • Basic familiarity with command line/terminal
  • Administrator access to install software

Installation Methods

Windows Installation

Method 1: Pre-compiled Binaries (Recommended)

  1. Visit Lua Binaries
  2. Download the Windows x64 version (or x86 for 32-bit systems)
  3. Extract the zip file to a folder like C:\Lua
  4. Add C:\Lua to your system PATH:
    • Right-click "This PC" → Properties → Advanced system settings
    • Click "Environment Variables"
    • Edit the "Path" variable and add C:\Lua

Method 2: Using Chocolatey

choco install lua

Method 3: Using Scoop

scoop install lua

macOS Installation

Method 1: Using Homebrew (Recommended)

brew install lua

Method 2: Using MacPorts

sudo port install lua

Method 3: Building from Source

curl -R -O http://www.lua.org/ftp/lua-5.4.6.tar.gz
tar zxf lua-5.4.6.tar.gz
cd lua-5.4.6
make macosx test
sudo make install

Linux Installation

Ubuntu/Debian

sudo apt update
sudo apt install lua5.4

CentOS/RHEL/Fedora

# CentOS/RHEL
sudo yum install lua

# Fedora
sudo dnf install lua

Arch Linux

sudo pacman -S lua

Building from Source (Any Linux)

curl -R -O http://www.lua.org/ftp/lua-5.4.6.tar.gz
tar zxf lua-5.4.6.tar.gz
cd lua-5.4.6
make linux test
sudo make install

Verifying Installation

Once installed, verify Lua is working by opening a terminal/command prompt and running:

lua -v

Expected Output:

Lua 5.4.6  Copyright (C) 1994-2023 Lua.org, PUC-Rio

Running Lua Code

Interactive Mode (REPL)

Start the interactive interpreter by typing lua:

$ lua
Lua 5.4.6  Copyright (C) 1994-2023 Lua.org, PUC-Rio
> print("Hello, World!")
Hello, World!
> 2 + 3
5
> os.exit()

Running Script Files

Create a file called hello.lua:

print("Hello from a Lua script!")
print("Current Lua version:", _VERSION)

Run it with:

lua hello.lua

Expected Output:

Hello from a Lua script!
Current Lua version:	Lua 5.4

One-liner Execution

Execute Lua code directly from the command line:

lua -e "print('Hello from command line!')"

Expected Output:

Hello from command line!

Development Environment Setup

Text Editors and IDEs

Visual Studio Code (Recommended)

  1. Install Visual Studio Code
  2. Install the "Lua" extension by sumneko
  3. Install the "Lua Debug" extension for debugging support

ZeroBrane Studio

  • Download from ZeroBrane Studio
  • Lua-specific IDE with built-in debugger
  • Great for beginners

Other Options

  • Sublime Text: With Lua syntax highlighting package
  • Atom: With language-lua package
  • Vim/Neovim: Built-in Lua support
  • IntelliJ IDEA: With Lua plugin

Online Lua Interpreters

For quick testing without local installation:

LuaJIT Alternative

LuaJIT is a Just-In-Time compiler for Lua that offers much better performance:

Installing LuaJIT

# macOS with Homebrew
brew install luajit

# Ubuntu/Debian
sudo apt install luajit

# Windows with Chocolatey
choco install luajit

Using LuaJIT

luajit -v
luajit hello.lua

Expected Output:

LuaJIT 2.1.0-beta3 -- Copyright (C) 2005-2017 Mike Pall.

Testing Your Setup

Create and run this test script to verify everything is working:

-- test.lua
print("=== Lua Installation Test ===")
print("Lua version:", _VERSION)
print("Platform:", package.config:sub(1,1) == "\\" and "Windows" or "Unix-like")

-- Test basic functionality
local function factorial(n)
    if n <= 1 then
        return 1
    else
        return n * factorial(n - 1)
    end
end

print("Factorial of 5:", factorial(5))

-- Test table functionality
local fruits = {"apple", "banana", "orange"}
print("Fruits:", table.concat(fruits, ", "))

print("=== All tests passed! ===")

Run with:

lua test.lua

Expected Output:

=== Lua Installation Test ===
Lua version:	Lua 5.4
Platform:	Unix-like
Factorial of 5:	120
Fruits:	apple, banana, orange
=== All tests passed! ===

Common Installation Issues

  • "lua: command not found": Lua is not in your PATH. Reinstall or add to PATH manually.
  • Permission denied: Use sudo on Unix systems for global installation.
  • Old version installed: Some systems have Lua 5.1. Use lua5.4 command if available.
  • Windows path issues: Use forward slashes or double backslashes in file paths.

Checks for Understanding

  1. How do you check which version of Lua is installed?
  2. What's the difference between running lua and lua filename.lua?
  3. What is LuaJIT and why might you use it?
  4. How do you execute a one-line Lua command from the terminal?
Show answers
  1. Run lua -v in the terminal/command prompt.
  2. lua starts the interactive REPL, while lua filename.lua executes a script file.
  3. LuaJIT is a Just-In-Time compiler that provides much better performance than standard Lua.
  4. Use lua -e "your code here", for example: lua -e "print('Hello')"

Next Steps

Now that Lua is installed and working, you're ready to write your first program! The next tutorial will walk you through creating and running your first Lua script.