Although neovim has built a lot of modules to ease the use of Lua, sometimes we may still want to use some packages from Luarocks. In this post, I would like to share how you can install a package from Luarocks and use it in nvim.
lazy.nvim#
The first way is to use lazy.nvim to install Luarocks package. We need to
- install Luarocks:
brew install luarocks
- install lua 5.1:
brew insall --force lua@5.1
(--force
is needed because otherwise brew will warn that lua 5.1 is deprecated and won’t install)
Also in the repo of the lua package, the rockspec file needs to be the in the top level. For example, for this package https://github.com/leafo/magick, we can install it with lazy just like a normal plugin:
{"leafo/magick"}
Lazy.nvim will automatically install and build it.
The plugin will be installed under ~/.local/share/nvim/lazy-rocks
,
and lazy.nvim will also update the package.path
, so that you can require/use the package.
For lua packages whose rockspec file are not in the top level, lazy.nvim seems can not correctly install them.
For example, for this lua-toml package: https://github.com/jonstoler/lua-toml,
lazy.nvim can not install it correctly: under ~/.local/share/nvim/lazy-rocks
, the package does not exist,
and lazy.nvim just treat it like a nvim plugin and put it under ~/.local/share/nvim/lazy/
instead,
and the package.path
is also not updated.
Using luarocks.nvim#
The second way is to use the plugin luarocks.nvim, and it works better.
Make sure to have all the requirements installed.
Note that for luarocks.nvim, we do not need to install luarocks
manually,
the plugin will try to install/build luarocks automatically for you.
If you are using lazy.nvim as plugin manager, the plugin spec for luarocks.nvim looks like this:
{
"vhyrro/luarocks.nvim",
priority = 1000, -- Very high priority is required, luarocks.nvim should run as the first plugin in your config.
opts = {
rocks = { "lua-toml" }, -- specifies a list of rocks to install
-- luarocks_build_args = { "--with-lua=/my/path" }, -- extra options to pass to luarocks's configuration script
},
},
The luarocks packages are installed under the luarocks.nvim plugin directory:
~/.local/share/nvim/lazy/luarocks.nvim/.rocks
The package.path
variable is also updated by luarocks.nvim.
So after installing a package, you can simply use require()
to import the package.