How to rebuild ELPA packages after upgrade of Emacs
Asked Answered
R

5

71

I just upgraded GNU Emacs from 23 to 24 on MacOS and some ELPA installed packages stopped working. One of them is AucTeX. Deleting it and reinstalling it through the package manager made it work again, but I don't want to do this by hand for every package. I'm slightly confused that I find nothing about that on the Internet.

Don't the .elc need to be recompiled for a new version of Emacs? Why isn't this a feature of package.el?

Robbie answered 13/7, 2014 at 18:17 Comment(0)
K
134

You do not need to re-install all packages. The packages itself are likely fine, however, they need to be re-compiled, because Emacs Lisp byte code is generally not compatible across major versions.

To re-compile all packages, type M-: (byte-recompile-directory package-user-dir nil 'force). After restarting Emacs, packages should work fine again.

Kacerek answered 14/7, 2014 at 11:19 Comment(3)
My understanding is that byte code compatibility is only an issue if you are downgrading (or similar) to an older major version of Emacs. A newer Emacs version will run byte code compiled by an older Emacs version.Christmann
While it is true that emacs byte code maintains backwards compatibility, it is usually a good idea to re-compile, especially when moving to a later major version. The reason is that later major versions often introduce new features and some of these can result in improved performance or functionality.Neutrophil
If you are me and deleted all .elc files, you need to set the ARG argument to 0 like so: (byte-recompile-directory package-user-dir 0 'force)Emmyemmye
K
10

This works for me on Emacs 25.1 and 26:

(defun package-reinstall-all-activated-packages ()
  "Refresh and reinstall all activated packages."
  (interactive)
  (package-refresh-contents)
  (dolist (package-name package-activated-list)
    (when (package-installed-p package-name)
      (unless (ignore-errors                   ;some packages may fail to install
                (package-reinstall package-name))
        (warn "Package %s failed to reinstall" package-name)))))
Kerbstone answered 26/10, 2016 at 21:24 Comment(1)
Just M-x reinstall package RET package-name works interactively for one package.Breakfront
S
5

The variable package-activated-list holds the list of packages we're interested in. So we just need to install each one again. We don't need to explicitly delete them; calling package-install will blow away an old version.

Put this code in a scratch buffer and evaluate it (that is, put your cursor at the end, and press C-x C-e):

(dolist (package-name package-activated-list)
  (package-install package-name))
Simplex answered 14/7, 2014 at 7:23 Comment(1)
This fails: Debugger entered--Lisp error: (wrong-type-argument arrayp ac-dabbrev): line 1: package-desc-full-name(ac-dabbrev) line 2: package-install(ac-dabbrev)Homolographic
M
3

my recipe after emacs 25:

  1. in .emacs (define your packages list):
(custom-set-variables
 '(package-selected-packages
   (quote
      (browse-kill-ring helm undo-tree use-package)))
  1. in a terminal:
$ rm -rf ~/.emacs.d/elpa/*
  1. in emacs:
(progn (package-refresh-contents)
    (package-install-selected-packages)
    (byte-recompile-directory package-user-dir nil 'force))
Micco answered 18/9, 2018 at 16:56 Comment(0)
C
2

There is now package-recompile-all to re-byte-compile all installed packages, which is a bit shorter than the existing answer.

(package-recompile-all)

Byte-compile all installed packages.
This is meant to be used only in the case the byte-compiled files
are invalid due to changed byte-code, macros or the like.

  Probably introduced at or before Emacs version 29.1.
Cyzicus answered 12/6 at 12:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.