After a previous post on this topic, this is an update, as several changes have made Lua more Windows friendly; the LuaRocks package manager now is a first-class Windows citizen, and the core network library LuaSocket is available as a working package installable from LuaRocks

Lua itself is pretty barebones, so to make it usefull, you’ll be needing additional libraries. The libraries come in several flavors; pure Lua code, C/C++ code, or binaries (precompiled C/C++ libraries). The pure Lua libraries can be used anyway, but the C libraries tend to mostly be available as source code only, meaning you’d have to compile them. And to do that you need a compiler…

Now if you don’t like compilers and all their complexity, don’t worry. LuaRocks is a package manager for Lua and also works on Windows. Once LuaRocks and the compiler are installed, LuaRocks will deal with all the compiling and building.

This tutorial requires you to know some basics about the command line in Windows, and you must have admin access to the system. That’s all.

What will this tutorial be doing;

install a compiler (MinGW (and MSYS)) compile and install Lua install LuaRocks

Assuming Windows 7 is used and MinGW is being installed in ‘c:\mingw\’

[ad name=”468×60 Banner”]

Installing the compiler

Download MinGW. The ‘MinGW-get’ application allows you install MinGW components.

Start the installer and install the ‘mingw-get’ application (it’s windows caption is “MinGW Installation Manager”)

When installation completes, click ‘continue’ to select the required MinGW packages

Select the ‘basic setup’ on the left and select the following packages on the right; mingw32-base (Basic MinGW installation) mingw-gcc-g++ (The GNU C++ compiler) msys-base (A basic MSYS installation)

Now apply the changes (in menu “Installation”) and exit

As we will later be using the compiler, we need to make sure that the compiler can be found on the system. To do this, its location must be added to the system path. Here’s how;

right-click ‘my computer’ and select ‘properties’, click ‘advanced system settings’, select tab ‘advanced’, click ‘Environment variables…’

find the entry ‘PATH’ under ‘system variables’, and add ‘c:\mingw\bin’ (use a semicolon ‘;’ as a separator) to this variable.

We won’t be adding MSYS to the path, as it has some Unix like utilities that might interfere with the Windows tools.

[ad name=”468×60 Banner”]

Installing Lua

Download Lua source code. For this tutorial we’ll be using ‘lua-5.1.5.tar.gz’, but a 5.2 version should be easy to use as well.

Create a temporary folder ‘c:\temp\’ and store the downloaded file there. Open it and inside there will be a ‘lua-5.1.5’ folder, extract that folder into ‘c:\temp\lua-5.1.5\’.

Now if your system cannot handle ‘.tar.gz’ files (common on Unix, but not on Windows) you can download 7zip, which is a compression utility that handles this format (and many others like .zip and .rar) very well.

Compiling the Lua core files must be done from the command line. So;

open the start menu and type ‘cmd’, once ‘cmd.exe’ is found, open it.

Before we can compile it we must temporarily add ‘MSYS’ to our system path to use some of it’s utilities, and we need to move into our Lua directory. To do so type the following commands;

SET PATH=%PATH%;c:\mingw\msys\1.0\bin CD c:\temp\lua-5.1.5

We’re all set to build our Lua installation, so type the following commands (in order; cleanup to make sure we start clean, then compile it using mingw, finally install the resulting binaries);

make clean make mingw make install INSTALL_TOP=c:/temp/lua/5.1 TO_BIN="lua.exe luac.exe lua51.dll"

CRUCIAL in the last line:

the version numbers 5.1 and 51 appear and must be correct (if using another version than 5.1)

‘c:/temp/lua/5.1’ uses FORWARD slashes (a Unix convention)

This should be your result:

