This is part I of my Programming Lua series. In this post, I am going to go into installing Lua and writing our first hello world program.
Installing Lua on Windows
To install Lua on windows, I recommend using LuaRocks. LuaRocks is a distribution of Lua that contains the Lua language and a few tools to get started. You can download an installer for your platform (32/64bit) from https://github.com/luarocks/luarocks/wiki/Download, extract the archive and run the luarocks.exe to install Lua.
Installing Lua on MacOS
To install Lua on MacOS, I recommend using Homebrew. Assuming you have Homebrew installed, open up your terminal and run the following commands to install the Lua formula
brew update
brew install lua
Installing Lua on Unix
For other Unix platforms (Ubuntu, Debian, etc.), I recommend downloading Lua source code, compiling it and installing it. All the tools that are necessary should be available in those platforms by default.
curl -R -O http://www.lua.org/ftp/lua-5.4.2.tar.gz
tar zxf lua-5.4.2.tar.gz
cd lua-5.4.2
make all test
Hello World Program
Writing a program that prints out hello world is the right of passage for most programmers. We are going to write a Lua program to print out hello world to begin our journey in Lua.
Lua ships with a program called REPL which allows you to try out Lua code. REPL stands for Read Evaluate Print Loop. So, let’s start up the Lua REPL by running the lua command. You should see something like this
➜ ~ lua
Lua 5.4.2 Copyright (C) 1994-2020 Lua.org, PUC-Rio
>
To print something in Lua, we need to invoke the built in Lua function print function (We will learn a lot more about functions in a later post. So, don’t worry about it for now). To print hello world in Lua, we need to invoke the print function and pass in the string hello world into the function. Let’s type that into the Lua REPL and see
> print "Hello World"
Hello World
Yay!!! We just wrote a hello world program in Lua.
Once you are ready, you can proceed to the next post in the Programming Lua series – https://adhithyarajasekaran.com/2021/01/12/programming-lua-creating-variables/.
Leave a comment