Buffer Yard
Workflow

Installing Emacs Packages with MELPA, ELPA and use-package

Updated

You find a package in someone’s config, paste the use-package block into yours, restart, and Emacs complains that it cannot find it. Nine times out of ten the archive it comes from is not configured. The package system is small once you see its parts: a few archives that host packages, the built-in package.el that downloads them, and use-package, which ties each package to its configuration in one readable block.

The package archives

An archive is a server that publishes a list of packages and their tarballs. Emacs knows about two out of the box.

GNU ELPA is the official archive of the GNU project. Packages there have copyright assigned to the Free Software Foundation, which is what allows code to move between ELPA and Emacs itself. Vertico, Consult, Orderless and Corfu all live there.

NonGNU ELPA is also run by the Emacs maintainers but does not require copyright assignment. It has been enabled by default since Emacs 28 and holds well-known packages such as markdown-mode and evil.

MELPA is the big community archive, with several thousand packages built straight from their Git repositories. It is not enabled by default, and most configs add it on the first line that deals with packages:

(require 'package)
(add-to-list 'package-archives
             '("melpa" . "https://melpa.org/packages/") t)

MELPA Stable (https://stable.melpa.org/packages/) builds only from tagged releases. It sounds safer, but many authors tag rarely, so you get older code, and mixing it with regular MELPA leads to dependency trouble. Plain MELPA is the better default; add Stable only for a specific package you want frozen.

You do not need (package-initialize) any more. Since Emacs 27 it runs automatically before init.el is loaded.

Installing a package by hand

To try a package before writing any config, run M-x package-refresh-contents once to download the archive lists, then M-x package-install and type the name. For browsing, M-x list-packages shows everything available; press i to mark a package, x to install, and U to mark upgrades. Emacs 29 added package-upgrade and package-upgrade-all, and package-autoremove cleans up dependencies nothing needs any more.

Installed packages land in ~/.emacs.d/elpa/ (or the elpa folder under whichever directory your Emacs uses, see where the init file lives). Deleting that folder is a harmless way to start fresh: a config written with use-package reinstalls everything on the next start.

If an install fails with a signature error, the GNU ELPA signing key on your machine is probably older than the one the archive now uses. That happens mostly on Emacs builds that sat unused for a while. The gnu-elpa-keyring-update package exists for exactly this case; installing it brings the keys up to date, after which package-refresh-contents works again. Turning signature checks off makes the error go away too, but it also removes the only verification the archive offers.

use-package: one block per package

Since Emacs 29.1 use-package is built in. It is a macro that wraps the install, the loading and the settings of a package in one form, and it is the most readable way to keep a config organised.

(use-package magit
  :ensure t
  :bind ("C-x g" . magit-status))

(use-package rainbow-delimiters
  :ensure t
  :hook (prog-mode . rainbow-delimiters-mode))

(use-package orderless
  :ensure t
  :init
  (setq completion-styles '(orderless basic))
  :config
  (setq orderless-matching-styles '(orderless-literal orderless-regexp)))

The keywords you will use most:

  • :ensure t installs the package from the archives if it is missing. Set use-package-always-ensure to t to make that the default, but leave it off for built-in packages such as eglot or dired.
  • :init runs before the package loads. Use it for variables that must be set early.
  • :config runs after the package loads. Most settings belong here.
  • :hook adds a function to a hook; (prog-mode . rainbow-delimiters-mode) turns the mode on in every programming buffer.
  • :bind sets keys and also makes loading lazy: the package loads the first time you press one of them.
  • :defer t delays loading until something triggers it, an autoload or a hook. :hook, :bind and :commands imply it already.

Deferring is where the startup time goes. A config with fifty packages that all load eagerly takes seconds to open; the same config with :bind and :hook used well opens in well under a second. Run M-x emacs-init-time to see your number, and set use-package-compute-statistics to t followed by M-x use-package-report to see which package costs the most.

Keys inside :bind follow the same notation as everywhere else in Emacs, and the conventions in the keybindings guide apply: personal commands go under C-c plus a letter.

Pinning a package to one archive

When the same package exists in two archives, package.el takes the highest version number. Sometimes you want a specific source instead. With use-package it is one keyword:

(add-to-list 'package-archives
             '("melpa-stable" . "https://stable.melpa.org/packages/") t)

(use-package cider
  :ensure t
  :pin melpa-stable)

Without use-package, the variable package-pinned-packages does the same thing, and package-archive-priorities lets you prefer one archive across the board. Pinning is worth it for packages that break often between releases; for everything else it adds maintenance for little gain.

Installing straight from Git with package-vc-install

Some packages live only on GitHub, Codeberg or SourceHut, or you want an unreleased fix. Emacs 29 added package-vc-install, which clones a repository and installs it as a regular package. Here it fetches the development version of Jinx, a spell checker by Daniel Mendler:

(package-vc-install "https://github.com/minad/jinx")

Emacs 30 added a :vc keyword to use-package, so the same thing fits into a normal block. Upgrades go through package-vc-upgrade. For one or two Git packages this is all you need, and it keeps everything inside the built-in system.

straight.el and elpaca

Two third-party package managers replace package.el entirely. straight.el clones every package as a Git repository and records exact commits in a lockfile, so your config reproduces the same versions on every machine. elpaca does the same job with asynchronous, parallel installs, which makes the first start on a new machine much faster.

Both are good tools, and both need package.el switched off in early-init.el with (setq package-enable-at-startup nil), plus a bootstrap snippet from their READMEs. For a new config, stay with package.el and use-package: they ship with Emacs, every README documents them, and package-vc-install covers the Git case. Move to straight.el or elpaca only when you actually need lockfiles across several machines.

With the archives set up, the first packages worth adding are the completion stack described in the completion guide, followed by a language server setup from the language guides.

Questions readers ask

Do I still need to call package-initialize in my init file?

No. Since Emacs 27 the package system is initialised automatically before init.el runs. Calling it again only slows startup down.

Should I use MELPA or MELPA Stable?

Use MELPA for most configs. MELPA Stable only builds tagged releases, and many packages tag rarely or never, so it is often older without being more reliable.

How do I update all installed packages?

On Emacs 29 and later, run M-x package-upgrade-all. On older versions, open M-x list-packages, press U to mark upgrades and x to run them.