Installing Lua is simple. On Ubuntu you can use the Software Center or if you prefer the command line use: 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: 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: 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: 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: This tells Linux that this script can be executed, to run it just type: And you will see the text.

The Luac compiler

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: This can be run using the Lua command: However it can also be compiled into Lua binary code like this: This will create a binary file called hellomte10.luac which can be run just like a normal .lua file: It can also be used from within the stand-alone interpreter. Create a file called hellomte10 without the .lua extension: 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: 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.

Conclusion

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.