DEV Community

Yawata Yahaha
Yawata Yahaha

Posted on • Originally published at en.deeprouter.org

Using Neovim on OpenWRT

😀 Because I often need to debug my home network via SSH, including the configuration of the main router, the configuration of the side router (side gateway), OpenClash, Adguard Home, MosDNS, etc., I frequently need a comfortable editor to modify json, yaml, and xml configuration files or structured data files. In my local environment, I use NeoVim more often, and I have previously shared articles about LazyVim. Currently, there is no ipk for this software in the OpenWRT system software repository, so today I will organize the process of compiling and installing NeoVim on OpenWRT and the pitfalls I encountered.

📝 Compile Neovim On OpenWRT

Before You Begin Before You Begin

To avoid uncontrollable problems during the compilation and installation process, I use a brand new virtual machine environment for this tutorial. The firmware is from ImmortalWrt Firmware, Generic x86/64 architecture, version number 23.05.3, kernel version 5.15.162, without any additional pre-installed software packages, consistent with the default provided software packages, and the available space is as large as possible. The actual space I used for this operation is about 1.2G, but it can be removed after the installation is completed.

Also, before starting the following operations, if you are using an OpenWRT system, it is recommended to make a backup. If it is a virtual machine system, it is recommended to add a checkpoint or backup point.

Compilation and Installation Process Begins Compilation and Installation Process Begins

If you don't want to see the process, you can directly look at the summary.

First, update the software repository source through OPKG package manager.

opkg update
Enter fullscreen mode Exit fullscreen mode

The result returned is Signature check passed. and there are no errors, continue to the next step.

Since we need to clone the Neovim repository on GitHub via git, we need these two software packages, where git-http is used for using git in SSL situations.

opkg install git git-http
Enter fullscreen mode Exit fullscreen mode

During this period, including dependencies, a total of git, git-http, zlib, libcurl4, libnghttp2-14 software packages were installed.

Clone the Neovim repository via git clone and try to execute make.

git clone https://github.com/neovim/neovim
cd neovim && make CMAKE_BUILD_TYPE=RelWithDebInfo
Enter fullscreen mode Exit fullscreen mode

The error result returned is:

make not found
Enter fullscreen mode Exit fullscreen mode

The reason is that we do not have the make software package. Install it through OPKG and try make again.

opkg install make
make CMAKE_BUILD_TYPE=RelWithDebInfo
Enter fullscreen mode Exit fullscreen mode

The error result returned is:

make: cmake: No such file or directory
make: *** [Makefile:94: build/.ran-deps-cmake] Error 127
Enter fullscreen mode Exit fullscreen mode

It indicates that cmake does not exist. OpenWrt does not provide an installation file for cmake, but we can install it indirectly through python-pip, and the mainstream Python version is currently Python3.

opkg install python3 python3-pip
pip install cmake
Enter fullscreen mode Exit fullscreen mode

After trying make again, the error message changes:

- The C compiler identification is unknown
CMake Error at CMakeLists.txt:3 (project):
No CMAKE_C_COMPILER could be found.
Tell CMake where to find the compiler by setting either the environment
variable "CC" or the CMake cache entry CMAKE_C_COMPILER to the full path to
the compiler, or to the compiler name if it is in the PATH.
- Configuring incomplete, errors occurred!
make: *** [Makefile:94: build/.ran-deps-cmake] Error 1
Enter fullscreen mode Exit fullscreen mode

It means that the C compiler was not found. We can install gcc to solve this.

opkg install gcc
make CMAKE_BUILD_TYPE=RelWithDebInfo
Enter fullscreen mode Exit fullscreen mode

This compilation took a long time, until an error occurred during the installation of luajit:

compilation terminated.
make[5]: *** [Makefile:709: lj_parse.o] Error 1
make[4]: *** [Makefile:126: src/luajit] Error 2
make[3]: *** [CMakeFiles/luajit.dir/build.make:106: build/src/luajit-stamp/luajit-install] Error 2
make[2]: *** [CMakeFiles/Makefile2:161: CMakeFiles/luajit.dir/all] Error 2
make[1]: *** [Makefile:91: all] Error 2
make[1]: Leaving directory '/root/neovim/.deps'
make: *** [Makefile:87: deps] Error 2
Enter fullscreen mode Exit fullscreen mode

We can directly install it through opkg.

opkg install luajit
Enter fullscreen mode Exit fullscreen mode

Then continue to try executing make, and this time the error changes:

/usr/bin/ld: cannot find -ldl: No such file or directory
Enter fullscreen mode Exit fullscreen mode

It indicates that the ld command does not have the ldl option. By checking the OpenWrt documentation, it can be found that the functions in these libraries are actually directly included in the musl libc. However, since some tools try to explicitly link these libraries, you may encounter a "No Such file or directory" error. To solve this problem, just create stub libraries for them.

Use Vi to create a sh file vi run.sh, and enter the following content in the file.

