There is an updated version of this post here
The older LuaforWindows distribution is getting obsolete quickly these days. Also because the distribution is aging, many “batteries” are out dated and they are not easily replaced as the package managers do not integrate with it. So I started an endavour to update my windows system with a clean installation (minimal batteries) and a proper package manager to be able to update and extend packages as needed.
So it wasn’t such a smooth experience as expected…. So this post is merely a reminder to myself on how to get this all set up and working properly.
There are 2 Lua package managers for Windows;
LuaDist seems to be the favourite as a successor for LuaforWindows, but I ran into trouble because its dependencies (CMake) interfered with my Git installation. Hence I picked LuaRocks. Despite my problems with LuaDist, LuaRocks is also the one with a larger selection of packages available. On the other hand LuaDist has tighter control over the packages, so is less likely to run into trouble.
Setting up LuaRocks
There are some dependencies, for c modules, a compiler is required to build them. And besides the packages themselves (rocks) I also need an editor, and my current favorite is ZeroBrane Studio. One big problem I ran into is that the defacto standard socket library for Lua (LuaSocket) does not compile with LuaRocks and the MinGW compiler. But a workaround is available.
The paths are assumed to be;
- “c:\MinGW” for the MinGW compiler
- “c:\users\public\Lua” for the Lua related stuff
- Download it as a windows installer package from its downloads page
- Start the installer and make sure to;
- install in “c:\MinGW” (for this tutorial this is used, anyway; do not use paths with spaces!)
- check “Download latest repository catalogs”
- check “C compiler” and “C++ compiler”
The main package required, install as follows;
- Download the windows package and pick the newest marked with “-win32.zip”
- extract it into a temporary directory eg. “c:\temp\LuaRocks”
- Open an elevated command prompt (in startmenu type “cmd” in the search box, then right-click and select “run as administrator”)
- cd c:\temp\LuaRocks
- install /p c:\users\public\lua\LuaRocks /MW
- copy C:\Users\Public\Lua\LuaRocks\2.0\lua5.1.exe C:\Users\Public\Lua\LuaRocks\2.0\lua.exe
The “install” command invokes the LuaRocks installer and installs it in the directory provided. The “/MW” parameter instructs it to use the MinGW compiler. The copy command last in the list makes sure Lua can be accessed as “lua.exe” instead of the more awkward “lua5.1.exe”.
The paths (both system paths and Lua paths) are required so the individual elements can all find the tools and libraries they need.
- Right click on “My Computer”, “Properties”, “Advanced system settings”, “Environment variables…”
- Lookup “PATH” in the bottom list, and append “;c:\mingw\bin;C:\Users\Public\Lua\LuaRocks\2.0;C:\Users\Public\Lua\LuaRocks\bin”
- The following two should be added as new enviornment variables;
- “LUA_PATH” with “.\?.lua;.\?\init.lua;C:\Users\Public\Lua\LuaRocks\share\lua\5.1\?.lua;C:\Users\Public\Lua\LuaRocks\share\lua\5.1\?\init.lua”
- “LUA_CPATH” with “.\?.dll;C:\Users\Public\Lua\LuaRocks\lib\lua\5.1\?.dll”
This is such a standard library that many packages require it. But as mentioned MinGW cannot build it. So here is the workaround;
- Download the binary LuaSocket package into “c:\temp”
- Open a command prompt (as administrator)
- cd c:\temp
- luarocks install luasocket-2.0.2-3.win32-x86.rock
This one is simple
- Download the ZIP archive from the ZeroBrane website
- unzip in “c:\users\public\Lua\ZeroBraneStudio”
- add shortcuts to ZeroBrane to the startmenu or the quick launch bar
Finalizing the installation
- configure extension “.lua” to open with either ZeroBrane Studio (edit as default action) or to “C:\Users\Public\Lua\LuaRocks\2.0\lua.exe” (run as default action)
- configure extension “.luac” to open with “C:\Users\Public\Lua\LuaRocks\2.0\lua.exe” (compiled Lua code, so can’t edit anyway)
- configure extension “.rockspec” (which is just a Lua file) to open with ZeroBrane Studio
- The LuaSocket implementation used is a binary rock, it has probably been compiled against VS2005, which may cause problems on Windows (DLL dependency hell). So far I haven’t seen any, but no guarantees…
- ZeroBrane also includes some packages, and when running from within the IDE, it might use those instead of the LuaRocks versions (type “print(package.path)” in the ZeroBrane console to see where it gets its modules)
LuaRocks is a command line tool, which remains slightly awkward on Windows. But it is easy to understand the basics. Simply type “luarocks” on the command line and it will provide some basic help.