28 July 2021 Link

Finally I got around to trying out LuaRocks by installing it in my system and also creating a rockspec to add my simple Lua module in the LuaRocks collection. Here are some tips I picked up:

Create and upload a rock to Luarocks

The major steps are as follows:
  1. Write the rockspec file as explained here
  2. Save the file with the name as lowercase package and version example many2one- Here is the version number and -3 is the rockspec number in case I just change something in the rockspec. This should match the version mentioned in the rockspec.
  3. Create a rock using luarocks and the rockspec described below
  4. Test the rock as described below
  5. Upload the rock and rockspec to Luarocks

Creating a rockspec

The instructions are very clear and easy to follow here. I had my script in github. One thing I did wrong was to provide an https url for github and then luarocks gives an error in installation. I had to make the URL start with git:// that fixed it.

Creating a rock

I then ran the command:
> luarocks pack mymodule-1.01-1.rockspec
And it packed the files in a rock file.
NOTE: When I put the rockspec in a subdirectory like I did in tableUtils I had to run the command from the repository root directory like this:
> luarocks pack rockspecs/tableutils-
It created a rock in the repository root. Also note in the rockspec the module file path has to be given from the repository root.

Then I simply uploaded the rockspec and rock files on LuaRocks and my 1st Lua script is available through LuaRocks!

Testing a rock

Before you upload to the LuaRocks server it is a good idea to test the rock. This can be done by simply going to the folder having the rock and typing:
> luarocks make mymodule-1.01-1.rockspec

If that works and installs the rock then you can upload the rockspec and rock file to the LuaRocks server.

Installing LuaRocks

Installing LuaRocks was not as straightforward. Here are some notes to help anyone who needs some extra help:

Install Lua

If you want the non default version of Lua (LuaRocks default is 5.1 and I wanted to go with 5.2.3) then you need to have it installed at a path in the following structure:
- rootPath
     |- bin
          |- lua52.exe (Can be simply lua.exe)
          |- lua52.dll (or lua53.dll for Lua 5.3)
     |- lib
          |- lua52.dll
     |- include
          |- lauxlib.h
          |- lua.h
          |- lua.hpp
          |- luaconf.h
          |- lualib.h

Download LuaRocks

This page gives the download link

Install LuaRocks

Installation on windows is done by running install.bat batch file in the downloaded archive. Use install /? for all the command line options. Open a command terminal with administrator privileges. The command I used was:
install /LV 5.2 /LUA c:\my\lua\rootPath /MW /P c:\my\lua\rootPath\luarocks /NOADMIN
Make sure MinGW is installed and is in the path of the shell from which you execute the above command.

/LV specifies my lua version
/LUA specifies my Lua root path which is the root directory path of the hierarchy I described above
/MW specifies I want to use mingw as my build system
/P specifies where I want the LuaRocks to be installed. LuaRocks creates the last directory (in the above case 'luarocks') and installs there.
/NOADMIN The installer requires admin priviledges. If not available it will elevate a new process. Use this switch to prevent elevation, but make sure the destination paths are all accessible for the current user.

Now the problem with this installation is that the rocks installed are not in my Lua Path. So I need to have package.path and package.cpath updated in my Lua executable so it points to the directory where LuaRocks installs its rocks.

At the end of the installation LuaRocks displays a list of paths that must be updated in the Lua executable and the command environment. For guidance on how to update the paths in the Lua executable directly see here. I simply took the text from the LuaRocks output and converted to a batch file which looks like this:
  1. REM You may want to add the following elements to your paths;
  2. REM Lua interpreter;
  3. SET PATH=d:\Milind\Documents\Programs\luarocks\bin\;%PATH%
  5. REM LuaRocks;
  6. SET PATH=%PATH%;d:\Milind\Documents\Programs\luarocks\luarocks
  7. SET LUA_PATH=%LUA_PATH%;d:\Milind\Documents\Programs\luarocks\luarocks\lua\?.lua;d:\Milind\Documents\Programs\luarocks\luarocks\lua\?\init.lua
  8. REM Local user rocktree (Note: %APPDATA% is user dependent);
  9. SET PATH=%PATH%;%APPDATA%\LuaRocks\bin
  10. SET LUA_PATH=%LUA_PATH%;%APPDATA%\LuaRocks\share\lua\5.3\?.lua;%APPDATA%\LuaRocks\share\lua\5.3\?\init.lua
  11. SET LUA_CPATH=%LUA_CPATH%;%APPDATA%\LuaRocks\lib\lua\5.3\?.dll
  12. REM System rocktree
  13. SET PATH=%PATH%;d:\milind\documents\programs\luarocks\bin
  14. SET LUA_PATH=%LUA_PATH%;d:\milind\documents\programs\luarocks\share\lua\5.3\?.lua;d:\milind\documents\programs\luarocks\share\lua\5.3\?\init.lua
  15. SET LUA_CPATH=d:\milind\documents\programs\luarocks\lib\lua\5.3\?.dll
  16. REM Note that the %APPDATA% element in the paths above is user specific and it MUST be replaced by its actual value.
  17. REM For the current user that value is: C:\Users\Milind\AppData\Roaming.

NOTE Here in the 1st Path statement I placed the luarocks bin path in front of all the previously defined paths so that when this batch file is run it places the lua executable in the luarocks installation 1st in line to run when lua is called from the command line.
I simply run this batch file to initialize my LuaRocks environment. I did not add them permanently since I have a separate Lua setup system which I use.

After the install process is done. It is best to run:
>luarocks install dkjson
This package is needed for uploading rockspecs and may be used by LuaRocks at other times as well.

Installing a complicated rock on your LuaRocks setup

Here I will list out the steps to install LuaSec using LuaRocks. The installation is not straightforward since it depends on openssl and LuaSec rockspec does not compile it and generate its dependencies. So you need to compile openssl separately and then place the dependency files in the LuaRocks tree and then run the LuaSec install. Here are the steps:

  1. Download and compile openssl. If you are not able to do that refer here for instructions and a script on how it can be done on windows
  2. Copy the include directory (named openssl) containing all the include files of openssl sources into the rootPath\include directory.
  3. Copy the generated dll files (libeay32 and ssleay32) into the rootPath\bin directory
  4. Copy the generated dll files (libeay32 and ssleay32) into the path of your compiler libraries. I was using MinGW so I placed them at C:\mingw\lib directory. To see which directories mingw compiler is looking at do:
    > ld --verbose | grep SEARCH_DIR
  5. Now run:
    >luarocks install LuaSec
    It should be able to install the rock now.