16

I want my emacs to automatically upgrade all packages under certain conditions.

What's the best way of doing this?

2

3 Answers 3

12

I'm not sure this is what you want (I don't know what you mean by “under certain conditions”), but here is a function I use to upgrade all packages without showing *Packages* buffer, which I find annoying when I just want to upgrade packages.

(defun package-upgrade-all ()
  "Upgrade all packages automatically without showing *Packages* buffer."
  (interactive)
  (package-refresh-contents)
  (let (upgrades)
    (cl-flet ((get-version (name where)
                (let ((pkg (cadr (assq name where))))
                  (when pkg
                    (package-desc-version pkg)))))
      (dolist (package (mapcar #'car package-alist))
        (let ((in-archive (get-version package package-archive-contents)))
          (when (and in-archive
                     (version-list-< (get-version package package-alist)
                                     in-archive))
            (push (cadr (assq package package-archive-contents))
                  upgrades)))))
    (if upgrades
        (when (yes-or-no-p
               (message "Upgrade %d package%s (%s)? "
                        (length upgrades)
                        (if (= (length upgrades) 1) "" "s")
                        (mapconcat #'package-desc-full-name upgrades ", ")))
          (save-window-excursion
            (dolist (package-desc upgrades)
              (let ((old-package (cadr (assq (package-desc-name package-desc)
                                             package-alist))))
                (package-install package-desc)
                (package-delete  old-package)))))
      (message "All packages are up to date"))))

This is well-tried. It also prevents compilation buffers from popping up.

4
  • Nice. I'm going to use this with a slight variation that doesn't prompt before upgrading the packages.
    – PythonNut
    Sep 7, 2015 at 15:03
  • Is there any particular reason why you do (y-or-n-p (message ..)) instead of just (y-or-n-p ...)?
    – PythonNut
    Sep 8, 2015 at 21:19
  • @PythonNut, Well I need to construct prompt message, I don't remember now why I use message, it seems it can be done with format just as well. Sep 8, 2015 at 21:55
  • Ah yes. I meant using format. Obviously you can't do it without any sort of string interpolation.
    – PythonNut
    Sep 8, 2015 at 21:58
9

You can use auto-package-update.

Install it by M-x package-install auto-update-package. It provides a function called auto-package-update-now. You can write a simple if condition and add that to your .emacs.

(if your-condition
   (auto-package-update-now))
1
  • Hm... I'll keep this in mind, but the idea is to try automatically fixing packaged if they are broken. Depending on a package to do so is kinda nonsensical. :-)
    – PythonNut
    Sep 7, 2015 at 4:05
2

The package is now called auto-package update. (Too low reputation to write it as a comment to the Chillar post above).

(Copied from his post and merged with this answer):

Install it by M-x package-install auto-package-update. It provides a function called auto-package-update-now. You can write a simple if condition and add that to your ~/.emacs.

(if your-condition
   (auto-package-update-now))
1
  • I've edited Chillar's answer to correct the package name, we can delete this one now I think. Mar 13, 2018 at 13:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.