Microsoft Windows [Version 6.1.7601] Copyright (c) 2009 Microsoft Corporation. All rights reserved. C:\windows\system32>SET PATH=%PATH%;c:\mingw\msys\1.0\bin C:\windows\system32>CD c:\temp\lua-5.1.5 c:\temp\lua-5.1.5>make clean cd src && make clean make[1]: Entering directory `/c/temp/lua-5.1.5/src' rm -f liblua.a lua luac lapi.o lcode.o ldebug.o ldo.o ldump.o lfunc.o lgc.o llex.o lmem.o lobject.o lopcodes.o lparser.o lstate.o lstring.o ltable.o ltm.o lundu mp.o lvm.o lzio.o lauxlib.o lbaselib.o ldblib.o liolib.o lmathlib.o loslib.o ltablib.o lstrlib.o loadlib.o linit.o lua.o luac.o print.o make[1]: Leaving directory `/c/temp/lua-5.1.5/src' c:\temp\lua-5.1.5>make mingw cd src && make mingw make[1]: Entering directory `/c/temp/lua-5.1.5/src' make "LUA_A=lua51.dll" "LUA_T=lua.exe" \ "AR=gcc -shared -o" "RANLIB=strip --strip-unneeded" \ "MYCFLAGS=-DLUA_BUILD_AS_DLL" "MYLIBS=" "MYLDFLAGS=-s" lua.exe make[2]: Entering directory `/c/temp/lua-5.1.5/src' gcc -O2 -Wall -DLUA_BUILD_AS_DLL -c -o lua.o lua.c gcc -O2 -Wall -DLUA_BUILD_AS_DLL -c -o lapi.o lapi.c gcc -O2 -Wall -DLUA_BUILD_AS_DLL -c -o lcode.o lcode.c gcc -O2 -Wall -DLUA_BUILD_AS_DLL -c -o ldebug.o ldebug.c gcc -O2 -Wall -DLUA_BUILD_AS_DLL -c -o ldo.o ldo.c gcc -O2 -Wall -DLUA_BUILD_AS_DLL -c -o ldump.o ldump.c gcc -O2 -Wall -DLUA_BUILD_AS_DLL -c -o lfunc.o lfunc.c gcc -O2 -Wall -DLUA_BUILD_AS_DLL -c -o lgc.o lgc.c gcc -O2 -Wall -DLUA_BUILD_AS_DLL -c -o llex.o llex.c gcc -O2 -Wall -DLUA_BUILD_AS_DLL -c -o lmem.o lmem.c gcc -O2 -Wall -DLUA_BUILD_AS_DLL -c -o lobject.o lobject.c gcc -O2 -Wall -DLUA_BUILD_AS_DLL -c -o lopcodes.o lopcodes.c gcc -O2 -Wall -DLUA_BUILD_AS_DLL -c -o lparser.o lparser.c gcc -O2 -Wall -DLUA_BUILD_AS_DLL -c -o lstate.o lstate.c gcc -O2 -Wall -DLUA_BUILD_AS_DLL -c -o lstring.o lstring.c gcc -O2 -Wall -DLUA_BUILD_AS_DLL -c -o ltable.o ltable.c gcc -O2 -Wall -DLUA_BUILD_AS_DLL -c -o ltm.o ltm.c gcc -O2 -Wall -DLUA_BUILD_AS_DLL -c -o lundump.o lundump.c gcc -O2 -Wall -DLUA_BUILD_AS_DLL -c -o lvm.o lvm.c gcc -O2 -Wall -DLUA_BUILD_AS_DLL -c -o lzio.o lzio.c gcc -O2 -Wall -DLUA_BUILD_AS_DLL -c -o lauxlib.o lauxlib.c gcc -O2 -Wall -DLUA_BUILD_AS_DLL -c -o lbaselib.o lbaselib.c gcc -O2 -Wall -DLUA_BUILD_AS_DLL -c -o ldblib.o ldblib.c gcc -O2 -Wall -DLUA_BUILD_AS_DLL -c -o liolib.o liolib.c gcc -O2 -Wall -DLUA_BUILD_AS_DLL -c -o lmathlib.o lmathlib.c gcc -O2 -Wall -DLUA_BUILD_AS_DLL -c -o loslib.o loslib.c gcc -O2 -Wall -DLUA_BUILD_AS_DLL -c -o ltablib.o ltablib.c gcc -O2 -Wall -DLUA_BUILD_AS_DLL -c -o lstrlib.o lstrlib.c gcc -O2 -Wall -DLUA_BUILD_AS_DLL -c -o loadlib.o loadlib.c gcc -O2 -Wall -DLUA_BUILD_AS_DLL -c -o linit.o linit.c gcc -shared -o lua51.dll lapi.o lcode.o ldebug.o ldo.o ldump.o lfunc.o lgc.o llex.o lmem.o lobject.o lopcodes.o lparser.o lstate.o lstring.o ltable.o ltm.o lundump.o lvm.o lzio.o lauxlib.o lbaselib.o ldblib.o liolib.o lmathlib.o loslib.o ltablib.o lstrlib.o loadlib.o linit.o # DLL needs all object files strip --strip-unneeded lua51.dll gcc -o lua.exe -s lua.o lua51.dll -lm make[2]: Leaving directory `/c/temp/lua-5.1.5/src' make "LUAC_T=luac.exe" luac.exe make[2]: Entering directory `/c/temp/lua-5.1.5/src' gcc -O2 -Wall -c -o luac.o luac.c gcc -O2 -Wall -c -o print.o print.c ar rcu liblua.a lapi.o lcode.o ldebug.o ldo.o ldump.o lfunc.o lgc.o llex.o lmem.o lobject.o lopcodes.o lparser.o lstate.o lstring.o ltable.o ltm.o lundump.o lvm.o lzio.o lauxlib.o lbaselib.o ldblib.o liolib.o lmathlib.o loslib.o ltablib.o lstrlib.o loadlib.o linit.o # DLL needs all object files ranlib liblua.a gcc -o luac.exe luac.o print.o liblua.a -lm make[2]: Leaving directory `/c/temp/lua-5.1.5/src' make[1]: Leaving directory `/c/temp/lua-5.1.5/src' c:\temp\lua-5.1.5>make install INSTALL_TOP=c:/temp/lua/5.1 TO_BIN="lua.exe luac.exe lua51.dll" cd src && mkdir -p c:/temp/lua/5.1/bin c:/temp/lua/5.1/include c:/temp/lua/5.1/lib c:/temp/lua/5.1/man/man1 c:/temp/lua/5.1/share/lua/5.1 c:/temp/lua/5.1/lib/lua/5.1 cd src && install -p -m 0755 lua.exe luac.exe lua51.dll c:/temp/lua/5.1/bin cd src && install -p -m 0644 lua.h luaconf.h lualib.h lauxlib.h ../etc/lua.hpp c:/temp/lua/5.1/include cd src && install -p -m 0644 liblua.a c:/temp/lua/5.1/lib cd doc && install -p -m 0644 lua.1 luac.1 c:/temp/lua/5.1/man/man1 c:\temp\lua-5.1.5>

