Installation & Setup
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)
- Visit LuaForWindows on GitHub
- Download the latest installer (.exe file)
- Run the installer and follow the prompts
- This includes Lua, LuaRocks (package manager), and several useful libraries
Option 2: Manual Installation
- Download pre-compiled binaries from LuaBinaries
- Extract to a folder (e.g.,
C:\Lua) - 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
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
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:
- Download and install VS Code
- Open VS Code
- Go to Extensions (Ctrl+Shift+X or Cmd+Shift+X)
- Search for "Lua" and install the "Lua" extension by sumneko
- Restart VS Code
- 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!')"
.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
~/.bashrcor~/.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 -vto check installation - Interactive Mode: Type
luafor 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! 🚀
Enjoying these tutorials?