Auto install emacs packages with MELPA

Objective picture Objective · Jan 11, 2014 · Viewed 10.9k times · Source

I want to declare all packages that I want to use in emacs in a init.el file. I wonder if its possible to load the missing packages with e.g. MELPA when I startup emacs without going through the list and mark the ones I want to install?

Answer

Chris picture Chris · Jan 11, 2014

New answer:

While my original answer is still valid, I now use the method suggested by Jordon. use-package is a fantastic tool for tidy Emacs configurations. In fact, I was already using it for clean package loading when I wrote my original answer.

At that time, I neglected to read all the way to the bottom of its README, and therefore didn't realize that it could handle package installation as well:

For package.el users

You can use use-package to load packages from ELPA with package.el. This is particularly useful if you share your .emacs between several machines; the relevant packages will download automatically once placed in your .emacs. The :ensure key will install the package automatically if it is not already present:

(use-package tex-site
  :ensure auctex)

After loading package.el and initializing my package repositories, my configuration contains

(if (not (package-installed-p 'use-package))
    (progn
      (package-refresh-contents)
      (package-install 'use-package)))

(require 'use-package)

and subsequently many snippets like this:

;;; Expand-region
(use-package expand-region
  :ensure expand-region
  :bind ("C-=" . er/expand-region))

If you find this information useful, please give Jordon an upvote.

Original answer:

Have a look at Cask and Pallet, which work together to solve this problem.

From the Cask website:

Cask for Emacs is what Bundler is to Ruby. It aims to make ELPA dependency management in Emacs painless (as painless as it can be). This includes both your local Emacs installation and Emacs package development.

After installing Cask, you'll need to add something like this to your Emacs configuration:

(require 'cask)
(cask-initialize)

Cask defines a doman-specific language for elisp dependencies. For installing packages, you'll need something like what is outlined here:

(source melpa)

(depends-on "auto-complete")
(depends-on "dash")
(depends-on "f")
(depends-on "flycheck")
(depends-on "helm")
(depends-on "magit")
(depends-on "popup")
(depends-on "projectile")
(depends-on "s")
(depends-on "smartparens")
(depends-on "yasnippet")

Note that this does not go into your Emacs config file, but rather into ~/.emacs.d/Cask.

Pallet updates the Cask file with packages installed interactively, e.g. via M-x package-list-packages, so you don't have to manually maintain the above file.