包管理 #
一、LuaRocks 简介 #
1.1 什么是 LuaRocks #
LuaRocks 是 Lua 的官方包管理器,类似于 Python 的 pip、Node.js 的 npm。
1.2 主要功能 #
- 安装、卸载 Lua 包
- 管理包依赖
- 创建和发布包
- 多版本管理
二、安装 LuaRocks #
2.1 Windows #
powershell
# 使用 Scoop
scoop install luarocks
# 使用 Chocolatey
choco install luarocks
# 手动安装
# 下载安装包:https://luarocks.org/
2.2 macOS #
bash
# 使用 Homebrew
brew install luarocks
# 验证安装
luarocks --version
2.3 Linux #
bash
# Ubuntu/Debian
sudo apt install luarocks
# Fedora
sudo dnf install luarocks
# Arch Linux
sudo pacman -S luarocks
# 从源码安装
wget https://luarocks.org/releases/luarocks-3.9.2.tar.gz
tar xzf luarocks-3.9.2.tar.gz
cd luarocks-3.9.2
./configure && make && sudo make install
三、基本使用 #
3.1 搜索包 #
bash
# 搜索包
luarocks search json
# 搜索特定版本
luarocks search dkjson 3.0
3.2 安装包 #
bash
# 安装最新版本
luarocks install dkjson
# 安装特定版本
luarocks install dkjson 2.5
# 安装到本地
luarocks install --local dkjson
# 安装到指定目录
luarocks install --tree=./libs dkjson
3.3 卸载包 #
bash
# 卸载包
luarocks remove dkjson
# 卸载特定版本
luarocks remove dkjson 2.5
3.4 列出已安装的包 #
bash
# 列出所有已安装的包
luarocks list
# 显示包详情
luarocks show dkjson
3.5 更新包 #
bash
# 更新包
luarocks install dkjson
# 更新 LuaRocks 自身
luarocks install luarocks
四、配置 #
4.1 配置文件 #
bash
# 查看配置
luarocks config
# 设置配置
luarocks config variables.LUA_DIR /usr/local
# 常用配置文件位置
# Unix: ~/.luarocks/config-5.4.lua
# Windows: %APPDATA%/luarocks/config-5.4.lua
4.2 配置镜像 #
lua
-- ~/.luarocks/config-5.4.lua
rocks_servers = {
"https://luarocks.org",
"https://raw.githubusercontent.com/rocks-moonscript-org/moonrocks-mirror/master/"
}
4.3 本地配置 #
bash
# 设置本地安装路径
luarocks config --local
# 查看路径
luarocks path
五、创建包 #
5.1 rockspec 文件 #
lua
-- mymodule-1.0-1.rockspec
package = "mymodule"
version = "1.0-1"
source = {
url = "git://github.com/user/mymodule.git",
tag = "v1.0"
}
description = {
summary = "My Lua Module",
detailed = "A detailed description of my module.",
homepage = "https://github.com/user/mymodule",
license = "MIT"
}
dependencies = {
"lua >= 5.1",
"dkjson >= 2.5"
}
build = {
type = "builtin",
modules = {
mymodule = "mymodule.lua"
}
}
5.2 构建和安装 #
bash
# 从 rockspec 构建
luarocks build mymodule-1.0-1.rockspec
# 从当前目录构建
luarocks make
# 打包
luarocks pack mymodule 1.0-1
5.3 发布包 #
bash
# 上传到 LuaRocks
luarocks upload mymodule-1.0-1.rockspec --api-key=YOUR_API_KEY
六、常用库 #
6.1 JSON 处理 #
bash
# 安装
luarocks install dkjson
luarocks install cjson
luarocks install rapidjson
lua
-- dkjson 使用
local json = require("dkjson")
local data = {
name = "Lua",
version = 5.4
}
local str = json.encode(data)
print(str) -- {"name":"Lua","version":5.4}
local obj = json.decode(str)
print(obj.name) -- Lua
6.2 HTTP 客户端 #
bash
# 安装
luarocks install lua-requests
luarocks install luasocket
luarocks install luasec
lua
-- luasocket 使用
local http = require("socket.http")
local body, code = http.request("https://api.github.com")
print(code, body)
6.3 数据库 #
bash
# LuaSQL
luarocks install luasql-sqlite3
luarocks install luasql-mysql
luarocks install luasql-postgres
lua
-- LuaSQL SQLite 使用
local sqlite3 = require("luasql.sqlite3")
local env = sqlite3.sqlite3()
local conn = env:connect("test.db")
conn:execute[[
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY,
name TEXT
)
]]
conn:execute("INSERT INTO users (name) VALUES ('Alice')")
6.4 测试框架 #
bash
# 安装
luarocks install busted
luarocks install luassert
lua
-- busted 测试示例
-- test_mymodule_spec.lua
local mymodule = require("mymodule")
describe("mymodule", function()
it("should add numbers", function()
assert.are.equal(5, mymodule.add(2, 3))
end)
end)
6.5 其他常用库 #
bash
# 日期时间
luarocks install luadate
# 字符串处理
luarocks install penlight
# 文件系统
luarocks install luafilesystem
# 命令行参数
luarocks install argparse
# 日志
luarocks install lua-log
七、项目依赖管理 #
7.1 依赖文件 #
lua
-- myproject-0.1-1.rockspec
package = "myproject"
version = "0.1-1"
dependencies = {
"lua >= 5.1",
"dkjson >= 2.5",
"luasocket >= 3.0",
"penlight >= 1.0"
}
7.2 安装项目依赖 #
bash
# 安装所有依赖
luarocks install myproject-0.1-1.rockspec --only-deps
八、多版本管理 #
8.1 使用不同版本 #
bash
# 安装多个版本
luarocks install dkjson 2.5
luarocks install dkjson 3.0
# 切换版本
luarocks remove dkjson 2.5
8.2 使用 hererocks #
bash
# 安装 hererocks
pip install hererocks
# 创建隔离环境
hererocks lua_env -l5.4 -r3.9
source lua_env/bin/activate
# 在环境中安装包
luarocks install dkjson
九、常见问题 #
9.1 权限问题 #
bash
# 使用 --local 安装到用户目录
luarocks install --local dkjson
# 或设置权限
sudo luarocks install dkjson
9.2 找不到模块 #
bash
# 检查路径
luarocks path
# 添加到环境变量
eval $(luarocks path)
9.3 编译错误 #
bash
# 安装编译依赖
sudo apt install build-essential
# 指定 Lua 路径
luarocks install dkjson --lua-dir=/usr/local
十、总结 #
本章介绍了 Lua 包管理:
- LuaRocks:Lua 官方包管理器
- 基本使用:搜索、安装、卸载、列出
- 配置:镜像、路径设置
- 创建包:rockspec 文件
- 常用库:JSON、HTTP、数据库、测试
- 依赖管理:项目依赖文件
- 多版本管理:hererocks 工具
下一章,我们将学习文件操作。
最后更新:2026-03-27