如何通过指定包名列表自动安装Emacs包?

时间:2012-04-10 16:07:15

标签: emacs install package

我正在使用package来管理我的Emacs扩展程序。为了在不同的计算机上同步我的Emacs设置,我想在.emacs文件中指定包名列表,然后package可以自动搜索和安装包,这样我就不会不需要通过调用M-x package-list-packages手动安装它们。怎么做?

11 个答案:

答案 0 :(得分:101)

; list the packages you want
(setq package-list '(package1 package2))

; list the repositories containing them
(setq package-archives '(("elpa" . "http://tromey.com/elpa/")
                         ("gnu" . "http://elpa.gnu.org/packages/")
                         ("marmalade" . "http://marmalade-repo.org/packages/")))

; activate all the packages (in particular autoloads)
(package-initialize)

; fetch the list of packages available 
(unless package-archive-contents
  (package-refresh-contents))

; install the missing packages
(dolist (package package-list)
  (unless (package-installed-p package)
    (package-install package)))

答案 1 :(得分:45)

根据Profpatsch的评论和以下答案:

(defun ensure-package-installed (&rest packages)
  "Assure every package is installed, ask for installation if it’s not.

Return a list of installed packages or nil for every skipped package."
  (mapcar
   (lambda (package)
     ;; (package-installed-p 'evil)
     (if (package-installed-p package)
         nil
       (if (y-or-n-p (format "Package %s is missing. Install it? " package))
           (package-install package)
         package)))
   packages))

;; make sure to have downloaded archive description.
;; Or use package-archive-contents as suggested by Nicolas Dudebout
(or (file-exists-p package-user-dir)
    (package-refresh-contents))

(ensure-package-installed 'iedit 'magit) ;  --> (nil nil) if iedit and magit are already installed

;; activate installed packages
(package-initialize)

答案 2 :(得分:37)

Emacs 25.1+将自动跟踪可自定义的package-selected-packages变量中的用户安装包。 package-install将更新自定义变量,您可以使用package-install-selected-packages函数安装所有选定的包。

此方法的另一个便利优势是您可以使用package-autoremove自动删除package-selected-packages中未包含的包(尽管它会保留依赖关系)。

(package-initialize)
(unless package-archive-contents
  (package-refresh-contents))
(package-install-selected-packages)

来源:http://endlessparentheses.com/new-in-package-el-in-emacs-25-1-user-selected-packages.html

答案 3 :(得分:17)

以下是我用于Emacs Prelude的代码:

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

(setq url-http-attempt-keepalives nil)

(defvar prelude-packages
  '(ack-and-a-half auctex clojure-mode coffee-mode deft expand-region
                   gist haml-mode haskell-mode helm helm-projectile inf-ruby
                   magit magithub markdown-mode paredit projectile
                   python sass-mode rainbow-mode scss-mode solarized-theme
                   volatile-highlights yaml-mode yari yasnippet zenburn-theme)
  "A list of packages to ensure are installed at launch.")

(defun prelude-packages-installed-p ()
  (loop for p in prelude-packages
        when (not (package-installed-p p)) do (return nil)
        finally (return t)))

(unless (prelude-packages-installed-p)
  ;; check for new packages (package versions)
  (message "%s" "Emacs Prelude is now refreshing its package database...")
  (package-refresh-contents)
  (message "%s" " done.")
  ;; install the missing packages
  (dolist (p prelude-packages)
    (when (not (package-installed-p p))
      (package-install p))))

