Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto install emacs packages with MELPA

I want to declare all packages that I want to use in emacs in a init.el file. I wonder if its possible to load the missing packages with e.g. MELPA when I startup emacs without going through the list and mark the ones I want to install?

like image 363
Objective Avatar asked Jan 11 '14 16:01

Objective


People also ask

How do I automatically install Emacs packages?

If you're already using use-package , you can use the :ensure keyword to install packages automatically. This also sets up package-selected-packages if you need to access the package list through customize or programatically.

How do I enable a package in Emacs?

To keep Emacs from automatically making packages available at startup, change the variable package-enable-at-startup to nil . You must do this in the early init file, as the variable is read before loading the regular init file. Currently this variable cannot be set via Customize.

Where are Emacs packages installed?

Once installed, the contents of a package are placed in a subdirectory of ~/. emacs. d/elpa/ (you can change the name of that directory by changing the variable package-user-dir ). The package subdirectory is named name - version , where name is the package name and version is its version string.


2 Answers

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.

like image 152
Chris Avatar answered Oct 02 '22 14:10

Chris


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.

like image 29
Jordon Biondo Avatar answered Oct 02 '22 13:10

Jordon Biondo