131

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

4

11 回答 11

111
; 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)))
于 2012-04-10T17:16:20.790 回答
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)
于 2012-04-10T20:18:33.113 回答
44

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

于 2016-10-06T08:29:02.047 回答
19

这是我用于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 不会每次都刷新(因为这会显着减慢启动速度) - 仅在存在已卸载软件包的情况下。

于 2012-04-11T08:06:50.583 回答
7

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

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

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

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

此外,您可以使用cask update.

于 2014-03-02T21:19:07.700 回答
4

package-install以包名作为符号调用。package-install您可以通过交互调用并完成名称来查找包的包名称。该功能package-installed-p将让您知道它是否已经安装。

例如:

(mapc
 (lambda (package)
   (or (package-installed-p package)
       (package-install package)))
 '(package1 package2 package3))
于 2012-04-10T16:23:48.863 回答
4
(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)
于 2015-09-10T06:25:19.533 回答
3

我喜欢检查用户是否想按照这个答案首先安装软件包。此外,在安装任何东西之前,我都会刷新一次我的包内容。我不确定这是否是最好的方法,但我认为最佳答案并不是为我做的。

(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)))
于 2015-06-18T21:20:38.260 回答
1

这是我的,它更短:)

(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))
于 2017-07-20T18:23:07.560 回答
1

我遇到了一个问题,添加(package-install 'org).emacs. 我想安装的最新版本org-mode和内置org-mode是相当旧的。

package-install我从 Emacs 25.3.1中挖出了源代码。函数 self 已经检查是否安装了包,如果包已经安装,则拒绝安装它。因此,实际上不需要(unless (package-installed-p package) ...)从答案10093312进行检查。

(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对象指定版本信息。

于 2017-12-17T02:57:51.800 回答
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)
于 2020-05-11T05:16:48.640 回答