Reputation: 35
I managed to install luasocket and works fine using lua5.3 but i can't find the way to install them to require from luajit.
If i write print(_VERSION) it shows luajit use lua5.1.
This is the stuff i been trying:
luarocks install luasocket
luarocks --local install luasocket
luarocks --lua-version 5.1 install luasocket
luarocks --lua-version 5.1 --local install luasocket
luarocks config lua_version 5.1
luarocks install luasocket
sock = require("/data/data/com.termux/files/home/.luarocks/lib/luarocks/rocks-5.1/socket")
package.path = package.path .. ";/data/data/com.termux/files/home/.luarocks/lib/luarocks/rocks-5.1" require "socket"
package.path = package.path .. ";/data/data/com.termux/files/home/.luarocks/lib/lua/5.1" require "socket"
...
The error:
$ luajit
LuaJIT 2.1.0-beta3 -- Copyright (C) 2005-2017 Mike Pall. http://luajit.org/
JIT: ON ARMv7 VFPv3 fold cse dce fwd dse narrow loop abc sink fuse
> sock = require("socket")
stdin:1: module 'socket' not found:
no field package.preload['socket']
no file './socket.lua'
no file '/data/data/com.termux/files/usr/share/luajit-2.1.0-beta3/socket.lua'
no file '/usr/local/share/lua/5.1/socket.lua'
no file '/usr/local/share/lua/5.1/socket/init.lua'
no file '/data/data/com.termux/files/usr/share/lua/5.1/socket.lua'
no file '/data/data/com.termux/files/usr/share/lua/5.1/socket/init.lua'
no file './socket.so'
no file '/usr/local/lib/lua/5.1/socket.so'
no file '/data/data/com.termux/files/usr/lib/lua/5.1/socket.so'
no file '/usr/local/lib/lua/5.1/loadall.so'
stack traceback:
[C]: in function 'require'
stdin:1: in main chunk
[C]: at 0xaba4406d
I tryied to move the installed files to some of those paths but still don't work.
Upvotes: 1
Views: 6699
Reputation: 2145
Here's the thing, you stated:
"I add the path and get: /data/data/com.termux/files/home/.luarocks/share/lua/5.1/socket.lua module 'socket.core' not found..."
That's because socket.lua exists in that directory, but you moved it's subdirectory:
/data/data/com.termux/files/home/.luarocks/share/lua/5.1/socket/core.lua
So that dir will no longer do anything for you until you delete it and begin again.
rm /data/data/com.termux/files/home/.luarocks/share/lua/5.1/socket.lua
rm -rf /data/data/com.termux/files/home/.luarocks/share/lua/5.1/socket/
luarocks --lua-version 5.1 install luasocket
Now watch carefully as it installs, to see what directory it actually installs to,
or use that find
command I mentioned earlier to locate your Lua5.1 compatable socket location and add that to your LUA_PATH.
sudo apt install mlocate && sudo updatedb && locate socket.lua
You don't need to add these directories to your LUA_PATH as they're already being searched
> sock = require("socket")
stdin:1: module 'socket' not found:
no field package.preload['socket']
no file './socket.lua'
no file '/data/data/com.termux/files/usr/share/luajit-2.1.0-beta3/socket.lua'
no file '/usr/local/share/lua/5.1/socket.lua'
no file '/usr/local/share/lua/5.1/socket/init.lua'
no file '/data/data/com.termux/files/usr/share/lua/5.1/socket.lua'
no file '/data/data/com.termux/files/usr/share/lua/5.1/socket/init.lua'
no file './socket.so'
no file '/usr/local/lib/lua/5.1/socket.so'
no file '/data/data/com.termux/files/usr/lib/lua/5.1/socket.so'
no file '/usr/local/lib/lua/5.1/loadall.so'
;;
won't do anything because you've already pointed out that it doesn't reside in any of those default dirs.
It is possible that it's looking for the right dir, but the wrong extension.
/usr/local/lib/lua/5.1/socket.so
as opposed to /usr/local/lib/lua/5.1/socket.lua
in this case, use the softlink method I mentioned earlier, making sure to use the correct location, this is just an example:
ln -s /usr/local/lib/lua/5.1/socket.lua /usr/local/lib/lua/5.1/socket.so
Upvotes: 0
Reputation: 7064
luarocks --lua-version 5.1 install luasocket
is the proper way to install it.
That should also tell you in the last line of output where it installed the module, so you can check if that's in your package.path
.
It seems you just misunderstand how package.path
works. From the manual:
A path is a string containing a sequence of templates separated by semicolons. For each template, the function replaces each interrogation mark (if any) in the template with a copy of name wherein all occurrences of sep (a dot, by default) were replaced by rep (the system's directory separator, by default), and then tries to open the resulting file name.
What that means is that a path like /usr/share/lua/5.1
won't find anything, because it doesn't find a valid Lua file at that path and since there's no ?
, no substitution is done whatsoever. The first of the two paths is OK, but many Lua modules use a file called init.lua
instead, so for every <path>/?.lua
you should always also add a <path>/?/init.lua
to cover that option as well.
Normally Luarocks should install the packages main Lua file at /usr/local/share/lua/5.1/socket.lua
, but for your case that doesn't seem to work. You can have a look at that directory and see if the file is there at all, and check that luarocks config deploy_lua_dir
actually returns /usr/local/share/lua/5.1
.
Upvotes: 2
Reputation: 2145
socket.lua was installed to a directory that doesn't exist in your LUA_PATH
find where socket.lua was installed, with find or mlocate, if installed:
find / -name socket.lua 2>&1 | grep -v Perm
sudo updatedb && locate socket.lua
~/.luaver/luarocks/3.2.0_5.1/share/lua/5.1/socket.lua
~/.luaver/luarocks/3.2.0_5.3/share/lua/5.3/socket.lua
/usr/share/lua/5.1/socket.lua
/usr/share/lua/5.2/socket.lua
/usr/share/lua/5.3/socket.lua
edit your .bashrc -- assuming a bash shell here, but others follow a similar process. take your pick, and modify accordingly https://wiki.termux.com/wiki/Shells
find the line that says export LUA_PATH='/usr/local/share/lua/5.1/?.lua;yadda-yadda
add a semicolon plus the path(s) you found from above.
export LUA_PATH='/usr/local/share/lua/5.1/?.lua;/usr/share/lua/5.1'
close any open terminals, then open a fresh one, now that you have that new path enabled.
alternatively, you can use softlinks.
ln -s /usr/share/lua/5.1/socket.lua /usr/local/share/lua/5.1/socket.lua
I'm not a fan of this secondary softlink method, because you'd have to do it for every Lua module that resides outside of your path, but I'm including it so you know your options. If it's just for that particular module, this would be an acceptable solution, because it does what you set out to do. However, in the future when you run into the same issue with other modules, you would have to repeat the process for those, as well. Modifying the LUA_PATH is your best bet.
Upvotes: 1