How to Compile LuaRocks for OpenResty and Install RapidJSON
This guide explains what LuaRocks is, shows step‑by‑step commands to compile and install it with OpenResty, details configuration flags, resolves a common unzip error, and demonstrates installing and using the rapidjson Lua package.
LuaRocks is a popular Lua package manager that provides a command‑line interface for handling Lua dependencies, and it has more packages than the alternative LuaDist.
Compilation and Installation
wget https://github.com/luarocks/luarocks/archive/v3.0.0.tar.gz
tar zxvf v3.0.0.tar.gz
cd luarocks-3.0.0/
./configure --prefix=/usr/local/openresty/luajit \
--with-lua=/usr/local/openresty/luajit/ \
--lua-suffix=jit \
--with-lua-include=/usr/local/openresty/luajit/include/luajit-2.1
make build
# Installation requires root privileges
sudo make installInstallation parameter description
--prefixsets the LuaRocks installation directory. --with-lua points to the root directory of the installed Lua. --lua-suffix specifies the version suffix; for OpenResty’s LuaJIT this should be jit. --with-lua-include provides the directory containing Lua header files.
Check Version
luarocks --version
/usr/local/openresty/luajit/bin/luarocks 3.0.0
LuaRocks main command‑line interfaceTypical error output
Configuring LuaRocks...
Lua version detected: 5.1
Lua interpreter found: /usr/local/openresty/luajit/bin/luajit
lua.h found: /usr/local/openresty/luajit/include/luajit-2.1/lua.h
Could not find 'unzip'.
Make sure it is installed and available in your PATH.
configure failed.Solution
sudo apt install unzipInstalling Lua Packages
Run luarocks install package to install a Lua package, or use luarocks install package --tree=path to specify a custom installation directory.
Install rapidjson
RapidJSON is a well‑known open‑source C++ JSON library whose Java‑like API is easy to use; the author prefers a simpler wrapper that consolidates the many setXXX and getXXX functions into a single interface.
$ luarocks install rapidjson --tree=/usr/local/openresty/lualib/resty
Installing https://luarocks.org/rapidjson-0.5.1-1.src.rock
-- The C compiler identification is GNU 7.3.0
-- The CXX compiler identification is GNU 7.3.0
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info - done
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features - done
-- LUA_RAPIDJSON_VERSION: 0.5.1
-- Configuring done
-- Generating done
-- Build files have been written to: /tmp/luarocks_rapidjson-0.5.1-1-fd1Qr3/lua-rapidjson/build.luarocks
Scanning dependencies of target lua-rapidjson
[ 20%] Building CXX object CMakeFiles/lua-rapidjson.dir/src/Document.cpp.o
[ 40%] Building CXX object CMakeFiles/lua-rapidjson.dir/src/Schema.cpp.o
[ 60%] Building CXX object CMakeFiles/lua-rapidjson.dir/src/rapidjson.cpp.o
[ 80%] Building CXX object CMakeFiles/lua-rapidjson.dir/src/values.cpp.o
[100%] Linking CXX shared module rapidjson.so
[100%] Built target lua-rapidjson
Install the project...
-- Install configuration: "Release"
-- Installing: /usr/local/openresty/lualib/resty/lib/luarocks/rocks-5.1/rapidjson/0.5.1-1/lib/rapidjson.so
rapidjson 0.5.1-1 is now installed in /usr/local/openresty/lualib/resty (license: MIT)Installation Path
/usr/local/openresty/lualib/resty/lib/luarocks/rocks-5.1
ls
30log luasocket manifest rapidjsonUsage
local rapidjson = require('rapidjson')
rapidjson.encode()
rapidjson.decode()
rapidjson.load()
rapidjson.dump()Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
