Friday, May 20, 2016

Get Command Line Lua Working On Mac And Windows

Having used Lua for a long time now, I realised that I've never actually run it as a stand-alone feature. So I figured I would find out how to run it from the command line. There are two routes to take and I stumbled a few times, until realising that it's way simpler than it can appear. I guess it depends on your mindset as to how you interpret some of the steps.

Note: This post was written when Lua 5.3.2 was the current version, but as none of the steps (or formal references) here have changed since v1, I don't see that it would be a different process for any other version.

I should point out here that I have been using Lua during my use of Corona SDK by CoronaLabs and it is so easy and just a great language. It is a lot older than it might appear, having been created around 1991. Check the bottom of this post for all the references.

So, here's how to get it working step by step...

Command Line Lua on Mac from the Binary:
  • Download the latest version for "Mac OS X Intel Executables" from:
  • This will get you a .tar file with a name like "lua-5.3.2_MacOS1011_bin.tar" (relevant to the version you downloaded) in your download location.
  • Decompress the .tar file and you will have a directory with the same name, minus the ".tar", something like "lua-5.3.2_MacOS1011_bin"
    • At this point you have downloaded the Lua binary and have everything you need to run Lua code.
  • Open Terminal by either going to Launchpad, Spotlight or Applications->Utilities->Terminal
    • This is the command line program for Mac and lets you execute instructions on the Mac directly. It is similar to CMD.exe on Windows, but uses a Unix instruction set. (You don't need to worry about this.)
  • If you have downloaded the Lua binary to Downloads in your own user folder you need to navigate to that. Steps:
    • The full path for your own user directory will be something like: /users/mattw and the terminal is probably already there.
    • Type: pwd to find out where you are. You should see something like /users/mattw
    • If you are in your user directory you can see what is in it by typing (this is not necessary): ls
    • To go into the Downloads folder type: cd Downloads
      • Tip: To autocomplete any file or folder name you can type part of it and then hit Tab
    • If you type ls again you will probably see a lot more - everything which is in your Downloads folder.
    • Type: cd lua-5.3.2_MacOS1011_bin to go into the decompressed Lua binary folder (you might want to type cd lua and press Tab here)
    • Type ls again to see what is inside the folder for your version of Lua.
    • You should now see two files. They will be called "lua" and "luac" plus the shortened version number of Lua which you have downloaded.
      • Eg: "lua53" and "luac53"
  • Note: From here we will check that the Lua binary is working on the command then. You can skip ahead to run a lua program, if you have one.
  • Check that you can run Lua on the Terminal command line by typing: ./lua53 (The . and / are important!)
    • You should see this (with the appropriate version and date):
      • Lua 5.3.2  Copyright (C) 1994-2015, PUC-Rio
    • Type:
      • > print("Hello World!")
    • You should see this:
      • > print("Hello World!")
        Hello World!
    • You have successfully installed Lua and executed your first line of Lua code!
    • One thing to note is that you are currently in the Lua command line - not the Terminal command line any more.
    • To get back to regular Terminal, type CTRL+C
    • From here you can run Lua programs which are saved in ".lua" files.
  • To run a Lua program create a new text file in a text editor (e.g. TextEdit) and save it in the same folder as the Lua binary - inside the decompressed Lua folder - and make sure it ends with ".lua" (all Lua programs must end in .lua)
    • Create a file in a text editor.
    • In the text file, copy and paste this:
math.randomseed( os.time() )
print( "%c" ) )
print( os.time() )
for i=1, 10 do
print("Counter: "..i)
    • Save the file inside the decompressed Lua folder and name it: main.lua
    • To run the program, go back to Terminal and type:
      • ./lua53 main.lua
      • Again, the "./" at the start is important and the "53" is for the version I am using. Yours may be different.
    • You should now see something similar to this:
Sat May 21 07:14:31 20161463811271Counter: 1Counter: 2Counter: 3Counter: 4Counter: 5Counter: 6Counter: 7Counter: 8Counter: 9Counter: 1048
    • As you can probably see in the program file, this is the current date, an integer representing the current date, a loop from 1 to 10 and a random number.
    • I'm not going to explain the Lua language here - there are plenty of references for that (see below) but you can start here:
    • The Corona Labs site also has a very good documentation system, but it is heavily geared towards their libraries - Corona is a very good starting point to learn Lua:
Command Line Lua on Mac from the Source:

Before we get started, I do need to point out that you will need XCode by Apple installed to be able to compile the Lua source into a binary. This is not a problem as it is available from the Mac App Store and Apple Developer website, but if you do install it and don't run it first you will likely see a message about the license (this would happen when XCode gets updated, as well.) When this happens, simply run XCode and accept the update terms. You can quit it immediately, as it is not necessary to have it running to build the Lua source code.
  • TBC
Command Line Lua on Windows from the Binary:
  • TBC
Command Line Lua on Windows from the Source:
  • TBC

1 comment:

  1. Wow this explanation will be clear the doubts with the command lines. By explaining with proper execution we can understand with the concepts. Thank you for posting and please keep update like this.

    Car Wash Services in Mumbai