鸿蒙PC融合开发引擎中编译安装Neovim与AstroNvim配置指南
本文记录在鸿蒙PC融合开发引擎的容器环境中,因glibc版本限制从源码编译Neovim和tree-sitter的完整过程,并配置AstroNvim作为开发环境。
问题背景 glibc版本冲突 在鸿蒙PC融合开发引擎的openEuler容器中,尝试安装nvim-treesitter插件时遇到错误:
1 2 [nvim-treesitter/install/c] error: Error during "tree-sitter build": tree-sitter: /usr/lib64/libc.so.6: version `GLIBC_2.39' not found (required by tree-sitter)
版本检查 1 2 3 4 5 6 ldd --version cat /etc/os-release
openEuler 24.03 SP1的glibc是2.38,而nvim-treesitter下载的预编译tree-sitter CLI需要glibc 2.39。
根本原因 容器环境共享虚拟机内核,无法升级glibc。解决方案是从源码编译所有组件 ,使用当前系统的glibc版本。
解决方案总览 1 2 3 4 5 1. 安装编译依赖 2. 安装Rust工具链(编译tree-sitter CLI需要) 3. 从源码编译tree-sitter CLI 4. 从源码编译Neovim 5. 配置AstroNvim
一、安装编译依赖 基础工具 1 2 3 4 5 6 sudo dnf install -y cmake ninja-build cmake --version ninja --version
其他依赖 1 2 3 gcc --version make --version
二、安装Rust工具链 tree-sitter CLI使用Rust编写,需要先安装Rust:
1 2 3 4 5 6 7 8 9 10 11 12 export https_proxy=http://192.168.3.6:1080curl --proto '=https' -sSf https://sh.rustup.rs | sh -s -- -y source ~/.cargo/envcargo --version rustc --version
三、编译tree-sitter CLI 获取源码 1 2 3 4 5 6 export https_proxy=http://192.168.3.6:1080git clone --depth 1 --branch v0.24.1 \ https://github.com/tree-sitter/tree-sitter.git \ ~/tree-sitter-cli
编译 1 2 cd ~/tree-sitter-clicargo build --release
编译过程约需1-2分钟。
安装 1 2 3 4 5 6 7 8 9 10 sudo cp ~/tree-sitter-cli/target/release/tree-sitter /usr/local /bin/ cp ~/tree-sitter-cli/target/release/tree-sitter \ ~/.local /share/nvim/mason/packages/tree-sitter-cli/tree-sitter-linux-arm64 tree-sitter --version
四、编译Neovim 获取源码 1 2 3 4 5 6 7 8 9 10 export https_proxy=http://192.168.3.6:1080git ls-remote --tags https://github.com/neovim/neovim.git | \ grep -v '\^{}' | grep 'v0\.1[0-2]' | tail -5 git clone --depth 1 --branch v0.12.0 \ https://github.com/neovim/neovim.git \ ~/neovim
编译 1 2 cd ~/neovimmake CMAKE_BUILD_TYPE=Release
编译过程约需3-5分钟,会自动下载并编译所有依赖。
安装 1 2 3 4 5 6 7 8 sudo make install nvim --version
编译输出位置 1 2 ls ~/neovim/build/bin/nvim
五、配置AstroNvim AstroNvim是一个预配置的Neovim发行版,提供完整的IDE体验。
备份现有配置 1 2 3 4 mv ~/.config/nvim ~/.config/nvim.bak mv ~/.local /share/nvim ~/.local /share/nvim.bak mv ~/.local /state/nvim ~/.local /state/nvim.bak
安装AstroNvim 1 2 3 4 5 6 git clone --depth 1 https://github.com/AstroNvim/AstroNvim \ ~/.config/nvim cp ~/.config/nvim/lua/user_template.lua ~/.config/nvim/lua/user.lua
首次启动
首次启动会自动安装所有插件和tree-sitter解析器。由于我们已编译了tree-sitter CLI,treesitter安装不会出现glibc错误。
自定义配置 编辑 ~/.config/nvim/lua/user.lua:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 return { colorscheme = "catppuccin-mocha" , options = { opt = { relativenumber = true , }, }, plugins = { "rebelot/kanagawa.nvim" , }, lsp = { formatting = { format_on_save = true , }, servers = { }, }, }
安装LSP和工具 在Neovim中运行:
1 2 3 4 5 :LspInstall python :LspInstall lua_ls :TSInstall python :TSInstall lua :MasonInstall prettier
六、验证tree-sitter工作 安装解析器
查看可用的解析器,选择需要的安装:
1 2 3 4 5 6 7 8 :TSInstall vimdoc :TSInstall lua :TSInstall python :TSInstall javascript :TSInstall html :TSInstall css :TSInstall json :TSInstall markdown
检查状态
所有解析器应显示为 ✓ installed。
七、总结 编译清单
组件
版本
状态
cmake
3.27.9
✓ 已安装
ninja
1.11.1
✓ 已安装
Rust
1.94.1
✓ 已安装
tree-sitter CLI
0.24.1
✓ 已编译
Neovim
0.12.0
✓ 已编译
AstroNvim
latest
✓ 已配置
关键点
glibc兼容 :从源码编译确保使用当前glibc
Rust依赖 :tree-sitter CLI需要Rust编译
完整流程 :依赖 → Rust → tree-sitter → Neovim → AstroNvim
性能表现 编译的Neovim在openEuler容器中运行流畅:
启动速度:< 100ms
LSP响应:快速
Treesitter高亮:正常
插件生态:完整支持
避坑指南
不要使用预编译tree-sitter :mason下载的预编译版本glibc不兼容
记得替换mason的tree-sitter :否则nvim-treesitter仍会使用预编译版本
使用稳定版本 :避免main分支的不稳定性
八、清理构建文件 编译完成后可以清理源码:
1 2 3 4 rm -rf ~/tree-sitter-cli rm -rf ~/neovim
建议保留源码目录,方便后续版本升级。
九、后续升级 升级Neovim:
1 2 3 4 5 cd ~/neovimgit fetch --tags git checkout v0.13.0 make CMAKE_BUILD_TYPE=Release sudo make install
升级tree-sitter:
1 2 3 4 5 cd ~/tree-sitter-cligit fetch --tags git checkout v0.25.0 cargo build --release sudo cp target/release/tree-sitter /usr/local /bin/
参考资源 :