I manage packages in Emacs using Cask and Pallet. Yesterday I upgraded to Emacs 24.4; it went well enough, but all of a sudden all my packages were missing. Cask installs packages in separate directories by Emacs version, so everything was in .cask/24.3.1
; there was nothing in .cask/24.4.1
, and in fact the directory didn't exist. Once I figured this out, I was able to run cask
and install everything again -- but is there a way to make this happpen automagically, from within Emacs itself, when upgrading?
1 Answer
Without using cask or pallet, you can achieve this with code such as the following:
(setq package-user-dir (expand-file-name (concat "elpa/" emacs-version) user-emacs-directory))
(setq package-enable-at-startup nil)
(package-initialize)
(defun require-package (package &optional min-version no-refresh)
"Install given PACKAGE, optionally requiring MIN-VERSION.
If NO-REFRESH is non-nil, the available package lists will not be
re-downloaded in order to locate PACKAGE."
(if (package-installed-p package min-version)
t
(if (or (assoc package package-archive-contents) no-refresh)
(package-install package)
(progn
(package-refresh-contents)
(require-package package min-version t)))))
Then you can sprinkle (require-package ...)
calls throughout your config:
(require-package 'flycheck)
The packages will be installed into a version-specific package directory at startup, unless they're already there. (Disclaimer: I haven't tried the modification to package-user-dir
above, so YMMV.)
This won't "lock" package versions, but I don't personally care about that. To upgrade packages, you'd go through the usual M-x package-list-packages
procedure.
packages-install
? I have all my packages installed automatically withpackages-install
, see github.com/rekado/.emacs.d/blob/master/lisp/init-package.el .cask
on every startup.