Completing the setup

Now you can close the command window (important, you cannot re-use this instance!)

Open the ‘c:\temp’ folder in a Windows explorer window and you can move the complete ‘c:\temp\lua’ directory to ‘c:\program files\’ (or ‘c:\program files (x86)’ on 64bit systems)

Open the ‘PATH’ variable again in the ‘environment variables’ dialog box (see ‘installing the compiler’ above), this time adding the location of ‘lua.exe’ to the system path (that would be ‘c:\program files\lua\5.1\bin’ or include the ‘(x86)’ in case of a 64bit system).

Congratulations, you’ve just compiled and installed Lua! Open a command window and type;

lua -e "print('hello world')"

to test it (if it doesn’t work, make sure to open a new command window, because of the change to the PATH variable)

[ad name=”468×60 Banner”]

Installing LuaRocks

Download LuaRocks, make sure to download a Windows version, ending with ‘-win32.zip’, and a minimum version 2.1.2.

Open the downloaded zip file and drag the ‘luarocks-2.x.x-win32’ folder inside to ‘c:\temp\’ to extract the contents to the temporary directory.

Now open a new command prompt (to make sure our changes to the PATH variable are into effect)

Move into the luarocks directory (replace x.x with the version you downloaded) with the following command;

CD c:\temp\luarocks-2.x.x-win32

