Compiling Emacs

Published August 26, 2024 · 496 words · 3 minute read

Emacs 29 brings built-in SQLite support that is superior to the community developed packages I’ve tried. Debian’s latest Emacs binary is version 28, which does not have this feature. I’d also like to turn on a few additional features like tree-sitter1. This article was adapted and expanded from this one - but I found it made a few mistakes and was missing a few details.

To start, take a deep breath and remove all Emacs packages from your system. autoremove will clean up any stray packages.

sudo apt remove emacs emacs-bin-common emacs-common \
                emacs23 emacs24 emacs25 emacs26 \
                emacs-el

sudo apt autoremove

Let’s also clean up our Emacs directory while we’re at it. While the compiled .elc files should work with an updated version of Emacs, it’s nice to recompile everything from scratch.

cd ~/.emacs.d/
rm -rf elpa
rm -rf straight

Next, install build dependencies:

sudo apt-get install git build-essential autoconf make \
  texinfo gnutls-bin libgccjit-12-dev gcc libgtk2.0-dev \
  libgnutls28-dev libxpm-dev libgif-dev gtk+-3.0 \
  libwebkitgtk-6.0-dev libgtk-4-dev librsvg2-dev \
  libwebkit2gtk-4.1-dev

To keep organized, create a source directory to keep all of this work in.

mkdir source
cd source

Tree-sitter enables a variety of benefits - clone, make, and install it.

cd ~/source/
git clone https://github.com/tree-sitter/tree-sitter.git
cd tree-sitter
make
sudo make install

Add the following line in /etc/environment for the build:

LD_LIBRARY_PATH=/usr/local/lib/

…make sure you log out and log back in to make this variable available.

Finally, we arrive at the interesting bit - actually cloning and installing Emacs from source. Cloning and building can take anywhere from a few minutes to fifteen, depending on the speed and age of your machine. For starters, clone the repository and run the autogen script.

cd ~/source/
git clone -b emacs-29 git://git.sv.gnu.org/emacs.git
cd emacs
git clean -f -d -x  # run if you changed branches
./autogen.sh

To configure my installation of Emacs I use the following flags. Your mileage may vary, and you may need a slightly different set for your system:

./configure --with-tree-sitter --with-native-compilation \
            --with-modules --with-mailutils --with-pop \
            --with-rsvg --with-xwidgets
  • If you are on Wayland, also add --with-pgtk
  • If you are on X11, also add --with-x
  • If you don’t know, run echo $XDG_SESSION_TYPE to find out

The final steps - compile and install Emacs 29:

make
sudo make install

Emacs should now be compiled and installed! You may need to log out and back in for your window manager to identify the launch icons.

Once you have launched Emacs and perhaps used the --debug-init flag to fix any problems in your configuration, tree-sitter can be set up by adding the following to your configs:

  (setq treesit-language-source-alist
   '(
     (bash "https://github.com/tree-sitter/tree-sitter-bash")
     (cmake "https://github.com/uyha/tree-sitter-cmake")
     (c "https://github.com/tree-sitter/tree-sitter-c")
     (elisp "https://github.com/Wilfred/tree-sitter-elisp")
     (html "https://github.com/tree-sitter/tree-sitter-html")
     (javascript "https://github.com/tree-sitter/tree-sitter-javascript" "master" "src")
     (json "https://github.com/tree-sitter/tree-sitter-json")
     (make "https://github.com/alemuller/tree-sitter-make")
     (markdown "https://github.com/ikatyang/tree-sitter-markdown")
     (python "https://github.com/tree-sitter/tree-sitter-python")))

Now in a scratch buffer in emacs run the following:

(mapcar #'treesit-install-language-grammar 
    '(bash cmake c elisp html javascript json make markdown python))

That’s all for now. Enjoy your speedy compiled copy of Emacs!

Comments