はじめに

本記事ではWindows11にNeovim + lazy.nvimをインストールする手順を記載します。

以前投稿した記事で、WSL2上のUbuntuにNeovimをインストールしていたのですが、WSL2が何かと重いので、Windows自体にNeovimを入れてみたいと思い試してみました。

環境

  • OS:Windows 11 Pro
    • バージョン:23H2
    • OSビルド:22631.4037

Neovimのインストール

今回はWingetを用いてNeovimをインストールします。

Wingetはマイクロソフトが提供するパッケージマネージャーで、CLIでお手軽に各種パッケージをインストールできます。

Wingetの取得

Wingetがインストールされていない場合は、Microsoft Storeからアプリインストーラーを取得します。

【参考】learn.microsoft.comのWingetインストールに関するドキュメント https://learn.microsoft.com/ja-jp/windows/package-manager/winget/#install-winget

Neovimインストール

Wingetが使えるようになったら、PowerShellを開き以下のコマンドでNeovimをインストールします。途中でインストールしても問題ないか確認された場合は、「はい(yes)」を選択して進めます。

> winget install --id=Neovim.Neovim -e

インストールが終わったら、以下のコマンドでNeovimのバージョンが参照できていればOKです。

> nvim -v
NVIM v0.10.2
Build type: Release
LuaJIT 2.1.1713484068
Run "nvim -V1 -v" for more info

コマンドが認識できない場合は、一度PowerShellを閉じ、再度開いて確認してください。

lazy.nvimのインストール

lazy.nvimの各種ファイルは、「%LOCALAPPDATA%\nvim」に配置することで読み取ってくれます。

そのため、lazy.nvimのドキュメントに則り、以下のフォルダ構成でセットアップしました。

%LOCALAPPDATA%\nvim
├── lua
   ├── config
      └── lazy.lua
   └── plugins
       ├── spec1.lua
       ├── **
       └── spec2.lua
└── init.lua

まずは、PowerShellでlazy.nvim用のフォルダを作成します。

mkdir %LOCALAPPDATA%\nvim

その後、「%LOCALAPPDATA%\nvim\init.lua」と「%LOCALAPPDATA%\nvim\lua\config\lazy.lua」という2つのファイルを作成します。

%LOCALAPPDATA%\nvim\init.lua
require("config.lazy")
%LOCALAPPDATA%\nvim\lua\config\lazy.lua
-- Bootstrap lazy.nvim
local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not (vim.uv or vim.loop).fs_stat(lazypath) then
local lazyrepo = "https://github.com/folke/lazy.nvim.git"
local out = vim.fn.system({ "git", "clone", "--filter=blob:none", "--branch=stable", lazyrepo, lazypath })
if vim.v.shell_error ~= 0 then
vim.api.nvim_echo({
{ "Failed to clone lazy.nvim:\n", "ErrorMsg" },
{ out, "WarningMsg" },
{ "\nPress any key to exit..." },
}, true, {})
vim.fn.getchar()
os.exit(1)
end
end
vim.opt.rtp:prepend(lazypath)

-- Make sure to setup `mapleader` and `maplocalleader` before
-- loading lazy.nvim so that mappings are correct.
-- This is also a good place to setup other settings (vim.opt)
vim.g.mapleader = " "
vim.g.maplocalleader = "\\"

-- Setup lazy.nvim
require("lazy").setup({
spec = {
-- import your plugins
{ import = "plugins" },
},
-- Configure any other settings here. See the documentation for more details.
-- colorscheme that will be used when installing plugins.
install = { colorscheme = { "habamax" } },
-- automatically check for plugin updates
checker = { enabled = true },
})

上記2ファイルが作成出来たら、Neovimを開き、コマンドモードで「:Lazy」と実行して以下のような画面が表示されればOKです。 start_lazy_nvim

Neovim-Qtのインストール

ここまでで、CLIからNeovimを起動できるようになりましたが、NeovimのGUI版であるNeovim-Qtもインストールします。(Neovimバージョン0.10.0以降はCLI版とGUI版それぞれインストールが必要とのこと)

