You add require "resty.some_library" to your project but get module 'resty.some_library' not found. To install Lua libraries in OpenResty successfully, you must handle two separate tasks: put the library on disk, then make the running service search that directory. Installing a library and making the service find it are two different things, and you need to verify each one.

The earlier Lua module tutorial showed how to write your own module. This article uses a real third-party library to walk through the complete process: check whether it ships with OpenResty, install it, configure the search path, and load it in the service. We use lua-resty-template, which renders HTML templates and documents both OPM and LuaRocks installation. The example renders one inline template and requires no external service.

Quick Answer

To install a Lua library in OpenResty, first check whether OpenResty already bundles it. For a project-local OPM package, install it under resty_modules/, add $prefix/resty_modules/lualib/?.lua;; to lua_package_path, and verify require through a request to the actual OpenResty service. If the library is not current on OPM, follow its maintainer’s installation guidance and use LuaRocks or a pinned vendored copy instead.

Does OpenResty Already Include the Lua Library?

OpenResty distributions already provide many common libraries. For example, lua-resty-redis can normally be loaded with require "resty.redis". Before installing a library, check the OpenResty component list and the lualib/ directory of your installation, and don’t install over the bundled version in a system-wide directory. For the distinction between bundled and user-installed library directories, see OpenResty Components: Modules, Lua Libraries, and How to Check Them.

lua-resty-template is not included in the OpenResty distribution and must be installed separately. Before you install it, read the project’s README: Which installation method does it recommend? Does it support your OpenResty and LuaJIT versions? Does it depend on other Lua libraries, system shared libraries, or external services? lua-resty-* is only a naming convention; the name alone does not guarantee that every operation is nonblocking. For network and disk I/O in particular, inspect which APIs the library actually calls. To learn why request-processing code must avoid blocking I/O, see the section “Why Must OpenResty Code Avoid Blocking Standard-Library I/O During Request Processing?” in Lua for OpenResty: Essential Syntax, Error Handling, and I/O Pitfalls.

Install a Lua Library in OpenResty with OPM

This example assumes that OpenResty and the opm command are installed. If opm is missing, official prebuilt Linux packages normally provide it in a separate openresty-opm package. See the optional-tools section of Install OpenResty - Complete Setup Guide for All Platforms for package names and instructions.

Create a separate project directory, then run OPM from that directory:

mkdir -p third-party-demo/conf third-party-demo/logs
cd third-party-demo
opm --cwd get bungle/lua-resty-template=2.0
opm --cwd list

--cwd installs the package under the current project’s resty_modules/ directory instead of OpenResty’s system-wide site/lualib/. You should now find resty_modules/lualib/resty/template.lua, the file later loaded by require "resty.template". To confirm a package’s publisher and version, check its OPM package page and source repository; don’t install a package just because its name looks right.

With a system-wide opm get, the library normally goes into site/lualib/ under the OpenResty installation tree. This example uses a project-local installation so the example project explicitly carries its dependency. The =2.0 suffix pins the version; opm --cwd list shows the installed version. When deploying to another machine, run the same version-pinned command in your build or release process, or deploy resty_modules/ with the application.

Configure the Path So the Service Can Load the Library

Put this minimal configuration in conf/nginx.conf:

worker_processes 1;

events {
    worker_connections 1024;
}

http {
    lua_package_path "$prefix/resty_modules/lualib/?.lua;;";

    server {
        listen 127.0.0.1:18080;

        location = /template {
            default_type text/html;

            content_by_lua_block {
                local template = require "resty.template"
                local html = template.process_string(
                    "<h1>{{message}}</h1>",
                    { message = "Hello from a third-party library" }
                )
                ngx.say(html)
            }
        }
    }
}

From the project directory, check the configuration, start the service, and send a request:

openresty -p "$PWD/" -c conf/nginx.conf -t
openresty -p "$PWD/" -c conf/nginx.conf
curl http://127.0.0.1:18080/template

The expected response is:

<h1>Hello from a third-party library</h1>

After testing, stop the example instance with the same prefix and configuration:

openresty -p "$PWD/" -c conf/nginx.conf -s stop

The path rules are straightforward: require "resty.template" looks for resty/template.lua; ? is the placeholder for the module’s file path; and $prefix is the service prefix supplied with -p. The trailing ;; preserves the existing search paths so distribution-provided libraries remain available. The OPM documentation uses the same project-local installation and path configuration. OPM currently distributes pure-Lua libraries only. Libraries with C modules installed by LuaRocks or another method also require lua_package_cpath, as discussed below. Because this path is in the http {} block, it applies to the service started with this configuration. A successful load from the resty command line is no substitute for the HTTP request test above.

Why Does OpenResty Say module not found?

