luarocks
Table of Contents
1 概述
luarocks 是 lua 的包管理库。类似与 python 的 pip。
2 安装
直接通过系统包管理工具进行安装:
apt install luarocks
3 使用
有如下命令行参数
luarocks --help
LuaRocks 2.4.2, a module deployment system for Lua
NAME
/usr/bin/luarocks - LuaRocks main command-line interface
SYNOPSIS
/usr/bin/luarocks [--from=<server> | --only-from=<server>] [--to=<tree>] [VAR=VALUE]... <command> [<argument>]
GENERAL OPTIONS
These apply to all commands, as appropriate:
--server=<server> Fetch rocks/rockspecs from this server
(takes priority over config file)
--only-server=<server> Fetch rocks/rockspecs from this server only
(overrides any entries in the config file)
--only-sources=<url> Restrict downloads to paths matching the
given URL.
--tree=<tree> Which tree to operate on.
--local Use the tree in the user's home directory.
To enable it, see '/usr/bin/luarocks help path'.
--verbose Display verbose output of commands executed.
--timeout=<seconds> Timeout on network operations, in seconds.
0 means no timeout (wait forever).
Default is 30.
VARIABLES
Variables from the "variables" table of the configuration file
can be overriden with VAR=VALUE assignments.
COMMANDS
build
Build/compile a rock.
config
Query information about the LuaRocks configuration.
doc
Show documentation for an installed rock.
download
Download a specific rock file from a rocks server.
help
Help on commands. Type '/usr/bin/luarocks help <command>' for more.
install
Install a rock.
lint
Check syntax of a rockspec.
list
List currently installed rocks.
make
Compile package in current directory using a rockspec.
new_version
Auto-write a rockspec for a new version of a rock.
pack
Create a rock, packing sources or binaries.
path
Return the currently configured package path.
purge
Remove all installed rocks from a tree.
remove
Uninstall a rock.
search
Query the LuaRocks servers.
show
Show information about an installed rock.
unpack
Unpack the contents of a rock.
upload
Upload a rockspec to the public rocks repository.
write_rockspec
Write a template for a rockspec file.
CONFIGURATION
Lua version: 5.1
Configuration files:
System: /etc/luarocks/config.lua (ok)
User : /home/lsl/.luarocks/config-5.1.lua (not found)
Rocks trees in use:
/home/lsl/.luarocks
/usr/local
luarocks 默认安装在 /usr/ 目录下,但很多时候由于权限原因,需要另外指定安装位置,可通过全局参数 --tree 做到这点。
4 使用举例
4.1 显示子命令帮助
进一步显示 lua 子命令的参数和用法:
luarocks help <command>
4.2 查找包
luarocks search json
可以看到有各种版本的 json 实现。
4.3 安装包
luarocks intall lua-cjson
会安装 lua-cjson 到默认目录。
可以使用 =--local= 安装到用户目录,这样没有 root 权限的额问题。
4.4 查看安装路径
luarocks path
export LUA_PATH='/home/lsl/.luarocks/share/lua/5.1/?.lua;/home/lsl/.luarocks/share/lua/5.1/?/init.lua;/usr/local/share/lua/5.1/?.lua;/usr/local/share/lua/5.1/?/init.lua;./?.lua;/usr/local/lib/lua/5.1/?.lua;/usr/local/lib/lua/5.1/?/init.lua;/usr/share/lua/5.1/?.lua;/usr/share/lua/5.1/?/init.lua' export LUA_CPATH='/home/lsl/.luarocks/lib/lua/5.1/?.so;/usr/local/lib/lua/5.1/?.so;./?.so;/usr/lib/x86_64-linux-gnu/lua/5.1/?.so;/usr/lib/lua/5.1/?.so;/usr/local/lib/lua/5.1/loadall.so'
打印结果可以直接在 shell 中执行以修改 LUA_PATH 和 LUA_CPATH 两个环境变量。
如果不想修改环境变量,还可以只打印路径:
luarocks path --lr-path
/home/lsl/.luarocks/share/lua/5.1/?.lua;/home/lsl/.luarocks/share/lua/5.1/?/init.lua;/usr/local/share/lua/5.1/?.lua;/usr/local/share/lua/5.1/?/init.lua
然后添加到代码文件中的 luapath 和 luacpath 后就可以使用 require 进行调用。
4.5 查看安装后信息
luarocks show lua-cjson
安装完成以后,这样查看安装了哪些文件以及位置。
4.6 查看包帮助文档
luarocks doc lua-cjson
该命令会在浏览器中打开包的帮助文档。
4.7 查看已安装的包
luarocks list
会打印已经安装的所有包。使用 --outdated 仅打印可升级的软件包。
4.8 删除所有安装包
luarocks --tree /home/test purge
该命令中, --tree 是必需的,luarocks 不会默认指定要删除的位置。
5 制作 package
主要介绍如何安装,如何参考Documentation。