PowerShellからWingetでインストールできます。

> winget install equalsraf.neovim-qt

インストール後、PowerShell等で「nvim-qt」と入力すれば、Neovim-Qtが起動できます。 start_neovimqt

こちらをタスクバー等に登録すれば素早くNeovimを起動できます。

その他の設定

ここからは、Neovimプラグインを設定する際にハマったポイントを記載します。

nvim-treesitterをインストールするとNo C compiler found! “cc”, “gcc”, “clang”, “cl”, “zig” are not executable.と表示される

Neovimでコードをハイライトしてくれるnvim-treesitterを導入したところ、以下のエラーが表示されました。 compile_error

nvim-treesitterのwikiによると、nvim-treesitterに必要なパーサをコンパイルするには、Cコンパイラが必要だが、WindowsはデフォルトではCコンパイラが導入されていないためエラーが発生した模様。

そのため、今回はMingwをインストールすることで回避できました。

MingwのインストールはChocolateyを利用しました。Wingetでもインストールできそうでしたが、バージョンが古かったので、Chocolateyを利用します。

Chocolateyがインストールされていない場合は、Powershellを管理者権限で開き、以下のコマンドを実行します。

> Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))

> choco --version
2.3.0

choco installでMingwをインストールします。

> choco install mingw

インストールしたmingwをPATHに反映させるために以下のコマンドを実行します。

> refreshenv

【備考1】refreshenv実行時に以下のエラーが出た場合、エラー内容に記載の通り、Chocolateyのプロファイルを読み込ませる必要があります。

# エラー内容
RefreshEnv.cmd does not work when run from this process. If you're in PowerShell, please 'Import-Module $env:ChocolateyInstall\helpers\chocolateyProfile.psm1' and try again.

# プロファイルの読み込み
> $env:ChocolateyInstall = Convert-Path "$((Get-Command choco).Path)\..\.."
> Import-Module "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1"

【備考2】 プロファイルの読み込み時に以下のスクリプトの実効が無効になっている旨が表示されたら、一時的にスクリプトの実効を許可する設定とし、再度プロファイルを読み込ませます。

