Chenyo's org-static-blog

Posts tagged "emacs":

07 Sep 2024

Install Doom Emacs with Lisp native compilation in WSL2

Today I installed Doom Emacs for my husband on his WSL2. Although the entire process was guided by Claude, there were some back-and-forth during the interaction. Therefore I would like to record the full commands I have used here in sequence for any potential reference.

1. Assumptions

This installation guide assumes a fresh installation of WSL2 Ubuntu 22.04 on Windows 11 in 2024 September.

2. Install prerequisite packages

According to the Doom Emacs documentation, the following packages are recommended:

  • Git 2.23+: this is already installed by default.
  • Emacs 29.4 with Lisp native compilation: this is finicky and will be elaborated later.
  • ripgrep 14.0+: the documentation says 11.0+ suffices, but doom doctor still complains the latest version (13.0) installed from apt is not advanced, so we need to install it from its Github released package later.
  • GNU find: also installed already.
  • fd: sudo apt install fd-find suffices.

3. Install Emacs 29.4

3.1. Before building

First, let’s install some build dependencies:

sudo apt update
sudo apt upgrade  # update packages
sudo apt install build-essential libjansson4 libjansson-dev \
    libgnutls28-dev libtree-sitter-dev libsqlite3-dev
sudo apt install texinfo  # to generate documentation

build-essnetial should install necessary tools to build C/C++ programs such as gcc, g++, make, gdb and dpkg. The rest packages install pre-compiled libraries.

Besides these packages, there are two important packages to support List native compilation:

sudo apt install ibgccjit0 libgccjit-11-dev  # 11 is my gcc version

After installing them, make sure to export the path in the current session, otherwise the compiler will not realize it.

export LD_LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/11:$LD_LIBRARY_PATH

The last thing to do is install a bunch of X and GTK-3 development libraries for Emacs GUI, and another bunch of image processing libraries.

sudo apt install libx11-dev libtiff-dev libgtk-3-dev libncurses-dev
sudo apt install libtiff5-dev libgif-dev libjpeg-dev libpng-dev libxpm-dev

Without the above packages, one may encounter the following error when configuring the Emacs build:

You seem to be running X, but no X development libraries were found. You should install the relevant development files for X and for the toolkit you want, such as Gtk+ or Motif. Also make sure you have development files for image handling, i.e. tiff, gif, jpeg, png and xpm.

3.2. Build Emacs 29.4 with native-comp

At this moment, we can start to download Emacs source code:

wget https://ftp.gnu.org/gnu/emacs/emacs-29.4.tar.xz
tar xvf emacs-29.4.tar.xz
cd emacs-29.4

Then we can configure the build (i.e., generate Makefile) with the following command.

./configure --with-native-compilation --with-x-toolkit=gtk3 --without-pop
  • --with-native-compilation: with this flag the Emacs source code is compiled to native machine code to achieve better performance.
    • Otherwise it is compiled to bytecode and then interpreted by Emacs virtual machine during runtime.
  • --with-x-toolkit=gtk3: this is recommended by Claude.
  • --without-pop: if we are not using Emacs as the email client, we don’t need to bother configure the protocol.

If everything goes well, one should see the following line in the output. If not, make sure libgccjit has been installed and exported.

Does Emacs have native lisp compiler? yes

Now we can finally start compiling the Emacs:

make -j$(nproc)

If some error occurs, we may want to start again, to do this:

sudo apt install autoconf automake
rm -f Makefile
./autogen.sh  # regenerate the configuration file
# then rebuild
make -j$(nproc)

Finally, install Emacs globally:

sudo make install

To confirm the Emacs indeed used the native Lisp compiler, one can evaluate inside the vanilla Emacs with M-: (M is Alt in WSL2):

(native-comp-available-p) ;; should return t

Congratulations! You have now installed the latest and fastest Emacs on WSL2.

4. Install ripgrep

As mentioned in ripgrep documentation, for Debian/Ubuntu users, one should install the latest ripgrep 14.0+ with the following commands.

curl -LO https://github.com/BurntSushi/ripgrep/releases/download/14.1.0/ripgrep_14.1.0-1_amd64.deb  # check the latest version on its documentation
sudo dpkg -i ripgrep_14.1.0-1_amd64.deb  # dpkg has been installed before

Instead, if one installs it with apt, a 13.0+ version is installed and running doom doctor later returns the warning:

The installed ripgrep binary was not built with support for PCRE lookaheads.

5. Install Doom Emacs

Installing Doom Emacs is straightforward, but before that, one should first remove the default Emacs configuration folder:

rm -rf ~/.emacs.d

Then, clone and install Doom Emacs, it could take a while.

git clone --depth 1 https://github.com/doomemacs/doomemacs ~/.config/emacs
~/.config/emacs/bin/doom install

Don’t forget to export ~/.config/emacs/bin to PATH:

echo 'export PATH="$HOME/.config/emacs/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc

Now one can run doom doctor to check any missing dependencies, e.g., shellcheck. One common issue is the Nerd font is not installed by default so that some icons are not properly displayed. To fix that, run M-x nerd-icons-install-font inside the Emacs, then update the font cache with:

fc-cache -fv
# fc-list | grep Nerd  # to verify the font is installed

6. Some issues with running Emacs in WSL2

  • The first thing is I cannot reload the configuration with M-x doom/reload as running this command always gives me the following error message so that I need to restart the Emacs every time the configuration is changed.

    %s sync -B -e /bin/bash: line 1: fg: no job control

  • I really dislike the white border that surrounds any application launched by WSL!
Tags: linux emacs tool
Other posts