Lua is a simple language. Sadly setting up Windows to do development in Lua can be a pain. If you know where to look you can find the version 5.4 binaries (here). That's fine if you just want to learn the language. Pretty soon you'll want to supplement the minimal Lua standard library with some third-party packages. This requires LuaRocks, which can be a pain to configure for Windows (it needs a C compiler on hand to build things). Even if you do get it setup you'll often find that some packages just wont build (the essential LuaSocket being a prime example).
So what to do? Admit defeat? Well yes, sort of...
Windows Subsystem for Linux feels like cheating. Instead of going through the pain of Lua on Windows, we just use a Linux virtual machine instead. Setup is simple on the default Ubuntu image, but there are one or two things to look out for. The rest of this document will walk through setting up Lua on WSL on Windows.
Open a terminal window and run the following command:
wsl --install
This will setup an Ubuntu Linux distro using WSL. Once installed (and your machine restarted if asked to), search for Ubuntu in the Windows search bar and run it. If you have Terminal installed (and why not?), then you can open that instead and the open a new tab icon should have Ubuntu listed.
Follow the prompts to setup Ubuntu for the first time. Don't forget to make a note the password you enter when prompted. You will need this to run subsequent commands in Linux.
In your Ubuntu terminal, run:
sudo apt update
This will update your package sources for Ubuntu's package manager. Then run:
sudo apt install lua5.4
This will run the install of Lua5.4 (currently the latest version) on your Ubuntu instance. After tha you will need to also install the Lua dev packages, so you can build third-party libraries with LuaRocks. Run:
sudo apt install liblua5.4-devIf you run the command lua -v in the Ubuntu terminal window, you will see it still says version 5.1. Why is that? Well there are a couple of symbolic links on Ubuntu that point the lua command to the older version. Here is how to update them both:
cd /etc/alternatives
sudo ln -sf /usr/bin/lua5.4 lua-interpreter
sudo ln -sf /usr/bin/luac5.4 lua-compiler
Run the command lua -v again and it should now report version 5.4. Great!
Lua is very small, so you will inevitably want to install some third-party packages at some point in time. The program LuaRocks is the default package manager for Lua development. Run the following to install it:
sudo apt install luarocksWhen running the luarocks command you need to specify the version of Lua you are targetting (your Ubuntu instance already has 5.1 and 5.4 installed at this point). You can either supply a command line argument every time, or run a wrapper script as follows:
sudo luarocks-5.4 dkjson
This will install the dkjson package. Go ahead and also install the very useful luasocket package.
VS Code makes for a great editor / lightweight IDE for Lua development. Install the following marketplace extensions to enable Lua support and support for opening VS Code from inside WSL.
WSL (by Microsoft)
Lua (by sumneko)
Local Lua Debugger (by Tom Blind)
Now in you Ubuntu terminal you can type the following to launch VS Code on the current directory:
code .To make sure we have everything working, let's write a simple test program in Lua.
local json = require "dkjson"
local http = require "socket.http"
local url = "http://api.open-notify.org/astros.json"
local response = http.request(url)
local astros = json.decode(response)
if astros.message ~= "success" then
io.write(" Error fetching data.\n")
os.exit(1)
end
local craft = {}
for _, v in ipairs(astros.people) do
if craft[v.craft] == nil then craft[v.craft] = {} end
local people = craft[v.craft]
people[#people + 1] = v.name
end
for name, people in pairs(craft) do
io.write(name, " => ")
for _, person in ipairs(people) do
io.write(person, ", ")
end
io.write("\n")
end
Save the file as astros.lua and run it from an Ubuntu terminal as follows:
lua astros.lua
Congratulations! You have a Lua development environment setup on Windows thanks to WSL.