lua-nginx-module icon indicating copy to clipboard operation
lua-nginx-module copied to clipboard

LUA_PATH must contain /usr/share/lua and /usr/lib/x86_64-linux-gnu/lua/

Open yurtpage opened this issue 10 months ago • 0 comments

I have a script that require("socket.unix") and the library is installed in Ubuntu with apt install lua-socket.

But the OpenResty can't resolve the library:

 no file '/usr/local/lib/lua/5.1/socket.so'

This is because it's not it the /usr/local/lib/lua/ but in /usr/lib/x86_64-linux-gnu/lua/. But this will work if a library was installed with luarocks because it's indeed uses the /usr/local/lib/lua/.

I tried to solve this by adding lua_package_path and lua_package_cpath like this:

  # By default Lua libraies are looked in the /usr/local/share/lua/ and /usr/local/lib/lua/ but must look into /usr/share/lua/ and /usr/lib/x86_64-linux-gnu/lua/
  # set search paths for pure Lua external libraries (';;' is the default path):
  lua_package_path '/usr/share/lua/5.1/?.lua;;';
  # set search paths for Lua external libraries written in C (can also use ';;'):
  lua_package_cpath '/usr/lib/x86_64-linux-gnu/lua/5.1/?.so;;';

This partially solved a problem but failed with another error attempt to call method 'get_certificate' (a nil value). This is probably because packages has own directories that also needs to be added recursively. I had to add them manually like this:

  # By default Lua libraies are looked in the /usr/local/share/lua/ and /usr/local/lib/lua/ but must look into /usr/share/lua/ and /usr/lib/x86_64-linux-gnu/lua/
  # set search paths for pure Lua external libraries (';;' is the default path):
  lua_package_path '/usr/share/lua/5.1/?.lua;/usr/share/lua/5.1/socket/?.lua;/usr/share/lua/5.1/ssl/?.lua;;';
  # set search paths for Lua external libraries written in C (can also use ';;'):
  lua_package_cpath '/usr/lib/x86_64-linux-gnu/lua/5.1/?.so;/usr/lib/x86_64-linux-gnu/lua/5.1/socket/?.so;/usr/lib/x86_64-linux-gnu/lua/5.1/mime/?.so;;';

This again didn't worked, don't know why. Anyway this solution would be fragile. Once a new library with own folder installed it also should be manually added to the path.

The only solution that worked was to create symlinks:

ln -s /usr/share/lua /usr/local/share/
ln -s /usr/lib/x86_64-linux-gnu/lua/ /usr/local/lib/

This is not something that I like but solves the problem for now.

I wish to hear how to solve this correctly. At least please add more examples to the lua_package_path and lua_package_cpath.

yurtpage avatar Apr 10 '24 12:04 yurtpage