Lua Rocks Config

lua-users home
wiki

An example LuaRocks configuration to serve multiple Lua installations each with their own rocks from a single LuaRocks installation.

Assuming /usr/local/bin/luarocks is luarocks-2.0.12 or newer and /usr/local/opt/lua/bin/lua is Lua 5.1.x, create a default /usr/local/share/luarocks/config-5.1.lua (these are the default package directories on Mountain Lion with Homebrew, but I also have something very similar working on an Ubuntu continuous integration server -- no doubt something similar can be done with BAT files on Windows):

rocks_trees = { "/usr/local/lib/luarocks/5.1" }
variables = {
   LUA = "/usr/local/opt/lua/bin/lua5.1",
   LUA_BINDIR = "/usr/local/opt/lua/bin",
   LUA_INCDIR = "/usr/local/opt/lua/include",
   LUA_LIBDIR = "/usr/local/opt/lua/lib",
}

And then make a wrapper script at /usr/local/bin/luarocks5.1:

#!/bin/sh

PATH=/usr/local/opt/lua/bin:/usr/local/opt/luarocks/bin:$PATH
LUAROCKS_CONFIG=/usr/local/share/luarocks/config-5.1.lua
export LUAROCKS_CONFIG

lua5.1 `which luarocks` ${1+"$@"}
exit $?

Similarly, you need to add another wrapper at /usr/local/bin/lua5.1 to set the paths to find the right rocks:

#!/bin/sh

: ${LUA_PATH=";"}
: ${LUA_CPATH=";"}

PATH=/usr/local/opt/lua/bin:/usr/local/opt/luarocks/bin:$PATH
LUAROCKS_CONFIG=/usr/local/share/luarocks/config-5.1.lua
export LUAROCKS_CONFIG

# Set search paths to user environment settings, followed by system path, and finally rocks_trees.
save_LUA_PATH=$LUA_PATH
save_LUA_CPATH=$LUA_PATH
eval `luarocks path`
LUA_PATH="$save_LUA_PATH;$LUA_PATH"
LUA_CPATH="$save_LUA_CPATH;$LUA_CPATH"

exec lua5.1 ${1+"$@"}

After installing Lua 5.2.x to /usr/local/opt/lua52/ (again, the default for homebrew on Mountain Lion, although some of the paths from brew lua52 clash with brew lua, so you have to do the linking manually), you'll need a new /usr/local/share/luarocks/config-5.2.lua:

rocks_trees = { "/usr/local/lib/luarocks/5.2" }
variables = {
   LUA = "/usr/local/opt/lua52/bin/lua",
   LUA_BINDIR = "/usr/local/opt/lua52/bin",
   LUA_INCDIR = "/usr/local/opt/lua52/include",
   LUA_LIBDIR = "/usr/local/opt/lua52/lib",
}

And a wrapper script at /usr/local/bin/luarocks5.2:

#!/bin/sh

PATH=/usr/local/opt/lua52/bin:/usr/local/opt/luarocks/bin:$PATH
LUAROCKS_CONFIG=/usr/local/share/luarocks/config-5.2.lua
export LUAROCKS_CONFIG

exec lua5.2 `which luarocks` ${1+"$@"}

And another at /usr/local/bin/lua5.2:

#!/bin/sh

: ${LUA_PATH=";"}
: ${LUA_CPATH=";"}

PATH=/usr/local/opt/lua52/bin:/usr/local/opt/luarocks/bin:$PATH
LUAROCKS_CONFIG=/usr/local/share/luarocks/config-5.2.lua
export LUAROCKS_CONFIG

# Set search paths to user environment settings, followed by system path, and finally rocks_trees.
save_LUA_PATH=$LUA_PATH
save_LUA_CPATH=$LUA_PATH
eval `luarocks path`
LUA_PATH="$save_LUA_PATH;$LUA_PATH"
LUA_CPATH="$save_LUA_CPATH;$LUA_CPATH"

exec lua5.2 ${1+"$@"}

LuaRocks 2.0.12 also works with luajit-2.0, so I have a third configuration using /usr/local/share/luarocks/config-luajit.lua and a pair of wrappers at /usr/local/bin/luarocks-luajit and /usr/local/bin/luajit. Adapting to different operating systems (such as Ubuntu, which installs the packages into the /usr/ tree), is a simple matter of adjusting the PATHs of the scripts, and in the scripts. For good measure, I've also made a soft-links to my preferred default interpreter:

$ ln -s lua5.2 /usr/local/bin/lua
$ ln -s luarocks5.2 /usr/local/bin/luarocks

At this point you can install and query to the appropriate LuaRocks tree for automatic discovery by the associated Lua interpreter, simply by choosing whether to use luarocks5.1 or luarocks-luajit etc.


RecentChanges · preferences
edit · history
Last edited June 28, 2013 9:50 am GMT (diff)