Install Lua on macOS

Installing Lua on macOS can be done using several methods. Homebrew is the most straightforward approach, but you can also use MacPorts or compile from source.

Using Homebrew (Recommended)

Homebrew is the most popular package manager for macOS and provides easy installation of Lua.

  1. Install Homebrew if you haven’t already:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  1. Install Lua:
brew install lua
  1. Verify the installation:
lua -v

Using MacPorts

If you prefer MacPorts over Homebrew:

  1. Install MacPorts from macports.org

  2. Install Lua:

sudo port install lua

Installing Multiple Versions

Homebrew allows you to install multiple Lua versions:

# Install specific versions
brew install [email protected]
brew install [email protected]

# Switch between versions
brew switch lua 5.4.4

Compiling from Source

For maximum control or if you need custom compilation options:

  1. Install Xcode Command Line Tools if you haven’t already:
xcode-select --install
  1. Download and compile Lua:
curl -R -O https://www.lua.org/ftp/lua-5.4.4.tar.gz
tar zxf lua-5.4.4.tar.gz
cd lua-5.4.4
make macosx test
sudo make install

Verifying Installation

Test your installation by creating and running a simple script:

echo 'print("Hello from Lua on macOS!", _VERSION)' > test.lua
lua test.lua

Using Interactive Mode

Start the Lua interactive interpreter to experiment:

lua

You’ll see the Lua prompt where you can enter commands:

> print("Hello, World!")
Hello, World!
> math.sqrt(16)
4.0
> os.exit()

Integrating with IDEs

Many code editors have excellent Lua support:

  • Visual Studio Code: Install the “Lua” extension
  • Sublime Text: Use the “Lua” package via Package Control
  • Vim/Neovim: Built-in support or use plugins like lua.vim

Next Steps

With Lua installed on your macOS system, you’re ready to start programming. Check out our Lua basics guide to learn the fundamentals, or install LuaRocks to manage Lua packages and libraries.

For more macOS-specific Lua information and troubleshooting, see the Lua Wiki.

Last updated on