Building Emacs from source on Ubuntu 20.04 in 2020

Using the package manager in Ubuntu to install Emacs will result in some older version (version 26.3 on Ubuntu MATE 20.04 (Focal Fossa)). One way to get the latest version, currently 27.2 for the stable version, is to compile from source.

Most blog posts and Stack Exchange answers about this are out of date. The versions of the required packages to install in order for the compile to succeed are wrong and the set of packages is different.

The main part of this blog post is step-by-step instructions on how to compile the latest version of Emacs from source. The gist is to install the latest (or available) development edition (suffix “-dev” in the package name) of the dependencies for Emacs. Development here means a package includes the header files (.h, etc.) – required for the build from source (that is, they are not pre-alpha versions (unstable/buggy)).

But it also covers all the error messages one will run into if following the instructions on the official Emacs website (they do not work at all on Ubuntu – or rather there are a lot of required prerequisites). Or it can be useful for getting this to work in the future, including new versions of Ubuntu or other Linux distributions.

How-to

In Bash (terminal window):

1. Get the source code (the work folder is off in sub folder ‘temp2/2020-11-10’ from the current user’s home folder):

mkdir ~/temp2
mkdir ~/temp2/2020-11-10
cd ~/temp2/2020-11-10
wget http://ftp.download-by.net/gnu/gnu/emacs/emacs-27.1.tar.gz
wget http://ftp.download-by.net/gnu/gnu/emacs/emacs-27.1.tar.gz.sig
tar -zxvf emacs-27.1.tar.gz

2. Install package ‘libtiff-dev’ first (separately from the following installs):

sudo apt-get install libtiff-dev

3. Next, install more packages and compile Emacs

sudo apt-get install build-essential
sudo apt-get install libgtk-3-dev libtiff5-dev libgif-dev libjpeg-dev libpng-dev libxpm-dev libncurses-dev
cd ~/temp2/2020-11-10/emacs-27.1
./configure --with-gnutls=ifavailable
make

At this point, Emacs can be launched (this will open a GUI application):

./src/emacs

To open Emacs in terminal mode (“-nw” is the short form for the option “–no-window-system”):

./src/emacs -nw

Exit by Ctrl + X, Ctrl + C (or F10, arrow up, and Enter to invoke the menu, select the last menu command in the first menu (“Quit”)).

It can now be installed (optional):

sudo make install

After this, it will launch the new version (27.1) anywhere on the command line (but the one in the GUI system (e.g., Win + “emacs”) will still list the old version). Note: Unless extra steps are taken, this does not work in the current terminal session, but it does work if a new terminal window is opened.

Integrate Emacs into Ubuntu

The install action will not make the compiled version turn up in the GUI, for example, when hitting the Windows key and typing “Emacs”. Here is one way to achieve that (presuming the standard (older) Emacs is already installed).

First, find the .desktop file for the existing installed (base) Emacs. E.g.,

ls -ls /usr/share/applications | grep -i emacs

It may be “/usr/local/share/applications” instead of “/usr/share/applications”.

Output may include:

emacs25.desktop

Second, clone and edit the .desktop file. For example,

sudo cp /usr/share/applications/emacs25.desktop /usr/share/applications/emacsclient27.1.desktop
ls -ls /usr/share/applications/emacs27.1.desktop
sudo vi /usr/share/applications/emacs27.1.desktop

For field “Name”, make the first part specific as only a limited number of characters may be shown when using the Windows key in Ubuntu. The existing ones already start with “Emacs” or “GNU Emacs” and are hard to distinguish from each other. For example, use “Name=Self-compiled Emacs 28.1 (GUI)” or “Name=V. 27.1 of Emacs”.

For field “Exec”, use “Exec=/usr/bin/emacs %F” (the existing one may be “Exec=/usr/bin/emacs25 %F”).

Casual use of Emacs

Use as a secondary text editor to see two parts of a document at the same time

For instance, on a two-monitor system, Emacs on one screen and Geany on the other. This is useful as Geany is not that great with more than one view per document (the split screen functionality is essentially completely broken).

Note that some of the menu commands (and the associated keyboard shortcuts) can be discovered through the menus. Use F10 to activate them and browse through them. Use Esc to cancel.

