Lua LuaRocks Guide — Package Management for Lua Projects
In this tutorial, you will learn about Lua LuaRocks Guide. We cover key concepts, practical examples, and best practices to help you master this topic.
LuaRocks is the official package manager for Lua -- handling installation, dependency resolution, and distribution of Lua modules (called "rocks") through a command-line interface and a public rock Repository.
What You'll Learn
- Installing LuaRocks on your system
- Finding and installing rocks
- Managing project dependencies with rockspec files
- Creating and distributing your own rocks
Why It Matters
Before LuaRocks, Lua dependency management was manual and error-prone. LuaRocks automates package installation, handles version dependencies, and provides a central repository. Durga Antivirus Pro uses LuaRocks to manage its Lua-based plugin dependencies across different deployment environments.
Installing LuaRocks
# Linux (Debian/Ubuntu)
sudo apt install luarocks
# macOS
brew install luarocks
# From source
wget https://luarocks.org/releases/luarocks-3.11.1.tar.gz
tar -xzf luarocks-3.11.1.tar.gz
cd luarocks-3.11.1
./configure --with-lua-include=/usr/include/lua5.4
make && sudo make install
# Verify
luarocks --version
Finding and Installing Rocks
# Search for packages
luarocks search luasocket
# Install a rock
luarocks install luasocket
# Install a specific version
luarocks install lpeg 1.1.0-1
# Install from local file
luarocks install my-rock-1.0-1.rockspec
# List installed rocks
luarocks list
# Show rock info
luarocks show luasocket
Using Installed Rocks
-- After installing luasocket
local socket = require("socket")
local http = require("socket.http")
local response, status = http.request("http://example.com")
if status == 200 then
print("Downloaded " .. #response .. " bytes")
end
Project Dependencies with Rockspec
-- myproject-1.0-1.rockspec
package = "myproject"
version = "1.0-1"
source = {
url = "git://github.com/user/myproject",
tag = "v1.0"
}
description = {
summary = "My awesome Lua project",
detailed = [[A longer description]],
license = "MIT"
}
dependencies = {
"lua >= 5.4",
"luasocket >= 3.0",
"lpeg >= 1.0"
}
build = {
type = "builtin",
modules = {
myproject = "src/myproject.lua",
["myproject.util"] = "src/util.lua"
}
}
Common Commands
# Create a new rock from current directory
luarocks make
# Generate a rockspec template
luarocks newrock
# Remove a rock
luarocks remove luasocket
# Show dependency tree
luarocks show --tree luasocket
# Update all rocks
luarocks install --upgrade luasocket
# Install for a specific Lua version
luarocks --lua-version 5.1 install luasocket
Common Mistakes
1. Mixing Lua and LuaJIT rocks
Rocks compiled for LuaJIT may not work with standard Lua and vice versa. Check compatibility.
2. Forgetting to install into the correct Lua version
Use luarocks --lua-version 5.4 install to target a specific version.
3. Not using a project-specific rock tree
Without luarocks --tree, rocks install globally. Use luarocks --tree=./lua_modules install for project isolation.
4. Ignoring dependency versions
Not specifying versions in rockspec leads to breaking changes. Pin dependencies.
Practice Questions
1. What is a rock in LuaRocks? A rock is a Lua module packaged with metadata (version, dependencies, build instructions) in a rockspec file.
2. How do you install LuaSocket using LuaRocks?
luarocks install luasocket.
3. How do you isolate project dependencies?
Use luarocks --tree=./lua_modules install <rock> to install into a project-local directory.
FAQ
{{< faq question="Is LuaRocks like pip or npm?" >}} Yes. It's the standard package manager for Lua, similar to pip (Python) or npm (JavaScript). It was created in 2007 and is widely used. {{< /faq >}}
{{< faq question="Can I use LuaRocks without internet access?" >}}
Yes. Download a rock's source and build it locally: luarocks install myrock-1.0-1.src.rock. Or set up a local rock server.
{{< /faq >}}
{{< faq question="Does LuaRocks work with LuaJIT?" >}}
Yes. LuaRocks supports LuaJIT. Use luarocks --lua-version 5.1 install for LuaJIT compatibility (LuaJIT targets Lua 5.1).
{{< /faq >}}
{{< faq question="How do I uninstall a rock?" >}}
luarocks remove <rockname>. Add --force to remove even if other rocks depend on it (not recommended).
{{< /faq >}}
{{< faq question="Can I create private rocks?" >}}
Yes. You can run your own rocks server using luarocks-admin or serve rockspec files from a private git repository.
{{< /faq >}}
What's Next
Now that you understand LuaRocks, learn about Lua strings and pattern matching.
| Topic | Description | Link |
|---|---|---|
| Strings | String manipulation and patterns | {{< ref "14-strings" >}} |
| Numbers | Arithmetic and math library | {{< ref "15-numbers" >}} |
| Python | Compare with Python's pip | Python |
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro