aleix's blog

Install Emacs packages from command line

8 January 2013 4:33 PM (emacs)

Lately, I have found myself playing with packages and my .emacs too much. Sometimes I had to comment the use of a package (e.g. smex) because it was not installed.

So, at the end, I wrote this basic elisp to install a package from the command line.

$ emacs --batch --expr "(define pkg-to-install 'smex)" -l emacs-pkg-install.el

The elisp script looks like this:

;;
;; Install package from command line. Example:
;;
;;   $ emacs --batch --expr "(define pkg-to-install 'smex)" -l emacs-pkg-install.el
;;

(require 'package)

(add-to-list 'package-archives
             '("melpa" . "http://melpa.milkbox.net/packages/") t)

(add-to-list 'package-archives
             '("marmalade" . "http://marmalade-repo.org/packages/") t)

;; Fix HTTP1/1.1 problems
(setq url-http-attempt-keepalives nil)

(package-refresh-contents)

(package-install pkg-to-install)

For convenience, you can wrap it in a shell script and simply type:

$ ./emacs-pkg-install.sh smex

The shell script:

#!/bin/sh

if [ $# -ne 1 ]
then
  echo "Usage: `basename $0` <package>"
  exit 1
fi

emacs --batch --eval "(defconst pkg-to-install '$1)" -l emacs-pkg-install.el

2 responses

  1. George Jones says:

    Thanks for the code.

    I had do

    (package-initialize)

    after

    (require 'package).

    Also had to explicitly call

    emacs-snapshot

    in the .sh script ... maybe it was pulling an older version?

    Thanks,
    ---George Jones

  2. Alejandro says:

    Thanks for the code.
    Great scripts man!
    =D

Leave a Reply