Getting Lua

The first step towards developing with Lua is getting Lua. If you read about the history of Lua, you will find that Lua is quite portable and available for most of the platforms. It is available as an interactive shell where you can run your Lua code interactively or as a set of C API’s that you can include into your own program and allowing for running Lua scripts and exposing your code to be scripted using Lua.

The interactive shell can be used like most shell scripts, like the Windows BAT files or the .sh files on the Linux/MAC OSX platforms. When compiled and included within a framework wrapping OpenGL, it can be used like with Love2D, CoronaSDK, Gideros Studio, Moai, Codea where the Lua C API is used for scripting and manipulating the objects created.

Getting Lua included with the GUI frameworks is easy, download the distributions from the corresponding websites and you are it. It is the interactive shell that can be a little tricky. You can download pre-build binaries for practically most of the platforms or download the source code and build your own.

Windows -> LuaForWindows_v5.1.4-46.exe
Mac OSX -> lua-5.1.4_MacOS107_bin.tar.gz (64-bit)
Mac OSX -> lua-5.1.4_MacOS105x86_bin.tar.gz (32-bit)

You can find more executables for other platforms based on Lua 5.1.4 at this link on SourceForge
http://sourceforge.net/projects/luabinaries/files/5.1.4/Executables/

The advantage of the LuaForWindows bundle is that all of the required libraries are included with this distribution, where as with the Mac or the Linux version, you have to include the libraries you need. Here is the list of the libraries you get included with the Windows bundle (http://code.google.com/p/luaforwindows/)

If you do have to build Lua yourself, you can download the lua source codes for the appropriate version (we are working with 5.1.4) and then using gcc, compile it.

On Mac OSX

curl -R -O http://www.lua.org/ftp/lua-5.1.4.tar.gz
tar zxf lua-5.1.4.tar.gz
cd lua-5.1.4
make macosx local

Note: You might have to also download the curl binaries (if they are not on your system) from http://curl.haxx.se/download.html

On Linux it is similar but wget is used instead of curl as

wget http://www.lua.org/ftp/lua-5.1.4.tar.gz
tar zxf lua-5.1.4.tar.gz
cd lua-5.1.4
make linux local

The compiling is dependent on gcc, a free GNU C compiler. If you are running Linux, this can be added easily from the repository and if you are running Mac OSX, it is part of the XCode command line tools. If you do not want to install XCode just to get gcc, you can get the standalone version from https://github.com/kennethreitz/osx-gcc-installer/downloads (choose the appropriate one for your Mac OS)

One last thing to note on the Mac is that while 10.6 and above are 64-bit kernels, they report i386 with the arch command and what you will need is 32-bit Lua that is for the “i386” arch not the “x86_64” (64-bit). You can read up on this article for more details on setting and compiling the 32-bit and 64-bit versions at http://howto.oz-apps.com/2012/10/how-to-build-from-command-line.html

Once you have installed or compiled Lua, you can start it by simply typing Lua from the terminal/command prompt

you will see the text

Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
> 

where you can type in your Lua commands. Let’s try hello world,

print("Hello World")

and when you press enter, you will see Lua respond with Hello World.

Congratulations, you have run your first interactive Lua program.

Comments are closed.