ITPub博客

首页 > 应用开发 > C/C++ > 在HarmonyOS PC融合开发引擎中编译并安装Neovim和AstroNvim配置指南

在HarmonyOS PC融合开发引擎中编译并安装Neovim和AstroNvim配置指南

C/C++ 作者:Flow_Official 时间:2026-10-09 13:39:40 0 删除 编辑

本文记录了由于HarmonyOS PC融合开发容器环境中glibc版本的限制,从源代码编译Neovim和tree-sitter的完整过程。

title=”HarmonyOS PC openEuler 常用于开发软件” frameborder=”0” allow=”accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share” referrerpolicy=”strict-origin-when-cross-origin” allowfullscreen>

问题背景

glibc版本冲突

在HarmonyOS PC融合开发引擎的openEuler容器中,尝试安装nvim-treesitter插件时遇到错误:

[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)

版本检查

ldd --version
# 输出:ldd (GNU libc) 2.38

cat /etc/os-release
# NAME=”openEuler”
# VERSION=”24.03 (LTS-SP1)”

openEuler 24.03 SP1的glibc版本为2.38,而nvim-treesitter下载的预编译tree-sitter CLI需要glibc 2.39。

根本原因

容器环境共享虚拟机内核,无法升级glibc。解决方案是从源代码编译所有组件,使用当前系统的glibc版本。

解决方案概述

1. 安装编译依赖
2. 安装Rust工具链(编译tree-sitter CLI需要)
3. 从源码编译tree-sitter CLI
4. 从源码编译Neovim
5. 配置AstroNvim

1. 安装编译依赖

基础工具

# 安装cmake和ninja
sudo dnf install -y cmake ninja-build

# 验证安装
cmake --version
ninja --version

其他依赖

# gcc、make等已预装
gcc --version
make --version

2. 安装 Rust 工具链

tree-sitter CLI 是用 Rust 编写的,需要先安装:

# 设置代理(可选)
export https_proxy=http://192.168.3.6:1080

# 安装Rust
curl --proto ‘=https’ -sSf https://sh.rustup.rs | sh -s -- -y

# 加载环境
source ~/.cargo/env

# 验证
cargo --version
rustc --version

3. 编译 tree-sitter CLI

获取源代码

export https_proxy=http://192.168.3.6:1080

# 克隆tree-sitter CLI(使用稳定版本v0.24.1)
git clone --depth 1 --branch v0.24.1 \
    https://github.com/tree-sitter/tree-sitter.git \
    ~/tree-sitter-cli

编译

cd ~/tree-sitter-cli
cargo build --release

编译过程大约需要 1-2 分钟。

安装

# 系统安装
sudo cp ~/tree-sitter-cli/target/release/tree-sitter /usr/local/bin/

# 或替换mason的预编译版本
cp ~/tree-sitter-cli/target/release/tree-sitter \
    ~/.local/share/nvim/mason/packages/tree-sitter-cli/tree-sitter-linux-arm64

# 验证
tree-sitter --version
# 输出:tree-sitter 0.24.1

4. 编译 Neovim

获取源代码

export https_proxy=http://192.168.3.6:1080

# 查看最新版本
git ls-remote --tags https://github.com/neovim/neovim.git | \
    grep -v ‘\^{}’ | grep ‘v0\.1[0-2]’ | tail -5

# 克隆最新稳定版v0.12.0
git clone --depth 1 --branch v0.12.0 \
    https://github.com/neovim/neovim.git \
    ~/neovim

编译

cd ~/neovim
make CMAKE_BUILD_TYPE=Release

编译过程大约需要 3-5 分钟,并会自动下载和编译所有依赖项。

安装

sudo make install

# 验证
nvim --version
# 输出:
# NVIM v0.12.0
# Build type: Release
# LuaJIT 2.1.1774638290

编译输出位置

ls ~/neovim/build/bin/nvim
# -rwxr-xr-x 1 user user 10346568 ... nvim

5. 配置 AstroNvim

AstroNvim 是一个预配置的 Neovim 发行版,提供完整的 IDE 体验。

备份现有配置

# 备份旧配置
mv ~/.config/nvim ~/.config/nvim.bak
mv ~/.local/share/nvim ~/.local/share/nvim.bak
mv ~/.local/state/nvim ~/.local/state/nvim.bak

安装 AstroNvim

# 克隆AstroNvim配置
git clone --depth 1 https://github.com/AstroNvim/AstroNvim \
    ~/.config/nvim

# 复制默认用户配置模板
cp ~/.config/nvim/lua/user_template.lua ~/.config/nvim/lua/user.lua

首次启动

nvim

首次启动会自动安装所有插件和 tree-sitter 解析器。由于我们已经编译了 tree-sitter CLI,treesitter 安装不会出现 glibc 错误。

自定义配置

编辑:~/.config/nvim/lua/user.lua

return {
  -- 自定义颜色方案
  colorscheme = “catppuccin-mocha”,
  
  -- 自定义选项
  options = {
    opt = {
      relativenumber = true, -- 相对行号
    },
  },
  
  -- 自定义插件
  plugins = {
    “rebelot/kanagawa.nvim”,
    -- 添加更多插件...
  },
  
  -- LSP配置
  lsp = {
    formatting = {
      format_on_save = true,
    },
    servers = {
      -- 添加LSP服务器...
    },
  },
}

安装 LSP 和工具

:LspInstall python
:LspInstall lua_ls
:TSInstall python
:TSInstall lua
:MasonInstall prettier

6. 验证 tree-sitter 工作

安装解析器

:TSInstallInfo

查看可用的解析器并选择所需的安装:

:TSInstall vimdoc
:TSInstall lua
:TSInstall python
:TSInstall javascript
:TSInstall html
:TSInstall css
:TSInstall json
:TSInstall markdown

检查状态

:TSInstallInfo

所有解析器都应显示为 . ✓ 已安装

7. 总结

编译检查清单

组件 版本 状态 cmake 3.27.9

✓ 已安装ninja1.11.1

✓ 已安装Rust1.94.1

✓ 已安装tree-sitter CLI0.24.1

✓ 已编译 Neovim0.12.0

✓ 已编译 Astro Nvim 最新版

关键点

  • 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 分支的不稳定性

8. 清理构建文件

编译完成后,可以清理源代码:

# 可选:保留源码用于后续升级
# 或删除以节省空间
rm -rf ~/tree-sitter-cli
rm -rf ~/neovim

建议保留源代码目录,以便后续版本升级。

9. 后续升级

cd ~/neovim
git fetch --tags
git checkout v0.13.0  # 假设新版本
make CMAKE_BUILD_TYPE=Release
sudo make install

升级 tree-sitter:

cd ~/tree-sitter-cli
git fetch --tags
git checkout v0.25.0  # 假设新版本
cargo build --release
sudo cp target/release/tree-sitter /usr/local/bin/
  • Neovim 官方网站:

  • tree-sitter 项目:

https://tree-sitter.github.io

  • AstroNvim 文档:

https://astronvim.com

  • AstroNvim 仓库:https://github.com/AstroNvim/AstroNvim

关于本文的讨论

准备好了解更多了吗?


转载声明:本文为转载整理,原文版权归原作者及原站点所有。

原文来源:Harmony Developers Feed

原创地址:https://www.harmony-developers.com/p/compile-and-install-the-neovim-and

原文作者:Harmony OS Developers;如有侵权,请联系管理员删除。

请登录后发表评论 登录
全部评论

注册时间:2022-08-02

  • 博文量
    36
  • 访问量
    32565