dshimizu/blog/alpha

とりとめのないITブログ

Debian 11 へ Neovim をインストールして、 dein.vim の plugin manager を使って Go の language server(gopls) を利用できるようにするためのメモ

はじめに

Neovim をインストールして、 dein.vim の plugin manager を使って Go の language server(gopls) を利用できるようにするのに結構手間取ったので、設定方法をメモしておきます。

目次

設定手順

環境

% uname -svo
Linux #1 SMP Debian 5.10.162-1 (2023-01-21) GNU/Linux
$ go version
go version go1.20 linux/amd64

gopls のインストール

gopls をインストールします。

$ go install golang.org/x/tools/gopls@latest

$GOPATH/bin 配下にインストールされます。 ここでは、 $HOME/$GOPATH/bin となっています。

$ ls  ~/go/bin/gopls
/home/dshimizu/go/bin/gopls

Neovim のインストール

Neovim をインストールします。 やり方はいくつかあると思いますが、ここでは GitHub リポジトリから取得して、 $HOME/.local/neovim にインストールします。

$ git clone https://github.com/neovim/neovim

$ cd neovim

$ make CMAKE_EXTRA_FLAGS="-DCMAKE_INSTALL_PREFIX=$HOME/.local/neovim" CMAKE_BUILD_TYPE=Release

$ make install

以下のような感じでファイルが配置されます。

$ tree -L 1 .local/neovim/
.local/neovim/
├── BACKERS.md
├── BSDmakefile
├── CMakeLists.txt
├── CMakePresets.json
├── CONTRIBUTING.md
├── LICENSE.txt
├── MAINTAIN.md
├── Makefile
├── README.md
├── bin
├── build
├── cmake
├── cmake.config
├── cmake.deps
├── cmake.packaging
├── contrib
├── lib
├── runtime
├── scripts
├── share
├── snap
├── src
└── test

14 directories, 9 files

パスを通します。

export PATH=$PATH:$HOME/.local/neovim/bin

パスが通っていることを確認します。

$ nvim -v
NVIM v0.10.0-dev-102+g85bc9e897
Build type: Release
LuaJIT 2.1.0-beta3

      システム vimrc: "$VIM/sysinit.vim"
       省略時の $VIM: "/home/dshimizu/.local/neovim/share/nvim"

Run :checkhealth for more info

dein.vim のインストール

これもやり方はいくつかあると思うけど、ここではインストールスクリプトを使って ~/.config/nvim/dein 配下に インストールします。

$ curl https://raw.githubusercontent.com/Shougo/dein-installer.vim/main/installer.sh > installer.sh

$ ./installer.sh ~/.config/nvim/dein

以下のような出力がされますので、 2 neovim path (~/.config/nvim/init.vim) を選択するため、 2 を入力します。

[ CONFIG LOCATION ]

1 vim path (~/.vimrc)
2 neovim path (~/.config/nvim/init.vim)

Select your editor config location (eg. 1 or 2)
2

以下のように GitHub からソースを取得されて配置されるようなログが出ます。 ~/.config/nvim/init.vim がある場合は、バックアップされて、dein 用のものが配置されるようです。

Cloning Dein.vim into '/home/dshimizu/.config/nvim/dein/repos/github.com/Shougo/dein.vim'...
remote: Enumerating objects: 66, done.
remote: Counting objects: 100% (66/66), done.
remote: Compressing objects: 100% (55/55), done.
remote: Total 66 (delta 2), reused 30 (delta 0), pack-reused 0
Unpacking objects: 100% (66/66), 80.78 KiB | 3.85 MiB/s, done.
From https://github.com/Shougo/dein.vim
 * branch            master     -> FETCH_HEAD
 * [new branch]      master     -> origin/master
Looking for an existing 'init.vim' config...
Found init.vim. Creating backup in '/home/dshimizu/.config/nvim/init.vim.pre-dein-vim'...
Using the Dein.vim config example and adding it to '/home/dshimizu/.config/nvim/init.vim'...
_______________________________________________________________

 #######  ######## ### ###  ###       ###  ### ### ###########
 ##!  ### ##!      ##! ##!#!###       ##!  ### ##! ##! ##! ##!
 #!#  !#! #!!!:!   !!# #!##!!#!       #!#  !#! !!# #!! !#! #!#
 !!:  !!! !!:      !!: !!:  !!!        !:..:!  !!: !!:     !!:
 ::::::   :::::::: ::: :::   ::   ::     ::    ::: :::     :::

              by Shougo • MIT License • v3.0
_______________________________________________________________

All done. Look at your init.vim file to set plugins, themes, and more.

