虚無庵

徒然なるままに

elementary OS 6 Odin(Ubuntu 20.04) に Emacs 28.0 をソースビルドでインストールする

未来の自分への備忘録、そして見知らぬ誰かへの参考情報

GNU Emacs の公式サイトでは apt-get を使ったインストールを記載しているが、これだと 2021/11/19 時点では Emacs 26.3 までしかインストールされない。macOS のバイナリーは最新版である 27.2-3 で公開されているし、Linux でも最新版を使いたかったから最新版をインストールする方法を調べた。しかし、いくらぐぐってもぐぐり方が悪いのか微妙に古い情報しか出てこないので、自力でソースビルドしてみることにした。で、どうせ忘れるだろうから備忘録として残しておくことにした。

Emacsソースコードを git clone してくる。

GitHub 上だとここにミラーリポジトリがある。本家の方は git://git.sv.gnu.org/emacs.git なので好きな方から git clone したらいい。

$ git clone git@github.com:emacs-mirror/emacs.git emacs

あとは INSTALL に従うだけ…のはずが、世の中そんなに甘くない

インストール手順は INSTALL に書いてあるので、それに従えばできるはず。なのだが、世の中そんなに甘くない。GitHub 上の BASIC INSTALLATION では emacs-VERSION.tar.xz を wget してきているが、 git clone しているので少し手順が違う*1

$ cd <yourpath>/emacs
$ git fetch origin emacs-28
$ ./autogen.sh
$ ./configure --with-x-toolkit=gtk3 --with-xpm=ifavailable --with-jpeg=ifavailable --with-gif=ifavailable --with-tiff=ifavailable --with-gnutls=ifavailable
$ make
$ src/emacs -Q
$ make check
$ sudo make install
$ make clean

必要なライブラリが既に揃っているならばこれで大丈夫なはず*2。個人的にハマったところを以下に紹介する。

X toolkit が見つからないと怒られる

$ ./configure
(中略)
checking for libXaw... configure: error: No X toolkit could be found.
If you are sure you want Emacs compiled without an X toolkit, pass
  --with-x-toolkit=no
to configure.  Otherwise, install the development libraries for the toolkit
that you want to use (e.g. Gtk+) and re-run configure.

X toolkit が見つからないと怒られた。--with-x-toolkit=no オプションを付けたら無視できそうに思えたのでこのオプションを付けて実行してみた。

$ ./configure --with-x-toolkit=no
(中略)
checking for gif_lib.h... no
configure: error: The following required libraries were not found:
    libXpm libjpeg libpng libgif/libungif libtiff gnutls
Maybe some development libraries/packages are missing?
To build anyway, give:
    --with-xpm=ifavailable --with-jpeg=ifavailable --with-png=ifavailable --with-gif=ifavailable --with-tiff=ifavailable --with-gnutls=ifavailable
as options to configure.

で、画像を表示するためのライブラリがなさそうに思えたので「やっぱり X toolkit って必要なんじゃね?」と思った*3。なので再度 --with-w-toolkit を指定して実行してみることにした。

GTK が見つからないと怒られる

$ ./configure
(中略)
checking for libXaw... configure: error: No X toolkit could be found.
If you are sure you want Emacs compiled without an X toolkit, pass
  --with-x-toolkit=no
to configure.  Otherwise, install the development libraries for the toolkit
that you want to use (e.g. Gtk+) and re-run configure.

再掲。libXaw なるものをチェックして「X toolkit を見つけられない」と言っているように読めるので、本当に libXaw なるものがインストールされていないのか確認した。

$ apt search libxaw
ソート中... 完了
全文検索... 完了  
libxaw-doc/focal 2:1.0.13-1 all
  X11 Athena Widget library (documentation)

libxaw3dxft6/focal 2.9.1.4-3.2 amd64
  An extended version of Xaw3d with support for UTF8

libxaw3dxft8/focal 1.6.2e-2 amd64
  Extended version of Xaw3d with support for UTF8

libxaw3dxft8-dev/focal 1.6.2e-2 amd64
  Extended version of Xaw3d with support for UTF8 (Development files)

libxaw7/focal,now 2:1.0.13-1 amd64 [インストール済み、自動]
  X11 Athena Widget library

libxaw7-dbg/focal 2:1.0.13-1 amd64
  X11 Athena Widget library (debug package)

libxaw7-dev/focal 2:1.0.13-1 amd64
  X11 Athena Widget library (development headers)

xaw3dg/focal 1.5+E-18.3 amd64
  Xaw3d ウィジェットセット

xpaint-dev/focal 2.9.1.4-3.2 amd64
  X 用のシンプルなペイントプログラム (開発用ファイル)

libxaw7 なるものがインストール済みなのでこれを --with-w-toolkit に指定したら行けそうな気がしたので試してみた。

$ ./configure --with-x-toolkit=libxaw7
(中略)
configure: error: '--with-x-toolkit=libxaw7' is invalid;
this option's value should be 'yes', 'no', 'lucid', 'athena', 'motif', 'gtk',
'gtk2' or 'gtk3'.  'yes' and 'gtk' are synonyms.
'athena' and 'lucid' are synonyms.

指定できる値は決まっているらしい。一体どれを指定したら良いのか分からなかったのでぐぐる

kaniza.hatenablog.com

こちらのサイトを参考にさせてもらった。

オプションを渡さなければ、GTK3 が使われる。--with-x-toolkit=gtk3 相当ですな。Ubuntu のデスクトップ環境で指定しているテーマが適用される。

なので恐らく --with-w-toolkit=gtk3 で俺が欲しいものが作られそうな気がしたので採用した。