# エラー内容
Import-Module : このシステムではスクリプトの実行が無効になっているためファイル C:\ProgramData\chocolatey\helpers\chocolateyProfile.psm1 を読み込むことができません詳細については、「about_Execution_Policies(https://go.microsoft.com/fw link/?LinkID=135170) を参照してください                                                                               発生場所 :1 文字:1                                                                                                    + Import-Module "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1"                                                 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~                                                     + CategoryInfo          : セキュリティ エラー: (: ) [Import-Module]PSSecurityException                                + FullyQualifiedErrorId : UnauthorizedAccess,Microsoft.PowerShell.Commands.ImportModuleCommand

# スクリプトの実効を一時的に有効化
> Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process
> Get-ExecutionPolicy
Restricted # Restrictedになっていること

refreshenv実行後、gccがPATHに通っていることを確認します。

> gcc --version
gcc.exe (x86_64-posix-seh-rev0, Built by MinGW-Builds project) 13.2.0
Copyright (C) 2023 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

nvim-treesitterを読み込ませるために、以下のluaファイルを用意します。

「require(“nvim-treesitter.install”).compilers」でgccを指定することでエラーを回避できました。

return {
	"nvim-treesitter/nvim-treesitter",
	build = ":TSUpdate",
	install = function()
		require("nvim-treesitter.install").setup({
			prefer_git = false,
			compilers = { "gcc" },
		})
	end,
	config = function()
		local configs = require("nvim-treesitter.configs")
		configs.setup({
			ensure_installed = {
				-- write supported languages
			},
			sync_install = false,
			highlight = { enable = true },
			indent = { enable = true },
		})
	end,
}

mason.nvimでパッケージをインストールするとnpm.cmd is not executableと表示される

mason.nvimでパッケージをインストールした際に、npmのエラーが出ました。 npm_error

こちらは単純にWindowsでnpmをインストールしていないものによるもののため、npmが使えるようにします。

今回は、wingetでnvmをインストールし、nvm経由でNode.js/npmをインストールします。

まずは、PowerShellでnvmをインストールします。

> winget install -e --id CoreyButler.NVMforWindows

その後、以下のコマンドで利用可能なNode.jsのバージョンを確認します。nvmコマンドが認識されていない場合は一度PowerShellを閉じ、再度開いてからコマンドを実行します。

> nvm list available

|   CURRENT    |     LTS      |  OLD STABLE  | OLD UNSTABLE |
|--------------|--------------|--------------|--------------|
|    22.9.0    |   20.18.0    |   0.12.18    |   0.11.16    |
|    22.8.0    |   20.17.0    |   0.12.17    |   0.11.15    |
|    22.7.0    |   20.16.0    |   0.12.16    |   0.11.14    |
|    22.6.0    |   20.15.1    |   0.12.15    |   0.11.13    |
|    22.5.1    |   20.15.0    |   0.12.14    |   0.11.12    |
|    22.5.0    |   20.14.0    |   0.12.13    |   0.11.11    |
|    22.4.1    |   20.13.1    |   0.12.12    |   0.11.10    |
|    22.4.0    |   20.13.0    |   0.12.11    |    0.11.9    |
|    22.3.0    |   20.12.2    |   0.12.10    |    0.11.8    |
|    22.2.0    |   20.12.1    |    0.12.9    |    0.11.7    |
|    22.1.0    |   20.12.0    |    0.12.8    |    0.11.6    |
|    22.0.0    |   20.11.1    |    0.12.7    |    0.11.5    |
|    21.7.3    |   20.11.0    |    0.12.6    |    0.11.4    |
|    21.7.2    |   20.10.0    |    0.12.5    |    0.11.3    |
|    21.7.1    |    20.9.0    |    0.12.4    |    0.11.2    |
|    21.7.0    |   18.20.4    |    0.12.3    |    0.11.1    |
|    21.6.2    |   18.20.3    |    0.12.2    |    0.11.0    |
|    21.6.1    |   18.20.2    |    0.12.1    |    0.9.12    |
|    21.6.0    |   18.20.1    |    0.12.0    |    0.9.11    |
|    21.5.0    |   18.20.0    |   0.10.48    |    0.9.10    |

This is a partial list. For a complete list, visit https://nodejs.org/en/download/releases

バージョン確認時(2024/10時点)のLTS最新バージョンは20.18.0なので、こちらをインストールします。

> nvm install 20.18.0
> nvm use 20.18.0

# バージョン確認
> node --version
v20.18.0
> npm --version
10.8.2

npmが認識できるようになったらエラーが解消できました。

lazy.nvimのプラグイン一覧画面でfatal: detected dubious ownership in repository at と表示される

nvimのコマンドモードで「:Lazy」と入力し、プラグイン一覧を確認すると、「fatal: detected dubious ownership in repository at <プラグインフォルダ>」と表示されていました。 lazy_error

このエラーは、Neovimのプラグインが現在のユーザとは異なるユーザ(Administrators)でインストールされていたため、フォルダの権限周りでエラーとなったように見えます。

ガイダンスでは、「git config –global –add safe.directory <プラグインフォルダ>」を実行せよと記載があるのですが、ワイルドカード等でプラグインフォルダをまとめてsafe.directoryに登録することができなかったため、親フォルダの所有権を変えることで対応しました。

ますは、エクスプローラーでNeovimのプラグインを格納する「%LOCALAPPDATA%/nvim-data」を右クリックし、プロパティ > セキュリティ > 詳細設定をクリックします。 select_settings

所有者欄の「変更」をクリックし、現在操作しているユーザを選択します。

その後、「サブコンテナーとオブジェクトの所有者を置き換える」にチェックを入れた上で適用します。 change_owner

その後、再度Neovimを開き「:Lazy」で確認すると、エラーが解消されていることを確認しました。

終わりに

WindowsでもサクッとNeovimを起動できるようになりました。

Neovimは触っているだけで楽しいのですが、触るだけで満足しないように気を付けたいです…

参考にさせていただいたサイト