How do install libraries for both Lua5.2 and 5.1 using Luarocks?
Asked Answered
C

4

22

I am writing a small Lua project and using Luarocks to install my 3rd-party dependencies. The default Lua version on my machine is 5.2 and up to this point everything is working just fine.

However, today I have stumbled across a problem that is confusing me. I want to run my program on Lua 5.1 and Luajit to see if it would also work on those versions but I am having a hard time getting Luarocks to download the appropriate versions of the dependencies. As a last resort hack, I have tried to tell Lua5.1 to use the 5.2 libraries that Luarocks installed (by setting the LUA_PATH environment variable to the same value as LUA_PATH_5_2) but unfortunately that is not enough: my project depends on LuaFileSystem, a C-based module, so I'm going to need to have separate versions of it installed for 5.1 and 5.2.

What do I have to do to install both the 5.1 and 5.2 versions of my dependencies? Do I need to pass some parameters to theluarocks install command? Do I need to have multiple instances of Luarocks installed on my machine? One thing that confuses me is that the inside the .luarocks folder things are classified under a 5.2 subfolder (~/.luarocks/share/lua/5.2/), suggesting that maybe there could be a way to install things in a sibling 5.1 folder but at the same time there is only one bin folder, suggesting that luarocks is only able to handle one version of Lua at a time...

Counterrevolution answered 2/12, 2013 at 5:39 Comment(1)
Note: for the benefit of those who posted an answer, it would be nice if you could clarify what operating system you're talking about. Because you mention a ~/.luarocks folder, it's highly likely that you're using a Un*x variant — possibly macOS or Linux — but instructions for either are not 100% identical. Also, if you could clarify what package manager you use (dpkg, rpm, apt, Hmebrew, snap, others...), or, alternatively, state that you prefer to compile everything locally.Chery
K
27

Based on your reference to ~/.luarocks/share/lua/5.2/, you seem to be running a Unix system (Linux or Mac). You can install the latest version of LuaRocks twice, for both Lua 5.1 and Lua 5.2 like this:

./configure --lua-version=5.1 --versioned-rocks-dir
make build
sudo make install

And then again for 5.2:

./configure --lua-version=5.2 --versioned-rocks-dir
make build
sudo make install

This will get you /usr/local/bin/luarocks-5.1 and /usr/local/bin/luarocks-5.2. If you installed Lua 5.1 and 5.2 in /usr/local/, and each of them will use its own ~/.luarocks/lib/luarocks/rocks-5.x/ entry for the user tree (and /usr/local/lib/luarocks/rocks-5.x for the system tree), and install modules to the right location at /usr/share/lua/5.x/ and ~/.luarocks/share/lua/5.x/ (and likewise for lib) appropriately.

Krissykrista answered 3/12, 2013 at 18:41 Comment(2)
When I ran configure for 5.1 I got the following error: Lua version detected: 5.2 This clashes with the value of --with-lua-version.. I managed to workaround it by by passing an additional --lua-suffix=5.1 parameter. However, is this something that everyone else is going to have to do or was it just me?Counterrevolution
That happened because your "lua" binary was detected first. Your workaround is fine. I just committed a change to make it unnecessary in future releases: github.com/keplerproject/luarocks/commit/…Krissykrista
C
8

As suggested by moteus, I decided to install a second version of Luarocks for Lua 5.1. But he is using Windows and I am using Linux so here is what I did:

  • Download the source for the latest version of Luarocks on the Luarocks website

  • From the source directory, run the ./configure script:

    /configure --prefix="${HOME}/.luarocks51" --lua-suffix=5.1

    The prefix setting tells Luarocks to put its stuff on the .luarocks51 folder, next to the existing .luarocks folder from my 5.2 install of Luarocks. The lua-suffix parameter tells Luarocks to use Lua 5.1 instead of the default lua version in my machine (5.2). This depends on me having named the interpreter for Lua 5.1 as lua5.1 (Debian installed mine on /usr/bin/lua5.1). Finally, Luarocks managed to automatically detect where the 5.1 headers and libraries are installed (/usr/include/lua5.1/) but if it didn't I guess I could have specified that with the --with-lua-include and --with-lua-lib parameters.

  • Compile Luarocks with make

  • Install it with make isntall (no need for Sudo since I'm installing it in a local directory).

  • Configure my 5.1 environment to use the libraries downloaded by Luarocks. I added the following to my .bashrc:

    export PATH=$PATH:~/.luarocks/bin:~/.luarocks51/bin
    export LUA_CPATH=";;${HOME}/.luarocks51/lib/lua/5.1/?.so"
    export LUA_PATH=";;${HOME}/.luarocks51/share/lua/5.1/?.lua;${HOME}/.luarocks51/share/lua/5.1/?/init.lua"
    
    export LUA_CPATH_5_2=";;${HOME}/.luarocks/lib/lua/5.2/?.so"
    export LUA_PATH_5_2=";;${HOME}/.luarocks/share/lua/5.2/?.lua;${HOME}/.luarocks/share/lua/5.2/?/init.lua"
    

    The 5.1 configuration also works for Luajit.

  • The executable for the 5.1 version of luarocks is named luarocks-5.1:

    luarocks-5.1 install lfs
    
Counterrevolution answered 2/12, 2013 at 18:18 Comment(0)
R
5

You have to mention both lua version and lua dir in the latest versions:

luarocks --lua-dir=$(brew --prefix)/opt/[email protected] --lua-version=5.1 install lua-cassandra
Ridgeling answered 12/9, 2019 at 14:25 Comment(1)
You should just add a small note on your answer saying that this applies to environments using Homebrew as a package manager (mostly macOS, but these days also Linux); the OP is not clear about what OS he uses...Chery
D
2

Using homebrew, you can do:

brew install lua51  # Lua 5.1
brew install lua    # Lua latest

Luarocks comes with Lua, so you can do:

# Install Lua 5.1 version of any package
luarocks-5.1 install moonscript

# Install Lua latest version of any package
luarocks install moonscript
Dogmatic answered 10/2, 2018 at 18:5 Comment(1)
Note: lua51 is a deprecated package; the correct one is [email protected]. luarocks should, these days, easily figure out which version of Lua to use, but using @Kamyar's answer it's more certain that it'll find the 'right' version of Lua...Chery

© 2022 - 2024 — McMap. All rights reserved.