Skip to content

LSP Configuration

The first plugin we will use to configure the LSP is called lsp-config and is provided by the Neovim team.

lsp-config is an abstraction for you to be able to quickly enable language servers that you have installed.

However, we haven’t installed any language servers yet.

Installing Language Servers

Each language server can be installed via a package manager — but often in a different way. For example, the language servers for HTML/CSS/JS are provided by Microsoft, and are installed via npm. Whereas the language server for say, Rust, might be managed by rustup.

An easy way to install and manage these language servers is to use a set of plugins called Mason and mason-lspconfig.

Setting Up Mason

As explained in the plugin section, add the following code to your lazy setup:

"williamboman/mason.nvim"

And set it up (e.g. in init.lua or lua/mason-setup.lua) with:

require("mason").setup()

From within Neovim, you can run Mason by typing :Mason and install any language servers you want with I.

mason-lspconfig

This can be extended this with another plugin called mason-lspconfig, which just provides some quality of life features, e.g. ensuring servers are installed, automatic installation if they aren’t etc.

Assuming you have installed both mason and mason-lspconfig with your plugin manager, the following code runs both on startup.

require("mason").setup()
require("mason-lspconfig").setup {
ensure_installed = { "lua_ls" },
}

The extra setup options for mason-lspconfig are the extra features such as ensuring language servers are installed and configured correctly, and automatically installing any that don’t exist on your system.

Setting up the individual language servers

Add the line require("lspconfig").lua_ls.setup {} for each language server you want to set up (replace lua_ls with the language server of your choice).

So the final code might look something like:

require("mason").setup()
require("mason-lspconfig").setup {
ensure_installed = { "lua_ls" },
}
require("lspconfig").lua_ls.setup {}

And that’s it! next time you quit and re-open Neovim, the language server should already be working.