You are currently browsing the tag archive for the ‘LuaRocks’ tag.

LuaRocks is a package manager for Lua (which is a really cool light-weight scripting language suitable for embedding into applications. But that’s a topic for another post.)

On Windows computers, I like running Cygwin to get all the usual Unix shell goodness. Cygwin comes with Lua (5.1, selectable from the installer), it does not come with LuaRocks.

To install LuaRocks, download and unpack:

$ wget http://luarocks.org/releases/luarocks-2.0.13.tar.gz
$ tar xfvz luarocks-2.0.13.tar.gz
$ cd luarocks-2.0.13

Then install, as per the instructions:

$ ./configure
$ make
$ make install

Now for the part that is not in the manual. If you install a rock:

$ luarocks install luasocket

and try to use it, you get an error:

$ lua -e "socket = require('socket')"
lua: (command line):1: module 'socket' not found:
no field package.preload['socket']
no file './socket.lua'
no file '/usr/share/lua/5.1/socket.lua'
no file '/usr/share/lua/5.1/socket/init.lua'
no file '/usr/lib/lua/5.1/socket.lua'
no file '/usr/lib/lua/5.1/socket/init.lua'
no file './socket.dll'
no file '/usr/lib/lua/5.1/socket.dll'
no file '/usr/lib/lua/5.1/loadall.dll'
stack traceback:
[C]: in function 'require'
(command line):1: in main chunk
[C]: ?

The problem is that Lua is unable to find the rocks because it does not know the path to them. In order to fix this, you have to set the LUA_PATH and LUA_CPATH environment variables.

Simple Solution

Append the following to your ~/.bashrc:

export LUA_CPATH="./?.dll;/usr/lib/lua/5.1/?.dll;/usr/lib/lua/5.1/loadall.dll;/usr/local/lib/lua/5.1/?/core.so"
export LUA_PATH="./?.lua;/usr/share/lua/5.1/?.lua;/usr/share/lua/5.1/?/init.lua;/usr/lib/lua/5.1/?.lua;/usr/lib/lua/5.1/?/init.lua;/usr/local/share/lua/5.1/?.lua"

Start a new bash and give it a whirl.

If you are not using bash, add it to whatever configuration file your favorite shell provides.

Advanced Solution

If your installation is not exactly like my current installation (and it won’t be as soon as Cygwin gets Lua 5.2, for example), you should figure out your current paths first:

$ lua -e "print(package.path)"
./?.lua;/usr/share/lua/5.1/?.lua;/usr/share/lua/5.1/?/init.lua;/usr/lib/lua/5.1/?.lua;/usr/lib/lua/5.1/?/init.lua

With the default settings, LuaRocks will install Lua modules into /usr/local/share/lua/x.x. So the LUA_PATH is constructed from the default package.path and the LuaRocks path:

$ export LUA_PATH="./?.lua;/usr/share/lua/5.1/?.lua;/usr/share/lua/5.1/?/init.lua;/usr/lib/lua/5.1/?.lua;/usr/lib/lua/5.1/?/init.lua;/usr/local/share/lua/5.1/?.lua"

The same goes for the LUA_CPATH, containing the path to C libraries. LuaRocks stores these in /usr/local/lib/lua/x.x/<modulename>/core.so so that is what we add to the path:

$ lua -e "print(package.cpath)"
./?.dll;/usr/lib/lua/5.1/?.dll;/usr/lib/lua/5.1/loadall.dll
$ export LUA_CPATH="./?.dll;/usr/lib/lua/5.1/?.dll;/usr/lib/lua/5.1/loadall.dll;/usr/local/lib/lua/5.1/?/core.so"

That’s it. Again, I recommend adding this to your ~/.bashrc.

Update 20-AUG-2013:


Issuing the command

$ luarocks path

outputs both LUA_PATH and LUA_CPATH, so it’s a bit easier than printing package.path/package.cpath. Thanks, moon, for the advice!