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" . "") t)

(add-to-list 'package-archives
             '("marmalade" . "") t)

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


(package-install pkg-to-install)

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

$ ./ smex

The shell script:


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

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



    (require 'package).

    Also had to explicitly call


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

    ---George Jones

  2. Alejandro says:

    Thanks for the code.
    Great scripts man!

Leave a Reply