Back

Setup: Installing Lua and VS Code

divider

What You Actually Need

Two things, and the second one matters more than it looks:

  • A way to run a file called main.lua and see what it prints.
  • Lua 5.4 specifically. Not 5.5, which is newer and prints two things differently. More on that below.

That is the whole requirement for Unit 1. There are four ways to get there and none of them is more correct than the others — pick by what your machine lets you do.

Unit 2 is different and needs LÖVE instead. That install is covered on Activity 2.1, when you get there. Nothing on this page expires when you reach it.

divider

First, Check What You Already Have

Do this before installing anything. Lua may already be present, and this one line works in every environment on this page — installed or in a browser tab.

main.lua
print(_VERSION)
What you want to see
Lua 5.4

If it says Lua 5.4, you are done. Skip to the bottom of this page and start Activity 1.1.

If it says Lua 5.5, read the note on versions below — you can still work, you just need to know about two figures that will not match. If it says Lua 5.1, that is LuaJIT, which is what LÖVE runs; it is fine for most of Unit 1 but is not what the outputs here were written against.

divider

Pick a Route

RouteUse it whenInstalls anything?
1 — wingetWindows, and you can install softwareYes, one command
2 — portable WindowsWindows with no admin rightsA folder, no installer
3 — macOSA MacYes, via Homebrew
4 — browserChromebook, locked-down machine, or you just want to start nowNothing at all
divider

Route 1: Windows, One Command

Windows ships a package manager called winget. Open Terminal or PowerShell from the Start menu and run:

PowerShell
winget install DEVCOM.Lua

That installs Lua 5.4.6, which is exactly the version this course was written against, and puts the lua command on your path so it works from any folder. Close and reopen your terminal afterwards, then check:

Confirm it worked
lua -v
Terminal window
Lua 5.4.6 Copyright (C) 1994-2023 Lua.org, PUC-Rio

If winget refuses because you are not an administrator, that is a normal thing to happen on a school machine and not something you did wrong. Use Route 2 or Route 4 instead.

divider

Route 2: Windows, No Admin Rights

Same Lua, no installer. Download Lua-5.4.6-win64.zip from the cmake-lua releases page — it is the same build winget hands you, just as a folder.

  1. Unzip it somewhere permanent. C:\Lua is a good choice; your Documents folder works too.
  2. Keep the bin folder intact. lua.exe needs lua54.dll sitting next to it. Copying just the .exe out on its own will not run.
  3. Either type the full path every time, or add the folder to your path once — see below.
Running it without touching your path
C:\Lua\bin\lua.exe main.lua

Adding it to your path, without admin

Search the Start menu for environment variables and choose Edit environment variables for your account — the for your account one. That version edits only your own settings and does not need an administrator. Select Path, choose Edit, add C:\Lua\bin, then reopen your terminal.

After that, plain lua main.lua works and you can ignore the long path for the rest of the course.

divider

Route 3: macOS

With Homebrew installed:

Terminal
brew install lua@5.4

Ask for lua@5.4, not plain lua. Plain brew install lua now gives you Lua 5.5, which is the version with the two differences described below.

There is one annoyance and it is worth knowing about rather than being surprised by: Homebrew does not put this one on your path, because it keeps older versions separate on purpose. Add it yourself:

Add to ~/.zshrc, then open a new terminal
export PATH="/opt/homebrew/opt/lua@5.4/bin:$PATH"

Then check with lua -v. If it still reports 5.5, the line above is not being read — make sure you added it to ~/.zshrc and opened a new terminal window.

divider

Route 4: Nothing Installed At All

This is the right answer on a Chromebook, on a school machine that will not let you install anything, and any time you would rather start writing Lua in the next thirty seconds than read installation instructions.

Open onecompiler.com/lua, delete the sample code, and type yours. It runs Lua 5.4, it has an input box for the few activities that read from you, and there is nothing to set up.

Run the version check first anyway. Any online tool can change what it runs without telling you, and print(_VERSION) takes one second.

The one place this route needs a workaround

Activity 1.12 uses two files — a module called bay.lua and a main.lua that pulls it in with require("bay"). A single-box editor has nowhere to put the second file.

You can still do the activity, and the trick is real Lua rather than a workaround that dodges the lesson. Paste the module into a package.preload entry above your main code, and require finds it there instead of on disk:

Activity 1.12 in a single file
package.preload["bay"] = function()
-- everything that would have been in bay.lua
local bay = {}
bay.capacity = 6
local docked = 0
function bay.dock(count)
docked = math.min(bay.capacity, docked + count)
return docked
end
function bay.status()
return docked .. " of " .. bay.capacity
end
return bay
end
-- main.lua starts here, unchanged
local bay = require("bay")
print("Capacity:", bay.capacity)

The output is identical to the two-file version, and the activity's point survives: the module is still a table you build privately and return, and docked is still invisible from outside it.

Chromebook, if you have Linux turned on

Chromebooks can run a real Linux terminal. If yours has it enabled, you get the full experience instead:

Linux terminal on ChromeOS
sudo apt update
sudo apt install lua5.4
divider

Why 5.4 and Not 5.5

Lua 5.5 runs everything in this course correctly. It just prints two things differently, and both appear in figures you are meant to compare your own output against. Knowing about them costs less than chasing a bug that is not there.

print(math.sqrt(2))
VersionPrints
5.4 — what Activity 1.4 shows1.4142135623731
5.51.4142135623730951

The other one is in Activity 1.6, where a loop counts 0 to 1 in steps of 0.25. Lua 5.4 prints the ends as 0 and 1; 5.5 prints 0.0 and 1.0.

Every other number in the course is identical on both. Those two are the whole list, so if something else does not match, it is worth reading your code rather than blaming your Lua.

divider

An Editor, If You Installed Lua

Routes 1 to 3 leave you needing somewhere to write files. Any editor works — the rest of the course assumes VS Code because it has a terminal built in, which saves flipping between windows every time you run something.

  1. Install VS Code and open it.
  2. File → Open Folder, and make a new folder for this course. Everything you write lives in there.
  3. Open the terminal with Terminal → New Terminal, or Ctrl+` — that is the backtick key, above Tab. It opens already pointed at your folder.
  4. Make a file called main.lua and run it:
Terminal window
lua main.lua

The Lua extension by sumneko is worth adding for highlighting and error squiggles. It does not include Lua itself, so it is not a substitute for any route above.

divider

You Are Set Up When

  • A file with print(_VERSION) in it runs and prints a version.
  • You know which version that is, so the figures make sense.
  • You know where you are going to keep your files.

That is enough. Activity 1.1 starts with one line of code and builds from there.