cat <<EOF > /usr/lib/libdl.a
!<arch>
EOF
cp -a /usr/lib/libdl.a /usr/lib/librt.a
cp -a /usr/lib/libdl.a /usr/lib/libpthread.a
cp -a /usr/lib/libdl.a /usr/lib/libresolv.a
Enter fullscreen mode Exit fullscreen mode
chmod +x run.sh
./run.sh
Enter fullscreen mode Exit fullscreen mode

Then continue to try make CMAKE_BUILD_TYPE=RelWithDebInfo, and this time the error content changes to:

/bin/sh: install: not found
Enter fullscreen mode Exit fullscreen mode

It indicates that the install command is not found. The solution to this is install the full package of install command:

opkg install coreutils-install
Enter fullscreen mode Exit fullscreen mode

The error content changes to:

CMake Error at /usr/lib/python3.11/site-packages/cmake/data/share/cmake-3.30/Modules/FindPackageHandleStandardArgs.cmake:233 (message):
Could NOT find Gettext (missing: GETTEXT_MSGMERGE_EXECUTABLE
GETTEXT_MSGFMT_EXECUTABLE)
Call Stack (most recent call first):
/usr/lib/python3.11/site-packages/cmake/data/share/cmake-3.30/Modules/FindPackageHandleStandardArgs.cmake:603 (_FPHSA_FAILURE_MESSAGE)
/usr/lib/python3.11/site-packages/cmake/data/share/cmake-3.30/Modules/FindGettext.cmake:81 (FIND_PACKAGE_HANDLE_STANDARD_ARGS)
src/nvim/po/CMakeLists.txt:1 (find_package)
Enter fullscreen mode Exit fullscreen mode

It indicates that Gettext was not found. There is currently no quick installation method for Gettext packages, and we need to install it by compiling.

wget -c https://ftp.gnu.org/pub/gnu/gettext/gettext-0.22.5.tar.gz
tar zxvf gettext-0.22.5.tar.gz
cd gettext-0.22.5
./configure
make && make install
Enter fullscreen mode Exit fullscreen mode

Then continue to try compiling neovim, and the error returned is:

/usr/bin/ld: cannot find -lutil: No such file or directory
Enter fullscreen mode Exit fullscreen mode

We can bypass it by creating stub libraries:

echo "int main() { return 0; }" > dummy.c
gcc -shared -o /usr/lib/libutil.so dummy.c
Enter fullscreen mode Exit fullscreen mode

At this point, the compilation is basically complete, and we can proceed with the installation.

make install
Enter fullscreen mode Exit fullscreen mode

The binary executable file is located in /usr/local/bin, and we can directly move it to /usr/bin or create a soft link there. After that, if you want to install Astrovim or Lazyvim, you can directly execute it.

🤗 Summary and Conclusion

OPKG Install Dependencies OPKG Install Dependencies

opkg update
opkg install git git-http python3 python3-pip make luajit gcc coreutils-install
Enter fullscreen mode Exit fullscreen mode

PIP Install Dependencies PIP Install Dependencies

pip install cmake 
Enter fullscreen mode Exit fullscreen mode

Compile and Install Gettext Compile and Install Gettext

wget -c https://ftp.gnu.org/pub/gnu/gettext/gettext-0.22.5.tar.gz
tar zxvf gettext-0.22.5.tar.gz
cd gettext-0.22.5
./configure
make && make install
Enter fullscreen mode Exit fullscreen mode

Create Stub Libraries Create Stub Libraries

vi run.sh
Enter fullscreen mode Exit fullscreen mode
cat <<EOF > /usr/lib/libdl.a
!<arch>
EOF
Enter fullscreen mode Exit fullscreen mode
cp -a /usr/lib/libdl.a /usr/lib/librt.a
cp -a /usr/lib/libdl.a /usr/lib/libpthread.a
cp -a /usr/lib/libdl.a /usr/lib/libresolv.a
chmod +x run.sh
./run.sh
Enter fullscreen mode Exit fullscreen mode
echo "int main() { return 0; }" > dummy.c
gcc -shared -o /usr/lib/libutil.so dummy.c
Enter fullscreen mode Exit fullscreen mode

Clone Neovim Repository and Compile and Install Clone Neovim Repository and Compile and Install

git clone https://github.com/neovim/neovim
cd neovim && make CMAKE_BUILD_TYPE=RelWithDebInfo
make install
Enter fullscreen mode Exit fullscreen mode

After completion, the nvim binary file is located in /usr/local/bin.If you need the stable version of Neovim, modify CMAKE_BUILD_TYPE=Release

Directly Download Binary File Directly Download Binary File

If you do not want to compile it yourself, and you happen to have an X86_64 architecture OpenWRT, you can also directly download the binary file I compiled for use.

init.vim/neovim.7z at main · moreoronce/init.vim

📎 Reference

💡 If you have any questions about the installation or use of OpenWrt, please feel free to leave a comment at the bottom, and let's communicate together~

Top comments (0)