● Documentation: /home/dshimizu/.config/nvim/dein/repos/github.com/Shougo/dein.vim/doc/dein.txt
● Chat with the community: https://gitter.im/Shougo/dein.vim
● Report issues: https://github.com/Shougo/dein.vim/issues

これで、 ~/.config/nvim/dein が作成されます。 また init.vim も配置されます。

/home/dshimizu/.config/nvim/
├── dein
├── init.vim
├── pack
└── plugins

dein.vim の設定

~/.config/nvim/dein/dein.toml に以下の設定を追記します。

[[plugins]]
repo = 'neovim/nvim-lspconfig'
hook_add = '''
lua <<EOF
  local lspconfig = require'lspconfig'
  lspconfig.gopls.setup{}
EOF
'''

[[plugins]]
repo = 'nvim-lua/completion-nvim'

~/.config/nvim/dein/dein_lazy.tomlを新規作成し、以下の設定を記述します。

[[plugins]]
repo = 'Shougo/neco-vim'

[[plugins]]
repo = 'Shougo/denite.nvim'

[[plugins]]
repo = 'Shougo/deoplete.nvim'

[[plugins]]
repo = 'Shougo/neomru.vim'

~/.config/nvim/init.vimcall dein#load_toml('~/.config/nvim/dein/dein.toml', {'lazy': 0}), call dein#load_toml('~/.config/nvim/dein/dein_lazy.toml', {'lazy': 1}) を追記します。 また、最後の if~ call dein#install() ~ endif の部分をコメントアウトをアンコメントして、plugin を自動でインストールできるようにしておきます。 それか Neovim 起動時に :call dein#install() を実行します。

" Ward off unexpected things that your distro might have made, as
" well as sanely reset options when re-sourcing .vimrc
set nocompatible

" Set Dein base path (required)
let s:dein_base = '/home/dshimizu/.config/nvim/dein'

" Set Dein source path (required)
let s:dein_src = '/home/dshimizu/.config/nvim/dein/repos/github.com/Shougo/dein.vim'

" Set Dein runtime path (required)
execute 'set runtimepath+=' . s:dein_src

" Call Dein initialization (required)
call dein#begin(s:dein_base)

call dein#add(s:dein_src)

" Your plugins go here:
"call dein#add('Shougo/neosnippet.vim')
"call dein#add('Shougo/neosnippet-snippets')
"call dein#add('/home/dshimizu/.config/nvim/dein/dein.toml')
call dein#load_toml('~/.config/nvim/dein/dein.toml', {'lazy': 0})
call dein#load_toml('~/.config/nvim/dein/dein_lazy.toml', {'lazy': 1})

" Finish Dein initialization (required)
call dein#end()

" Attempt to determine the type of a file based on its name and possibly its
" contents. Use this to allow intelligent auto-indenting for each filetype,
" and for plugins that are filetype specific.
if has('filetype')
  filetype indent plugin on
endif

" Enable syntax highlighting
if has('syntax')
  syntax on
endif

" Uncomment if you want to install not-installed plugins on startup.
if dein#check_install()
  call dein#install()
endif

発生したエラーとその対処

1つ目

以下のようなエラーが出ました。

Spawning language server with cmd: `gopls` failed. The language server is either not installed, missing from PATH, or not executable.

gopls にパスが読めない場合に発生するので、パスが通っているか確認して、通っていない場合はパスを設定します。

2 つ目

Neovim を起動したときに以下のようなエラーが出ました。

[deoplete] deoplete requires Python3 support("+python3").
VimEnter Autocommands for "*"..function deoplete#enable[9]..deoplete#initialize[1]..deoplete#init#_initialize[15]..deoplete#init#_channel[59]..VimEnter Autocommands for "*"..function deoplete#enable[9]..deoplete#initialize[1]..deoplet
e#init#_initialize[15]..deoplete#init#_channel[42]..deoplete#init#_python_version_check の処理中にエラーが検出されました:
行    8:
E319: No "python3" provider found. Run ":checkhealth provider"

Neovim の pip ライブラリが必要なようですのでインストールします。

$ pip3 install neovim

neovim を起動し、以下を実行します。

:checkhealth provider

以下を実行するようなメッセージが出力されたら、以下も実行します。

:UpdateRemotePlugins

確認

これで、Neovim で language server(gopls) を起動できるようになりました。 適当にコードをエラーになるコードを書いてみると、以下のように表示されます。

package main

import "fmt"

func main() {
        fmt.Printn("Hello World")     ■ undefined: fmt.Printn
}

まとめ

Neovim をインストールして、 dein.vim の plugin manager を使って Go の language server(gopls) を利用できるようにするための設定がうまくできなかったのでその手順を記載しました。

  • gopls のインストール
  • Neovim のインストール
  • dein.vim のインストール
  • dein.vim の設定/プラグインのインストール
  • エラーの対処