Install luastatic
masterYou can install luastatic by running the luastatic.lua script directly or by installing it via LuaRocks.
To install via LuaRocks:
luarocks install luastaticluarocks install luastaticrepository·master·Indexed 21 days ago
https://github.com/ers35/luastaticA command line tool that builds standalone executables from Lua programs, allowing distribution as single binaries without a pre-installed Lua interpreter. It supports Lua 5.1, 5.2, 5.3, 5.4, and LuaJIT, and provides capabilities for embedding Lua files, including C libraries, cross-compiling for Windows, and static linking with musl libc.
You can install luastatic by running the luastatic.lua script directly or by installing it via LuaRocks.
To install via LuaRocks:
luarocks install luastaticluarocks install luastaticIf you installed Lua and LuaRocks via Homebrew, use brew --prefix to dynamically locate the correct library and include paths.
# Install Lua and LuaRocks from Homebrew.
brew install lua luarocks
# Install luastatic from LuaRocks.
luarocks install luastatic
# Build using the Homebrew installation path.
luastatic main.lua $(brew --prefix lua)/lib/liblua.a -I$(brew --prefix lua)/include/luabrew install lua luarocks
luarocks install luastatic
luastatic main.lua $(brew --prefix lua)/lib/liblua.a -I$(brew --prefix lua)/include/luaTo create a highly portable binary using musl-gcc and static linking, set the CC environment variable and pass the -static flag.
CC=musl-gcc luastatic main.lua /usr/lib/x86_64-linux-musl/liblua5.2.a -I/usr/include/lua5.2 -staticCC=musl-gcc luastatic main.lua /usr/lib/x86_64-linux-musl/liblua5.2.a -I/usr/include/lua5.2 -staticWhen using LuaJIT on macOS, you may need to specify -pagezero_size and -image_base for the linker.
luastatic main.lua /opt/local/lib/libluajit-5.1.a -I/opt/local/include/luajit-2.0 -pagezero_size 10000 -image_base 100000000luastatic main.lua /opt/local/lib/libluajit-5.1.a -I/opt/local/include/luajit-2.0 -pagezero_size 10000 -image_base 100000000To include a Lua binary module (a C library), pass the static library file to luastatic.
luastatic main.lua library.a /usr/lib/x86_64-linux-gnu/liblua5.2.a -I/usr/include/lua5.2luastatic main.lua library.a /usr/lib/x86_64-linux-gnu/liblua5.2.a -I/usr/include/lua5.2If you prefer to link against a shared Lua library instead of a static one, use the -l flag to pass linking instructions to the C compiler.
luastatic main.lua -llua5.2 -I/usr/include/lua5.2luastatic main.lua -llua5.2 -I/usr/include/lua5.2To build a standalone executable from a single Lua file, provide the entry point, the path to the Lua static library, and the include directory for lua.h.
luastatic main.lua /usr/lib/x86_64-linux-gnu/liblua5.2.a -I/usr/include/lua5.2luastatic main.lua /usr/lib/x86_64-linux-gnu/liblua5.2.a -I/usr/include/lua5.2When using LuaJIT on Ubuntu, you may need to pass specific flags like -no-pie to the compiler.
luastatic main.lua /usr/lib/x86_64-linux-gnu/libluajit-5.1.a -I/usr/include/luajit-2.0 -no-pieluastatic main.lua /usr/lib/x86_64-linux-gnu/libluajit-5.1.a -I/usr/include/luajit-2.0 -no-pieIf you want to see the generated C code without actually running the compiler, set the CC environment variable to an empty string.
CC="" luastatic main.luaCC="" luastatic main.luaYou can cross-compile for Windows by setting the CC environment variable to a MinGW compiler and providing the Windows-compatible Lua static library and include paths.
CC=x86_64-w64-mingw32-gcc luastatic main.lua /usr/x86_64-w64-mingw32/lib/liblua5.2.a -I/usr/x86_64-w64-mingw32/include/lua5.2/CC=x86_64-w64-mingw32-gcc luastatic main.lua /usr/x86_64-w64-mingw32/lib/liblua5.2.a -I/usr/x86_64-w64-mingw32/include/lua5.2/If your main script uses require("library"), you can embed library.lua directly into the executable so it is available at runtime.
luastatic main.lua library.lua /usr/lib/x86_64-linux-gnu/liblua5.2.a -I/usr/include/lua5.2luastatic main.lua library.lua /usr/lib/x86_64-linux-gnu/liblua5.2.a -I/usr/include/lua5.2The luastatic command line tool builds standalone executables from Lua programs. The resulting executable can run on systems that do not have Lua installed. It supports Lua 5.1, 5.2, 5.3, 5.4, and LuaJIT.
Command Syntax:
luastatic main.lua [required_lua_files] [lua_interpreter_static_lib] [binary_module_static_libs] -I/include/lua [additional_c_compiler_args]Arguments:
main.lua: The entry point to the Lua program.required.lua: One or more required Lua source files to be embedded.liblua.a: The path to the Lua interpreter static library.library.a: One or more static libraries for a required Lua binary module.-I/include/lua: The path to the directory containing lua.h.[additional_args]: Any additional arguments are passed directly to the C compiler.luastatic main.lua require.lua liblua.a library.a -I/include/lua