$ ./configure --with-x-toolkit=gtk3
(中略)
checking for gtk+-3.0 >= 3.10 glib-2.0 >= 2.37.5... no
configure: error: No package 'gtk+-3.0' found
No package 'glib-2.0' found

エラーメッセージが変わった。どうやら gtk+-3.0 と glib-2.0 なるものが見つからないらしい。apt search で確認する。

$ apt search gtk+-3.0
ソート中... 完了
全文検索... 完了  
gir1.2-gtk-3.0/focal-updates,now 3.24.20-0ubuntu1 amd64 [インストール済み、自動]
  GTK graphical user interface library -- gir bindings

gir1.2-spiceclientgtk-3.0/focal 0.37-2fakesync1 amd64
  GTK3 widget for SPICE clients (GObject-Introspection)

gnome-accessibility-themes/focal 3.28-1ubuntu1 all
  High Contrast GTK+ 2 theme and icons

gnome-themes-extra/focal 3.28-1ubuntu1 amd64
  Adwaita GTK+ 2 theme — engine

gnome-themes-extra-data/focal 3.28-1ubuntu1 all
  Adwaita GTK+ 2 theme — common files

libgtk-3-0/focal-updates,now 3.24.20-0ubuntu1 amd64 [インストール済み、自動]
  GTK graphical user interface library

libspice-client-gtk-3.0-5/focal 0.37-2fakesync1 amd64
  GTK3 widget for SPICE clients (runtime library)

libspice-client-gtk-3.0-dev/focal 0.37-2fakesync1 amd64
  GTK3 widget for SPICE clients (development files)

libgtk-3-0 なるものがインストールされてそう。

$ apt search glib-2.0
ソート中... 完了
全文検索... 完了  
gir1.2-glib-2.0/focal-updates,now 1.64.1-1~ubuntu20.04.1 amd64 [インストール済み]
  GLib、GObject、Gio、GModule 用 Introspection データ

gir1.2-spiceclientglib-2.0/focal 0.37-2fakesync1 amd64
  Spice サーバと通信する GObject (GObject イントロスペクション)

libqt5glib-2.0-0/focal 1.2.0-5 amd64
  C++ bindings library for GLib and GObject with a Qt-style API - Qt 5 build

librust-glib-sys-dev/focal 0.9.0-3 amd64
  FFI bindings to libglib-2.0 - Rust source code

libspice-client-glib-2.0-8/focal 0.37-2fakesync1 amd64
  GObject for communicating with Spice servers (runtime library)

libspice-client-glib-2.0-dev/focal 0.37-2fakesync1 amd64
  GObject for communicating with Spice servers (development files)

gir1.2-glib-2.0 なるものがインストールされてそう。

つまるところ gtk+-3.0 と glib-2.0 はインストールされていると思っていた。ここがハマりポイントでめっちゃハマった。

apt-file コマンドを使う

kobapan.hatenadiary.org

どこかで makeinfo が見つからなくて手動でインストールする*4ためにいろいろ調べてたらこのサイトに行き着いた。

$ sudo apt install apt-file
$ sudo apt-file search gtk+-3.0
libgtk-3-dev: /usr/lib/x86_64-linux-gnu/pkgconfig/gtk+-3.0.pc
valac-0.48-vapi: /usr/share/vala-0.48/vapi/gtk+-3.0.deps
valac-0.48-vapi: /usr/share/vala-0.48/vapi/gtk+-3.0.vapi

どうやらインストールされていると思っていた gtk+-3.0 はインストールされていなかったようだ。

$ sudo apt install libgtk-3-dev

これをやってようやく先に進めた。

JPEG とか GIF を表示してくれそうなライブラリが見つからないと怒られる

$ ./configure --with-x-toolkit=gtk3
(中略)
checking for gif_lib.h... no
configure: error: The following required libraries were not found:
    libXpm libjpeg libgif/libungif libtiff gnutls
Maybe some development libraries/packages are missing?
To build anyway, give:
    --with-xpm=ifavailable --with-jpeg=ifavailable --with-gif=ifavailable --with-tiff=ifavailable --with-gnutls=ifavailable
as options to configure.

libXpm libjpeg libgif/libungif libtiff gnutls

こいつらが見つからないらしい。

Maybe some development libraries/packages are missing?

こうおっしゃっているしこれらのオプションをつけて実行してみよう。

$ ./configure --with-x-toolkit=gtk3 --with-xpm=ifavailable --with-jpeg=ifavailable --with-gif=ifavailable --with-tiff=ifavailable --with-gnutls=ifavailable

なんか行けたっぽい。

まとめ

以前 macOS が Big Sur くらいになったあたりで Downloads ディレクトリなどファイルアクセス権限をもらわないといけないディレクトリに Emacs からアクセスできなくなった。それを気軽に ruby-jp の emacs チャンネルで

「このアクセスできないやつ、みんなどうやって解決してるんですか?」

と聞いたら、ほとんどの人の回答が

「ソースビルドでインストールしているから、その現象に遭遇したことがない」

というものだった。Stable 版のパッケージが反映されるのも時差があるので、最新版を早く使いたければソースビルドが正しいのだろう。

勢い余って 28.0 をインストールしたが、そこはご愛嬌で。

あと ./configure --help を実行するとオプションの説明がちゃんと書かれているからちゃんと読もう。

*1:最初は *.tar.gz ファイルをダウンロードしてやったが、すでに configure ファイルが作成済みだったので「何か依存関係無視して作られているかも」と思って git clone する方を選択した。試していないが多分関係ないと思う。

*2:src/emacs -Q, make check, make clean は省略可

*3:有識者ならわかるだろうが、この考えは誤り

*4:history に何も残っていない