Writing Your First Lua Program on Linux

There are a multitude of programming languages out there but if you are looking to learn a new language, one that is easy to grasp, fast and open source is Lua. From the Portuguese word for moon, the Lua language is found in some unexpected places. It is used in Adobe’s Photoshop Lightroom and in games like World of Warcraft and Angry Birds. In fact Lua is currently the leading scripting language for games. It is also the language used by Corona, a free software development kit that lets you write apps for smartphones and tablets running iOS or Android.

Installing Lua is simple. On Ubuntu you can use the Software Center or if you prefer the command line use:

sudo apt-get install lua5.1

Once installed, you have access to two tools, lua which is the Lua language interpreter and luac which is the Lua compiler. Programming in Lua is very easy to learn. Using a text editor, create a file called hellomte.lua with the following line:

print ("Hello Make Tech Easier!")

Save the file and then from the command prompt, go to the directory where you saved the file and run the Lua program like this:

lua hellomte.lua

The output, as I hope you expected, was the text Hello Make Tech Easier!. Congratulations you have written your first Lua program!

You can also run Lua as a stand-alone interpreter like you would for bash or python. This means you can write scripts which act like stand-alone executables. Create a file called looknohands without the .lua extension. In the file add:

#!/usr/bin/env lua
print ("Look no hands!")

The first line tells Linux that this is a script file and the script uses lua. The second line prints out the text “Look no hands!” Before the script can be run, it needs to be given execute permission. To do this run the “chmod” command in the directory with the file in it:

chmod +x looknohands

This tells Linux that this script can be executed, to run it just type:

./looknohands

And you will see the text.

lua-look-no-hands

If you have any programming experience, you may be expecting that the Lua compiler generates a binary executable that can be run directly on the host, much like a C compiler would. However the Lua compiler is slightly different. Rather than executable code, it produces binary files that can be later loaded and executed within the Lua interpreter. The main advantages of pre-compiling Lua code is that it loads faster and also it protects the source code from being tampered with, either accidentally or intentionally.

Here is a simple Lua program that loops around 10 times printing some text. Create a file called hellomte10.lua and save it with the following lines of code:

for i=1,10,1 do
print ("Hello Make Tech Easier: ", i)
end

This can be run using the Lua command:

lua hellomte10.lua

However it can also be compiled into Lua binary code like this:

luac -o hellomte10.luac hellomte10.lua

This will create a binary file called hellomte10.luac which can be run just like a normal .lua file:

lua hellomte10.luac

It can also be used from within the stand-alone interpreter. Create a file called hellomte10 without the .lua extension:

#!/usr/bin/env lua
dofile("hellomte10.luac")

The dofile() function will load the binary file and execute it. To run the hellomte10  program grant it execute permission using the chmod command and then run it:

./hellomte10

To distribute pre-compiled Lua programs you need to ship the .luac file along with the stand-alone interpreter script file (i.e. hellomte10.luac and hellomte10), but you don’t need to supply the original .lua file.

Lua is a very flexible language which, as we have seen, can be used in a variety of different ways. Try reading the Programming in Lua book to see what else Lua can do.