Menu Buffers*scratch* (a mouse is not required—use F10 and the arrow keys to navigate to it).

Paste

Emacs, by default, uses the nonstandard keyboard shortcut Ctrl + Y to paste into a document from the system clipboard (the standard is Ctrl + V).

Select

Ctrl + Space to start. Move the cursor to the end of the selection (for example, by the arrow keys). The selection is invisible!

Alternative (similar to most other text editors): Hold down Shift while moving the cursor to the end of the selection by the arrow keys. The selection is invisible!

Select all

Ctrl + X, H. It also moves the cursor to the beginning of the document. The selection is invisible!

Select a block (column mode)

Ctrl + X, Space to start. Move the cursor to the end of the block/column selection (for example, by the arrow keys). The selection is invisible!

Block/column mode has been supported since approximately Emacs 24 (Linux distribution support since approximately 2015).

Copy

Emacs, by default, uses the nonstandard keyboard shortcut Alt + W (not Ctrl + W!) to copy to the system clipboard (the standard is Ctrl + C).

It is also nonstandard in that it removes the selection after copy.

Undo

Ctrl + X, U.

Word wrap

The default is to wrap words for long lines. This is not normally desirable.

To change it for the current document (will not be permanent): Alt + X toggle-truncate-lines (tab completion works – there isn’t any need to type all of “toggle-truncate-lines”. The minimum is “tog” + TAB + “tr” + TAB).

To change it permanently:

vi ~/.emacs

Add this line (yes, only one single quote):

(set-default 'truncate-lines t)

Search in the current document

Emacs, by default, uses the nonstandard keyboard shortcut Ctrl + S to search in the current document (the standard is Ctrl + F). Use Ctrl + R to search backward.

A subsequent Ctrl + S works as search next (the standard is F3).

Use Alt + C to toggle case-sensitivity.

Go to a particular line, say 71899

Use Alt + G, Alt + G (yes, two times Alt. Or Alt can be held down, so it is Alt + G + G). It will prompt with “Goto line: “. Then 71899 and press Enter.

Increase font size

Use F10 → menu “Options”“Set Default Font…”. Increase the default “13”.

Or Ctrl + X + +. Or, perhaps depending on the system it is on, just Ctrl + +.

Save to a file

Use F10 → menu “File”“Save As…” (or Ctrl + X, Ctrl + W). Select the folder by pressing TAB two times (optionally first delete the “~/”). For a direct selection, switch between the sub panes by Ctrl + X, O. This selection must be done for each sub folder level (it is not possible to navigate to the sub folder in one go). Otherwise, TAB completion often works better.

Normal save (if working on an existing file) is Ctrl + X, Ctrl + S.

Press Ctrl + G to cancel the process (there may be a slight delay of 2-3 seconds).

Load a file

If the file path is in the clipboard: Ctrl + X, Ctrl + F. Delete the prefix and paste in the path using Ctrl + Y. Press Enter.

Example path: ~/.config/geany/plugins/Geany_Numbered_Bookmarks/settings.conf

Switch between documents (“buffers” in Emacs lingo)

Use Ctrl + X, <right arrow key> (or Ctrl + X, <left arrow key>)

A slower way is using the “Buffers” menu: F10 * → * choose menu “Buffers” (with the arrow keys).

See also

For a similar treatment for using Emacs, see also my previous blog post Emacs basics (2017). For instance, it covers creating/locating the settings file.

Appendix A Error messages and troubleshooting

While the main part of this post is focused on getting Emacs compiled as quickly as possible, this appendix documents the error messages that you may run into.

‘apt-get’ blues

Search in the list of all packages (not just the installed ones) to get the package names for the available development versions of libtiff:

apt-cache search libtiff | grep -i dev

Result:

libtiff-dev - Tag Image File Format library (TIFF), development files
libtiff5-dev - Tag Image File Format library (TIFF), development files (transitional package)

Appendix B Cheat sheets

The most succinct Emacs cheat sheet is probably “GNU Emacs Survival Card“.

Leave a Reply

Your email address will not be published. Required fields are marked *

*