April 14th, 2020
How I install Emacs on Linux
Every few years I need to install Emacs on another Linux system and I waste a bunch of time remembering how to do it properly. This time I finally decided to take some notes! I’ve written them up as a blog post in case they’re helpful for other people too.
In the past, I’ve needed to install very recent versions of Emacs, if that’s you, you can install one of the Emacs snapshot packages. These days however, I find that the versions packaged in Debian/Ubuntu are recent enough for my purposes1. For example, at the time of writing, Debian Buster provides 26.1 and Ubuntu Eon provides 26.3. Unless you have a good reason, I recommend just going with the version your Linux distribution provides.
Launching Emacs can take a while if your configuration is of a reasonable size. Luckily though, Emacs has a client/server mode which allows you to have a headless copy of Emacs start as your Emacs server at boot time. Then anytime you open Emacs, you’re really just opening a client which connects to your pre-existing Emacs server. It’s way faster.
Running Emacs like that provides some great flexibility too. For example, I can SSH to my desktop computer, fire up an Emacs client and continue working with all my existing buffers. It also means that if your system’s GUI restarts you don’t lose any work, invaluable!
One thing to note however, the GTK build of Emacs has a nasty problem which means that the Emacs server closes when the system’s GUI restarts. Therefore I recommend going with the Lucid build of Emacs instead. It seems a lot stabler, though a little uglier by default - but we’ll get to that.
So first, let’s install Emacs:
sudo apt install emacs-lucid
Next, we’ll set up the systemd service to ensure that your Emacs server starts when the computer boots:
mkdir -p ~/.config/systemd/user
wget https://raw.githubusercontent.com/kzar/emacs.d/master/emacs.service -O ~/.config/systemd/user/emacs.service
systemctl enable --user emacs
systemctl start --user emacs
When you start Emacs from the application launcher, or by clicking on a file in your file manager, you’ll want it to connect to the existing Emacs server instead of starting whole new copy of Emacs. We can ensure that by installing our own emacs.desktop file:
mkdir ~/.local/share/applications
wget https://raw.githubusercontent.com/kzar/emacs.d/master/emacs.desktop -O ~/.local/share/applications/emacs.desktop
If you need to connect to your Emacs server from the console, you can do so by typing this:
emacsclient -t
Some terminal commands, for example git config --edit
, will launch your
default editor. Let’s make sure they make use of the existing Emacs server too:
echo -e "\nexport EDITOR=\"emacsclient -c\"" >> ~/.bashrc
export EDITOR="emacsclient -c"
Next we need to configure Emacs to make the Lucid build look less ugly. I found that by disabling the toolbars and increasing the font size it looks identical to the GTK build. Add this to your Emacs configuration:
(defun first-frame-opened (frame)
;; Hide frame toolbars
(menu-bar-mode -1)
(tool-bar-mode -1)
(scroll-bar-mode -1)
;; Tidy up the hook.
(remove-hook 'after-make-frame-functions 'first-frame-opened))
(add-hook 'after-make-frame-functions 'first-frame-opened)
; Include column number in the mode line.
(setq column-number-mode t)
; Set the correct font size.
(set-face-attribute 'default nil :height 110)
You’ll need to get used to looking at the mode line to see your position in a buffer instead of the scrollbars. I found that a little weird initially, but after I adapted I found that it saved screen estate and was less visually distracting.
By default, text which you highlight wont be “yankable”. For example, if you
highlight some text in your web browser, then hit C-y
in Emacs the highlighted
text won’t be pasted. That can be fixed by adding the following to your Emacs
configuration:
; Make highlighted text "yankable".
(setq x-select-enable-primary t)
Emacs using some different key-bindings than the Windows etc norm. For example,
C-a
moves to the start of the line instead of selecting all. To make the
key-bindings a bit more consistent, you can use the Emacs GTK key theme:
gsettings set org.gnome.desktop.interface gtk-key-theme "Emacs"
Or, if you’re using XFCE:
xfconf-query -c xsettings -p /Gtk/KeyThemeName -s Emacs
I also found that by default special Unicode characters (e.g. 😎) aren’t rendered properly. This can be fixed with help from the Symbola font.
First, install the Symbola font and restart the Emacs server:
sudo apt install fonts-symbola
systemctl restart --user emacs
Then add this to your Emacs configuration:
; Have special Unicode characters render with the Symbola font.
(when (member "Symbola" (font-family-list))
(set-fontset-font t 'unicode "Symbola" nil 'prepend))
Now special characters in Emacs buffers should be displayed properly.
If you’re using a desktop computer, I recommend enabling Desktop-Save mode. That will maintain any file buffers that you have open between power cycles. It is especially useful when working on larger codebases. Without Desktop-Save mode, I often found myself considering the trade-off between finding & opening all the relevant files again tomorrow or leaving the computer on overnight and wasting electricity.
;; desktop-mode appears to have a bug when used in combination with Emacs
;; server. As a workaround, we force loading the Desktop state, even when
;; it is considered locked.
(setq desktop-load-locked-desktop t)
;; Use desktop-mode to maintain open buffers. This means I can shut down the
;; computer more often to save power.
(desktop-save-mode 1)
(desktop-read)
To get spell checking to work, I recommend installing Hunspell and a language pack before restarting the Emacs server:
sudo apt install hunspell hunspell-en-gb
systemctl restart --user emacs
Unless you also use British English, replace hunspell-en-gb
with the language
pack of your choice.
And finally, add the corresponding Emacs lisp to your configuration (again
you’ll need to replace "en_GB"
with the appropriate language code):
; Set up the Hunspell spell-checker.
(setq ispell-program-name "hunspell"
flyspell-issue-welcome-flag nil
ispell-local-dictionary "en_GB"
ispell-local-dictionary-alist
'(("en_GB" "[[:alpha:]]" "[^[:alpha:]]" "[']" nil ("-d" "en_GB") nil utf-8)))
Now you should be good to go, try it out with M-x flyspell-mode
.
Of course, configuring Emacs once it’s installed is a whole other topic! There’s no way to cover that in a blog post, but if you’re curious to take a look my Emacs configuration is online.
Notes
-
One thing that it might be worth using a snapshot build for is colour font support. For example, Ubuntu 20.04 comes with the
fonts-noto-color-emoji
package with great looking colour emojis. If you’d like to try using a snapshot build of Emacs, I recommend going for the Debian APT repository, even if you’re running Ubuntu. That includes theemacs-snapshot-lucid
package, unlike the Ubuntu repository. If you are installing it on Ubuntu, you might need to install thelibjpeg62-turbo
Debian package manually first. Once you have a snapshot build running, you can replace"Symbola"
with"Noto Color Emoji"
in your Emacs configuration. ↩︎