Skip to content
Tutorials Center
My learning

Topics

Lesson 24 of 38 ยท Packages and Programming Modes

Emacs Package Management: package.el, MELPA and use-package

Install Emacs packages from GNU ELPA and MELPA, understand archives and dependencies, and organize your configuration with the built-in use-package macro.

Emacs ships with a lot, but most users extend it with packages: bundles of Lisp files that add modes, commands and themes. Since Emacs 24 a package manager, package.el, is built in. In this lesson you configure archives, install packages, and use use-package (built in since Emacs 29) to keep each package's settings together and fast to load.

The Lisp snippets are real. Because Emacs cannot run on this page, the runnable blocks are labelled JavaScript stand-ins that model dependency resolution and macro expansion.

Archives

A package archive is a web location that serves package lists. The defaults you get are:

  • GNU ELPA: packages that are part of the GNU project and carry copyright assignment. Enabled by default.
  • NonGNU ELPA: packages from the Emacs project with other licenses. Enabled by default since Emacs 28.
  • MELPA (melpa.org): a huge community archive built from the latest Git commit of each package. Not enabled by default; MELPA Stable builds only tagged releases.

Add MELPA in your init file:

ELISP
(require 'package)
(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/") t)
(package-initialize)   ; not needed on Emacs 27+ unless package-enable-at-startup is nil

Then use M-x package-refresh-contents to download the lists, M-x package-install RET magit RET to install, M-x list-packages for a browsable list (press i to mark for install, U to mark upgrades, x to execute) and M-x package-delete to remove. Packages install into ~/.emacs.d/elpa/ and are activated at startup.

Security: prefer HTTPS archives, which is the default. Emacs checks package signatures for GNU ELPA when GnuPG is available (package-check-signature). MELPA packages are not signed, so install only what you need and review unfamiliar ones.

Dependencies and versions

Every package declares a version and a list of dependencies in its header, for example ;; Package-Requires: ((emacs "27.1") (compat "29.1")). When you install a package, package.el also installs everything it requires, in dependency order. Versions are compared numerically segment by segment, so 1.10 is newer than 1.9.

JS Stand-in: dependency order and version compare

Press Run (or Ctrl + Enter) to see the console output here.

No output.

use-package

Once you have ten or more packages, loose require, setq and add-hook lines get messy. use-package is a macro that groups everything about one package and can defer loading until the package is needed, which keeps startup fast:

ELISP
(use-package magit
  :ensure t                      ; install if missing
  :bind (("C-x g" . magit-status))
  :custom
  (magit-diff-refine-hunk t))

(use-package which-key
  :ensure t
  :init (setq which-key-idle-delay 0.5)
  :config (which-key-mode 1))

(use-package markdown-mode
  :ensure t
  :mode ("\\.md\\'" . markdown-mode)
  :hook (markdown-mode . visual-line-mode))

The keywords you will use most:

Keyword Meaning
:ensure t Install from the archives if not present
:init Code that runs before the package loads
:config Code that runs after the package loads
:bind Create key bindings and autoload the command on first use
:mode, :interpreter Associate file names or interpreters with the mode
:hook Add functions to hooks (:hook (prog-mode . company-mode))
:custom Set user options via their setters
:defer t Do not load until something needs it
:if, :when Only configure when a condition holds

To make :ensure t the default for every block, set (setq use-package-always-ensure t). Using :bind, :mode, :hook or :commands implies deferral, so you rarely need :defer t explicitly.

JS Stand-in: what :bind and :config really do

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Other approaches

package.el always installs the newest archive version. If you want pinned versions or to install straight from a Git repository, there are community managers: straight.el and Elpaca clone repositories and can lock exact commits (a lockfile). Emacs 29 also added package-vc-install, which installs a package from its Git URL: M-x package-vc-install RET https://github.com/user/pkg RET. Choose one manager and stay with it; mixing them leads to confusion.

Good hygiene: only install what you use, update regularly (M-x list-packages, then U, x), and put the package list in your init file so a new machine reproduces your setup from scratch.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which archive is NOT enabled by default and is commonly added by users?

    GNU ELPA and NonGNU ELPA are on by default; MELPA must be added to package-archives.
  2. 2. In use-package, where does code in :config run?

    :init runs before loading, :config after loading.
  3. 3. What does :ensure t do?

    :ensure t asks package.el to install the package when it is not present.
  4. 4. Why does using :bind speed up startup?

    :bind creates autoloads so the package is loaded only when the command is first used.

Summary

  • GNU ELPA and NonGNU ELPA are default; add MELPA to package-archives for more.
  • package-refresh-contents, package-install and list-packages manage packages; dependencies install automatically.
  • use-package groups a package's install, bindings, hooks and settings, and defers loading for fast startup.
  • Know :ensure, :init, :config, :bind, :hook, :mode, :custom.
  • package-vc-install, straight.el and Elpaca install from Git when you need pinned or unreleased versions.

Next: Minibuffer Completion with Vertico, Consult and Orderless