First inspect the project’s logs/error.log and the candidate no file paths listed in the error. Then check the following in order:

  1. Does the file actually exist? Look for resty_modules/lualib/resty/template.lua. If it is missing, review the installation output and opm --cwd list, and confirm which directory you were in when running opm --cwd. If the library ships as LuaJIT bytecode (.ljbc files), add the .ljbc path to lua_package_path before the .lua path, for example "$prefix/resty_modules/lualib/?.ljbc;$prefix/resty_modules/lualib/?.lua;;". See LuaJIT Bytecode in OpenResty - How to Precompile Lua Modules for details.
  2. Which prefix does the service use? In this example, $prefix comes from openresty -p "$PWD/"; it is not the conf/ directory. For a production service, also confirm which binary, -p argument, and configuration file it actually runs with.
  3. Was the search path overwritten? Your lua_package_path must include the installation directory and use ;; to preserve existing paths. If the error refers to a C extension such as a .so file, check lua_package_cpath as well; a .lua path cannot resolve a C module.
  4. Did you test only from the command line? resty does not read your nginx.conf and may use different paths than the running Nginx instance. To load a project-local package on the command line, add the directory explicitly with -I, for example resty -I ./resty_modules/lualib -e 'require "resty.template"'. The final verification must still be a real request to the target service and inspection of its error.log.

If the error says a dependency inside the library is missing, a shared library failed to load, or an API is not available in the current execution phase, you are no longer dealing with a missing top-level module. Follow the error message to check dependencies or the execution phase rather than appending the same path again and again.

Should You Use OPM, LuaRocks, or Vendored Source?

Choose an installation method based on where the library’s maintainer publishes current releases. OPM is OpenResty’s package manager, but not every OPM package is updated in step with its source repository. Before installation, compare the project’s README, OPM page, and release dates. A real-world example is the current lua-resty-http README: the maintainer explains that its OPM package can no longer be updated and recommends LuaRocks. That is why this article does not use lua-resty-http as its example: its outdated OPM package cannot demonstrate installing the latest release.

MethodAppropriate whenAlso verify
OPMThe maintainer publishes the required version and dependencies on OPMPublisher, version, installation location, and lua_package_path
LuaRocksThe maintainer primarily publishes the required version through LuaRocksWhether LuaRocks targets OpenResty’s LuaJIT, whether the library uses nonblocking I/O, and build dependencies for C modules
Vendored, pinned sourceNo package repository has the required version, or the project needs to review and pin sourceTag/commit, the directory that actually contains the .lua files, and whether all dependencies are also included

Installing with LuaRocks

LuaRocks must target the LuaJIT shipped with OpenResty, not another Lua installation on the system. Otherwise, luarocks install can report success while installing the package into another Lua tree that the running OpenResty service cannot find. When building LuaRocks from source, point configure at LuaJIT under the OpenResty installation. With the default /usr/local/openresty prefix:

./configure --prefix=/usr/local/openresty/luajit \
    --with-lua=/usr/local/openresty/luajit \
    --with-lua-include=/usr/local/openresty/luajit/include/luajit-2.1
make build && make install

The official OpenResty Docker images use this configuration. If you deploy with Docker, variants such as alpine-fat already include a matching LuaRocks executable at /usr/local/openresty/luajit/bin/luarocks. Packages installed in the default location this way fall within OpenResty’s default search path and need no additional configuration.

Like opm --cwd, LuaRocks can install into a project directory with --tree. Pin the version so every deployment installs the same release:

/usr/local/openresty/luajit/bin/luarocks --tree ./lua_modules install lua-resty-template 2.0-1

Then add the directory’s Lua and C module paths to the configuration:

lua_package_path  "$prefix/lua_modules/share/lua/5.1/?.lua;$prefix/lua_modules/share/lua/5.1/?/init.lua;;";
lua_package_cpath "$prefix/lua_modules/lib/lua/5.1/?.so;;";

Packages with C modules also require a compiler toolchain. Libraries using OpenSSL must be compatible with the OpenSSL version bundled with OpenResty. More importantly, many LuaRocks libraries target ordinary Lua environments. OpenResty’s LuaRocks guidance warns that such libraries may block the OpenResty event loop. Before installing one, confirm that its network I/O uses cosockets or another nonblocking API.

Vendoring a Pinned Source Version

Most lua-resty-* libraries are pure Lua: just a handful of .lua files. Obtain a specific tag or commit through a Git submodule or release archive, then add the repository directory containing the library files to lua_package_path. If the files are under lib/resty/, add the repository’s lib/ directory so require "resty.xxx" can load them. This method does not depend on a package repository, gives you full control over which version you run, and makes source review easy. The tradeoff is that you must manage every dependency and vendor any other Lua libraries it requires.

A Final Check Before You Use the Library

A successful require proves only that the module can be loaded, not that it is suitable for request processing. Before production deployment, verify version compatibility, reproducible dependencies, and nonblocking I/O on request paths. A later cosocket article explains why blocking calls stall a worker. If an open-source library hits performance or connection-count limits under a particular workload, you can also look at the equivalent implementations in the OpenResty private libraries.

Next Steps

This article is part of the OpenResty tutorial series. You can now install a third-party library in a project and verify that the actual service can find it. Next, study request processing and execution phases to understand how these libraries run inside OpenResty.

If you like this tutorial series, subscribe to this blog and our YouTube channel.

About the Author

Yichun Zhang (GitHub handle: agentzh) is the original creator of the OpenResty® open-source project and the CEO of OpenResty Inc..

Yichun is a pioneer of edge computing, dynamic tracing, and machine coding. He has more than 22 years of programming experience and 16 years of open-source experience, and has contributed more than a million lines of code to projects including the Linux kernel, Nginx, LuaJIT, GDB, SystemTap, LLVM, and Perl. He has also authored more than 60 open-source software libraries.