(provide 'prelude-packages)

如果你没有使用MELPA,你不需要它(如果你melpa.el必须在load-path上(或通过MELPA安装)。包db是每次都不刷新(因为这会显着减慢启动速度) - 只有在存在卸载的软件包的情况下才会刷新。

答案 4 :(得分:7)

还没有人提到Cask,但它非常适合这项任务。

基本上,您创建列出要安装的软件包的~/.emacs.d/Cask。例如:

(source melpa)
(depends-on "expand-region")
(depends-on "goto-last-change")
; ... etc

从命令行运行cask将为您安装这些软件包以及它们所需的任何依赖项。

此外,您可以使用cask update自动更新已安装的软件包。

答案 5 :(得分:4)

使用包名称作为符号调用package-install。您可以通过以交互方式调用package-install并填写名称来查找软件包的软件包名称。函数package-installed-p会告诉您它是否已经安装。

例如:

(mapc
 (lambda (package)
   (or (package-installed-p package)
       (package-install package)))
 '(package1 package2 package3))

答案 6 :(得分:3)

我喜欢检查用户是否要先安装包http://faq.sealedabstract.com/xcodeCI/#signing-for-distribution。在安装任何东西之前,我还要刷新一次包装内容。我不确定这是不是最好的方法,但我不认为最好的答案是为我做的。

(setq required-pkgs '(jedi flycheck cider clojure-mode paredit markdown-mode jsx-mode company))

(require 'cl)

(setq pkgs-to-install
      (let ((uninstalled-pkgs (remove-if 'package-installed-p required-pkgs)))
        (remove-if-not '(lambda (pkg) (y-or-n-p (format "Package %s is missing. Install it? " pkg))) uninstalled-pkgs)))

(when (> (length pkgs-to-install) 0)
  (package-refresh-contents)
  (dolist (pkg pkgs-to-install)
    (package-install pkg)))

答案 7 :(得分:3)

(require 'cl)
(require 'package)

(setq cfg-var:packages '(
       emmet-mode
       ergoemacs-mode
       flycheck
       flycheck-pyflakes
       monokai-theme
       py-autopep8
       py-isort
       rainbow-mode
       yafolding
       yasnippet))

(defun cfg:install-packages ()
    (let ((pkgs (remove-if #'package-installed-p cfg-var:packages)))
        (when pkgs
            (message "%s" "Emacs refresh packages database...")
            (package-refresh-contents)
            (message "%s" " done.")
            (dolist (p cfg-var:packages)
                (package-install p)))))

(add-to-list 'package-archives '("gnu" . "http://elpa.gnu.org/packages/") t)
(add-to-list 'package-archives '("melpa" . "http://melpa.org/packages/") t)
(add-to-list 'package-archives '("melpa-stable" . "http://stable.melpa.org/packages/") t)
(add-to-list 'package-archives '("org" . "http://orgmode.org/elpa/") t)
(package-initialize)

(cfg:install-packages)

答案 8 :(得分:1)

我遇到了一个问题,即在(package-install 'org)添加.emacs后没有任何反应。我想安装最新版本的org-mode,内置org-mode已经过时了。

我从Emacs 25.3.1中挖掘出package-install的源代码。函数self已经检查是否已安装软件包,如果已安装软件包,则拒绝安装软件包。因此,来自回答10093312的检查(unless (package-installed-p package) ...)实际上是不必要的。

(defun package-install (pkg &optional dont-select)
  "Install the package PKG.
PKG can be a package-desc or a symbol naming one of the available packages
in an archive in `package-archives'.  Interactively, prompt for its name.

If called interactively or if DONT-SELECT nil, add PKG to
`package-selected-packages'.

If PKG is a package-desc and it is already installed, don't try
to install it but still mark it as selected."
  (interactive
   (progn
     ;; Initialize the package system to get the list of package
     ;; symbols for completion.
     (unless package--initialized
       (package-initialize t))
     (unless package-archive-contents
       (package-refresh-contents))
     (list (intern (completing-read
                    "Install package: "
                    (delq nil
                          (mapcar (lambda (elt)
                                    (unless (package-installed-p (car elt))
                                      (symbol-name (car elt))))
                                  package-archive-contents))
                    nil t))
           nil)))
  (add-hook 'post-command-hook #'package-menu--post-refresh)
  (let ((name (if (package-desc-p pkg)
                  (package-desc-name pkg)
                pkg)))
    (unless (or dont-select (package--user-selected-p name))
      (package--save-selected-packages
       (cons name package-selected-packages)))
    (if-let ((transaction
              (if (package-desc-p pkg)
                  (unless (package-installed-p pkg)
                    (package-compute-transaction (list pkg)
                                                 (package-desc-reqs pkg)))
                (package-compute-transaction () (list (list pkg))))))
        (package-download-transaction transaction)
      (message "`%s' is already installed" name))))

内置org-mode也计入安装状态,package-install拒绝安装ELPA中的新版本。在花了一些时间阅读package.el之后,我提出了以下解决方案。

(dolist (package (package-compute-transaction
                  () (list (list 'python '(0 25 1))
                           (list 'org '(20171211)))))
  ;; package-download-transaction may be more suitable here and
  ;; I don't have time to check it
  (package-install package))

它起作用的原因是package-*族函数根据它是符号还是package-desc对象来不同地处理参数。您只能通过package-install对象指定package-desc的版本信息。

答案 9 :(得分:0)

这是我的,它更短:)

(mapc
 (lambda (package)
   (unless (package-installed-p package)
     (progn (message "installing %s" package)
            (package-refresh-contents)
            (package-install package))))
 '(browse-kill-ring flycheck less-css-mode tabbar org auto-complete undo-tree clojure-mode markdown-mode yasnippet paredit paredit-menu php-mode haml-mode rainbow-mode fontawesome))

答案 10 :(得分:0)

这是另一种方式。

;; assure every package is installed
(defun ensure-package-installed (&rest packages)
  (let ((user-required-packages
         (seq-remove
          (lambda (package) (package-installed-p package))
          packages)))
    (when user-required-packages
      (package-refresh-contents)
      (dolist (package user-required-packages)
        (package-install package)))))

;; list of packages to install
(ensure-package-installed
 'try
 'which-key)