~/.emacs (which you can find on GitHub) file loads several different Emacs packages and I would like them to be automatically installed as soon as Emacs starts up, if they are not already installed. This way if I have to reinstall my Ubuntu 16.04 operating system again I will not have to install each package myself manually using M-x
package-install. Is there a way to write an
~/.emacs file so that it will automatically install all loaded packages on startup? I would like this to be done with the minimum of lines as I have over ten different packages loaded so if I have to add extra lines for each individual package it will make my
~/.emacs rather long.
The easiest way is to install
(package-initialize) (require 'package) (add-to-list 'package-archives '("melpa" . "http://melpa.milkbox.net/packages/") t) (dolist (package '(use-package)) (unless (package-installed-p package) (package-install package)))
(use-package paredit :ensure t)
:ensure t is what ensures that the package is installed.
To just load ensure multiple packages are install
(dolist (package '(package-a package-b package-c)) (unless (package-installed-p package) (package-install package)) (require package))))
package.el keeps track of the packages you asked to install (as opposed to those that are auto-installed as dependencies) in the custom variable
So if you copy your
~/.emacs (in which Custom normally writes settings like that of
package-selected-packages) to another system, all you should need to do is
Of course, this doesn't automatically install the packages if they're absent, but I consider it a feature (I think it's a mistake for a program like Emacs to make network connections without an explicit request to do so). And it does save you the trouble of installing each package one by one.