Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preferred Way to Configure Lua's Load Path

Tags:

lua

So I installed Lua 5.1.4 from the Centos 7 package archives and Luarocks 2.2.2 from their website. I tried installing luacurl via luarocks install luacurl. It splits files between /usr/local/lib/lua/5.1, /usr/local/lib/luarocks/rocks, and /usr/local/share/lua/5.1. I can't figure out what I'm supposed to add to my lua package path or how you are supposed to configure that.

> lua -e 'print(package.path)'

./?.lua;/usr/share/lua/5.1/?.lua;/usr/share/lua/5.1/?/init.lua;/usr/lib64/lua/5.1/?.lua;/usr/lib64/lua/5.1/?/init.lua

but when I try to require("luacurl"), I get the following error message

stdin:1: module 'luacurl' not found:
    no field package.preload['luacurl']
    no file './luacurl.lua'
    no file '/usr/share/lua/5.1/luacurl.lua'
    no file '/usr/share/lua/5.1/luacurl/init.lua'
    no file '/usr/lib64/lua/5.1/luacurl.lua'
    no file '/usr/lib64/lua/5.1/luacurl/init.lua'
    no file './luacurl.so'
    no file '/usr/lib64/lua/5.1/luacurl.so'
    no file '/usr/lib64/lua/5.1/loadall.so'

Is there a good reference on the standards that lua packages adhere to? Attempting to require an absolute path to the location where my luacurl.so file actually resides does not seem to work.

module '/usr/local/lib/lua/5.1/luacurl.so' not found:
    no field package.preload['/usr/local/lib/lua/5.1/luacurl.so']
    no file './/usr/local/lib/lua/5/1/luacurl/so.lua'
    no file '/usr/share/lua/5.1//usr/local/lib/lua/5/1/luacurl/so.lua'
    no file '/usr/share/lua/5.1//usr/local/lib/lua/5/1/luacurl/so/init.lua'
    no file '/usr/lib64/lua/5.1//usr/local/lib/lua/5/1/luacurl/so.lua'
    no file '/usr/lib64/lua/5.1//usr/local/lib/lua/5/1/luacurl/so/init.lua'
    no file './/usr/local/lib/lua/5/1/luacurl/so.so'
    no file '/usr/lib64/lua/5.1//usr/local/lib/lua/5/1/luacurl/so.so'
    no file '/usr/lib64/lua/5.1/loadall.so'
    no file './/usr/local/lib/lua/5.so'
    no file '/usr/lib64/lua/5.1//usr/local/lib/lua/5.so'
    no file '/usr/lib64/lua/5.1/loadall.so'

It is entirely possible that require is not the right function to use here.

like image 212
Gregory Nisbet Avatar asked Oct 15 '25 18:10

Gregory Nisbet


2 Answers

Luarocks installs itself in the default path. Before requiring any luarocks, do:

require 'luarocks.loader'

This will modify require to also look for and find luarocks in the place where luarocks have installed them.

If the Lua code should run on different systems, where some systems use luarocks and other systems have installed the libraries in the default search path, it may be necessary to do:

pcall( require, 'luarocks.loader' )

This will allow the require to silently fail when luarocks is not installed, and continue with just the default search path for require.

like image 70
Tommy Pettersson Avatar answered Oct 18 '25 00:10

Tommy Pettersson


There are 2 types of require paths in Lua: package.path is for pure lua libraries (.lua), and package.cpath is for compiled libraries (.so).

If you know where luacurl.so is located ($ whereis luacurl.so), then you can modify your cpath like this:

package.cpath = package.cpath .. ";/usr/local/lib/lua/5.1/?.so"

or wherever luacurl.so is placed. Just read the docs about the format (; is separator, ? is what you pass in require argument, _ transforms to /)

like image 39
marsgpl Avatar answered Oct 18 '25 01:10

marsgpl