Install it using the following command (use ‘install /?’ first to get an explanation of the options), but do not click the command window away when it is done! it contains information on paths that need to be set. (NOTE: you might be prompted for administrator credentials)

install /MW /F /LV 5.1

When done, take note of the paths it lists at the end. It refers to PATH, LUA_PATH and LUA_CPATH, make a print screen, or copy the text from the command window to keep it handy. If you use Lua 5.2 or later you don’t need LUA_PATH and LUA_CPATH, but their 5.2 equivalents LUA_PATH_5_2 and LUA_CPATH_5_2.

Open the window with the ‘environment variables’ again and add the values given to the variables there (only if they are not there already). LUA_PATH and LUA_CPATH (or _5_2 versions) probably do not exist, so you’ll have to create them. This step is crucial for your Lua installation to work properly, so be carefull to get this right!

For an explanation of the rocks trees and the paths, see http://luarocks.org/en/Rocks_repositories.

Now close all command windows and open a new one and type

luarocks help

to see whether your new LuaRocks installation is found.

Now install some very common, basic libraries to test the LuaRocks-MinGW combination. Install the Lua file system module by typing;

luarocks install luafilesystem

Which should give you something similar to this;

c:\>luarocks install luafilesystem Installing http://luarocks.org/repositories/rocks/luafilesystem-1.6.2-1.src.rock... Using http://luarocks.org/repositories/rocks/luafilesystem-1.6.2-1.src.rock... switching to 'build' mode mingw32-gcc -O2 -c -o src/lfs.o -IC:/Program Files/Lua/5.1/include/ src/lfs.c mingw32-gcc -shared -o lfs.dll src/lfs.o C:/Program Files/Lua/5.1/bin/lua51.dll -lm Updating manifest for C:\ProgramData\LuaRocks//lib/luarocks/rocks luafilesystem 1.6.2-1 is now built and installed in C:\ProgramData\LuaRocks/ c:\>

When completed you can type

lua -l lfs

to test whether Lua can load the compiled library properly (is doesn’t do anything, it should just start Lua and load the library without errors, press <ctrl>+<c> to exit the Lua interpreter). This should be the result;

c:\>lua -l lfs Lua 5.1.5 Copyright (C) 1994-2012 Lua.org, PUC-Rio > ^C c:\>

another common one, luasocket, can be installed with;

luarocks install luasocket

When completed you can type

lua -l socket

to test whether Lua can load the compiled library properly.

Congratulations again! you just setup a working package manager. This will allow you to install Lua modules with just a few simple commands, while not having to worry about the technical complexities of the compiler. Obviously only packages intended for Windows can be installed this way, explicit unix packages will not work.

[ad name=”468×60 Banner”]

From here;

available modules can be found in the main LuaRocks repository. The commands to start practicing with are ‘luarocks install <module name>’ and ‘luarocks search <part-of-name>’

The LuaRocks installer registered the ‘.rockspec’ extension with LuaRocks, so you can right-click a .rockspec file and choose the basic LuaRocks commands from the pop-up menu. This comes in handy when installing modules downloaded from the web, which are not in the LuaRocks repository.

The Lua scripts are simple text files, but probably best edited with an IDE. An excellent one is ZeroBrane Studio.

You probably want to associate the `.lua` extension with the Lua interpreter, so you can start scripts by double clicking them. If you use Lua scripts a lot from the command line you might want to add `.LUA` to the PATHEXT environment variable so you can ommit the extension. Eg. type `myscript` instead of `myscript.lua` to execute a Lua script.

Further resources for first time Lua users can be found here. And if you really want to get into Lua, then the book Programming in Lua (or PIL) is a must-have.

If you find any errrors omissions or run into trouble, drop me a message below.