Auto install emacs packages with MELPA

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.


All of these answers will work, but I would highly recommend using use-package

found here: https://github.com/jwiegley/use-package

use-package not only will automatically install your missing packages, but it greatly simplifies your init.el.

Here's an example from my init.el

;; initial package setup
(push "path/to/use-package" load-path)
(require 'use-package)
(require 'package)
(mapc (lambda(p) (push p package-archives))
      '(("marmalade" . "http://marmalade-repo.org/packages/")
        ("melpa" . "http://melpa.milkbox.net/packages/")))
(package-refresh-contents)
(package-initialize)

;; this will install undo-tree if it's not there
;; and it will set it up how I want
(use-package undo-tree
  :init (global-undo-tree-mode 1)
  :bind (("C-c j" . undo-tree-undo)
         ("C-c k" . undo-tree-redo)
         ("C-c l" . undo-tree-switch-branch)
         ("C-c ;" . undo-tree-visualize))
  :ensure t)

Take a look at my init.el here: https://github.com/jordonbiondo/.emacs.d/blob/master/init.el

each use-package block will install the specified package if it is not there, and it encapsulates all my additional setup for packages like keybindings, hooks, and other customizations.