Update log
  • 2022-08-13: update nvim lsp config for lua-language-server.

In this post, I will share how to set up sumneko lua for nvim-lspconfig.

Install sumneko lua

Install using package manager

From macOS, we can install lua-language-server directly using HomeBrew:

brew install lua-language-server

Install using releases

Now lua-language-server provides binary release for Windows, macOS and Linux, download it from its release page.

Install from source

We can also build it from source. First, we need to install its dependency packages.

ninja

On Linux, install ninja from its binary release:

wget https://hub.fastgit.xyz/ninja-build/ninja/releases/download/v1.10.2/ninja-linux.zip

mkdir -p $HOME/local/bin
unzip ninja-linux.zip -d $HOME/local/bin

On macOS, install ninja via homebrew:

brew install ninja

C++ compiler

We need to install a cpp compile that support c++ 17, gcc-7.3 is fine. Clang 12.0 is also fine.

build

Build lua-language-server:

mkdir -p $HOME/tools/ && cd $HOME/tools
git clone --depth=1 https://hub.fastgit.xyz/sumneko/lua-language-server

cd lua-language-server
# if the cloning speed is too slow, edit .gitmodules and replace github.com
# with hub.fastgit.org, which should be faster than github.
git submodule update --init --recursive

# build on Linux
cd 3rd/luamake
compile/install.sh
cd ../..
./3rd/luamake/luamake rebuild

After that, we should add the path to lua-language-server to PATH:

# For macOS
export PATH="$HOME/tools/lua-language-server/bin/macOS:$PATH"

# For Linux
export PATH="$HOME/tools/lua-language-server/bin/Linux:$PATH"

Config for nvim-lspconfig

Following the instructions on the nvim-lspconfig repo, it is pretty straight forward to config sumneko-lua for Nvim. Here is a working config for me:

local runtime_path = vim.split(package.path, ";")
table.insert(runtime_path, "lua/?.lua")
table.insert(runtime_path, "lua/?/init.lua")

lspconfig.sumneko_lua.setup({
on_attach = custom_attach,
settings = {
    Lua = {
    runtime = {
        -- Tell the language server which version of Lua you're using (most likely LuaJIT in the case of Neovim)
        version = "LuaJIT",
        -- Setup your lua path
        path = runtime_path,
    },
    diagnostics = {
        -- Get the language server to recognize the `vim` global
        globals = { "vim" },
    },
    workspace = {
        -- Make the server aware of Neovim runtime files
        library = api.nvim_get_runtime_file("", true),
    },
    -- Do not send telemetry data containing a randomized but unique identifier
    telemetry = {
        enable = false,
    },
    },
},
})

For the complete config for lsp, check my config here.

References