Web Analytics

Installation & Setup

Beginner ~15 min read

Before we start coding in Lua, we need to set up our development environment. In this lesson, you'll learn how to install Lua on Windows, macOS, and Linux, choose a code editor, and run your first Lua script from the command line. Don't worry—it's easier than you think!

Installing Lua

Lua is available for all major operating systems. Choose your platform below:

Windows Installation

There are several ways to install Lua on Windows:

Option 1: LuaForWindows (Recommended for Beginners)

  1. Visit LuaForWindows on GitHub
  2. Download the latest installer (.exe file)
  3. Run the installer and follow the prompts
  4. This includes Lua, LuaRocks (package manager), and several useful libraries

Option 2: Manual Installation

  1. Download pre-compiled binaries from LuaBinaries
  2. Extract to a folder (e.g., C:\Lua)
  3. Add the folder to your PATH environment variable

macOS Installation

The easiest way to install Lua on macOS is using Homebrew:

# Install Homebrew (if not already installed)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

# Install Lua
brew install lua

# Verify installation
lua -v

Linux Installation

Most Linux distributions include Lua in their package repositories:

# Ubuntu/Debian
sudo apt-get update
sudo apt-get install lua5.4

# Fedora
sudo dnf install lua

# Arch Linux
sudo pacman -S lua

# Verify installation
lua -v
Version Note: This tutorial uses Lua 5.4 (the latest version). Lua 5.3 and 5.2 are also widely used and mostly compatible. Most code examples will work across versions, but we'll note any version-specific features.

Verifying Your Installation

Open your terminal (Command Prompt on Windows, Terminal on macOS/Linux) and type:

lua -v

You should see output similar to:

Lua 5.4.6  Copyright (C) 1994-2023 Lua.org, PUC-Rio
Pro Tip: If you get a "command not found" error, Lua might not be in your PATH. On Windows, try restarting your terminal or computer after installation. On macOS/Linux, you may need to add Lua to your PATH in your shell configuration file.

The Lua Interactive Interpreter

Lua comes with an interactive interpreter (REPL - Read-Eval-Print Loop) that's perfect for testing code snippets. Start it by typing lua in your terminal:

$ lua
Lua 5.4.6  Copyright (C) 1994-2023 Lua.org, PUC-Rio
> print("Hello, Lua!")
Hello, Lua!
> x = 10
> print(x * 2)
20
> os.exit()  -- Exit the interpreter

This is great for quick experiments! Try it now—type some simple Lua commands and see the results immediately.

Choosing a Code Editor

While you can write Lua in any text editor, these editors provide excellent Lua support:

Editor Platform Best For Lua Support
VS Code All General development Excellent (with Lua extension)
ZeroBrane Studio All Lua-specific IDE Built-in debugger
Sublime Text All Lightweight editing Good (with packages)
Neovim/Vim All Terminal-based Excellent (native Lua config)

Recommended: Visual Studio Code

For beginners, we recommend VS Code with the Lua extension:

  1. Download and install VS Code
  2. Open VS Code
  3. Go to Extensions (Ctrl+Shift+X or Cmd+Shift+X)
  4. Search for "Lua" and install the "Lua" extension by sumneko
  5. Restart VS Code
Editor Features to Look For:
  • Syntax highlighting
  • Auto-completion
  • Error detection (linting)
  • Integrated terminal
  • Debugger support

Running Lua Scripts

There are three main ways to run Lua code:

1. From a File

Create a file called test.lua with this content:

print("Hello from a Lua file!")
print("2 + 2 =", 2 + 2)

Run it from the terminal:

lua test.lua

2. Using the Interactive Interpreter

As we saw earlier, just type lua and enter commands interactively.

3. One-liner Execution

Execute Lua code directly from the command line:

lua -e "print('Quick test!')"
File Extension: Lua files use the .lua extension. Make sure your text editor doesn't add an extra extension like .txt. The file should be named script.lua, not script.lua.txt.

Installing LuaRocks (Package Manager)

LuaRocks is Lua's package manager, similar to npm for JavaScript or pip for Python. It allows you to easily install third-party libraries.

Windows

LuaForWindows includes LuaRocks. Otherwise, download from luarocks.org.

macOS

brew install luarocks

Linux

# Ubuntu/Debian
sudo apt-get install luarocks

# Fedora
sudo dnf install luarocks

Verify LuaRocks

luarocks --version

Common Installation Issues

1. "lua: command not found"

Problem: Lua is not in your system PATH.

Solution:

  • Windows: Add Lua's bin folder to PATH in System Environment Variables
  • macOS/Linux: Add to PATH in ~/.bashrc or ~/.zshrc

2. Multiple Lua versions installed

Problem: Different versions causing conflicts.

Solution: Use version managers like luaenv or specify the version explicitly (e.g., lua5.4 instead of lua).

Summary

  • Installation: Use package managers (Homebrew, apt, etc.) or download binaries
  • Verification: Run lua -v to check installation
  • Interactive Mode: Type lua for REPL
  • Editor: VS Code with Lua extension recommended for beginners
  • Running Scripts: Use lua filename.lua
  • LuaRocks: Package manager for installing libraries

What's Next?

Congratulations! You now have Lua installed and ready to use. In the next lesson, we'll write our first Lua program—the classic "Hello, World!"—and explore the basic structure of a Lua script. Let's start coding! 🚀