Skip to content
AgileLogics
4 min read

LuaRocks and Project Structure

Prerequisites: Modules and require

LuaRocks is Lua's package manager. Packages are called rocks.

luarocks install dkjson              # a pure-Lua JSON library
luarocks install luafilesystem       # a C module (compiled on install)
luarocks install busted              # testing framework
luarocks list                        # installed rocks
luarocks install --local penlight    # install for your user only

Useful rocks: penlight (utility functions), luafilesystem (directories), luasocket and lua-http (networking), lpeg (parsing), argparse (command-line arguments), inspect (pretty printing tables).

A project layout

my-lib/
├── my-lib-1.0.0-1.rockspec
├── src/
│   └── mylib/
│       ├── init.lua          # require("mylib")
│       └── strings.lua       # require("mylib.strings")
├── spec/
│   └── strings_spec.lua      # busted tests
├── .luacheckrc
└── README.md

A rockspec

-- illustrative
package = "my-lib"
version = "1.0.0-1"
source = {url = "git+https://github.com/example/my-lib.git", tag = "v1.0.0"}
description = {summary = "String helpers for Lua", license = "MIT"}
dependencies = {"lua >= 5.1"}
build = {
  type = "builtin",
  modules = {
    ["mylib"] = "src/mylib/init.lua",
    ["mylib.strings"] = "src/mylib/strings.lua",
  },
}

luarocks make installs the project locally; luarocks upload publishes it to luarocks.org.

Per-project dependencies

LuaRocks installs globally or per user by default. For per-project installs, use luarocks --tree lua_modules install <rock> and add lua_modules to package.path — or a tool like lux. Game and editor projects usually just copy single-file libraries into a lib/ folder.