0

Run the following simple lua script but got error. I am running lua 5.2 on ubuntu.

local host, port = "127.0.0.1", 80
local socket = require("socket")
local tcp = assert(socket.tcp())

tcp:connect(host, port);
--note the newline below
tcp:send("GET / HTTP/1.1\r\n\r\n");

Here is the error:

~/learn/lua$ lua te.lua
lua: te.lua:2: module 'socket' not found:
    no field package.preload['socket']
    no file '/usr/local/share/lua/5.2/socket.lua'
    no file '/usr/local/share/lua/5.2/socket/init.lua'
    no file '/usr/local/lib/lua/5.2/socket.lua'
    no file '/usr/local/lib/lua/5.2/socket/init.lua'
    no file '/usr/share/lua/5.2/socket.lua'
    no file '/usr/share/lua/5.2/socket/init.lua'
    no file './socket.lua'
    no file '/usr/local/lib/lua/5.2/socket.so'
    no file '/usr/lib/x86_64-linux-gnu/lua/5.2/socket.so'
    no file '/usr/lib/lua/5.2/socket.so'
    no file '/usr/local/lib/lua/5.2/loadall.so'
    no file './socket.so'
stack traceback:
    [C]: in function 'require'
    te.lua:2: in main chunk
    [C]: in ?
2
  • Possible duplicate of Module socket not found lua Commented Nov 12, 2016 at 17:12
  • Thanks for the link, I googled and find the lua package manager that solved me problem. Commented Nov 12, 2016 at 17:35

1 Answer 1

1

Problem solved after installing the package manager luarocks and then socket package.

Here are the commands from the page

$ wget https://luarocks.org/releases/luarocks-2.4.1.tar.gz
$ tar zxpf luarocks-2.4.1.tar.gz
$ cd luarocks-2.4.1
$ ./configure; sudo make bootstrap
$ sudo luarocks install luasocket
$ lua
Lua 5.3.3 Copyright (C) 1994-2016 Lua.org, PUC-Rio
> require "socket" 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.