简体   繁体   中英

use different package repository in emacs

I have a .emacs file in which I use package-install to automatically install needed packages from a repository at initialization. Up to now, I was using the http://melpa.milkbox.net/packages/ repository, which I got from an online tutorial. At some point, I tried to install the auctex package and got an error message which told me the package was not available under that repository. I checked and it is true. I found out the the auctex package is available under the GNU repository: http://elpa.gnu.org/packages/ instead.

What I tried to do is the following: add a second package repository (GNU ELPA in this case) to my .emacs file from which package-install can feed if it needs to install packages not available under the first repository (MELPA).

I am not an expert (in fact, I am a pure beginner) in Elisp and I browsed various threads to find a solution. Here is my actual code:

(package-initialize)

(require 'package)

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

(package-refresh-contents)

(dolist (package '(use-package))
  (unless (package-installed-p package)
           (package-install package)))

(use-package paredit :ensure t)

(dolist (package '(auctex         ; <-- Not available under MELPA!
                   auto-complete
                   auto-complete-c-headers
                   magit
                   sr-speedbar
                   yasnippet
                   ))

  (unless (package-installed-p package)
    (package-install package))
     (require package))

With this code in my .emacs file, when I boot emacs up, my configurations are ignored. I get this error message: error: Required feature 'auctex' was not provided .

How can I modify my code to see the auctex package from GNU ELPA?

For your information, I use Ubuntu 16.04 with GNU Emacs 24.5.1.


EDIT: Just to add a few words on what Stefan proposed: there were several problems with my code and splitting my add-to-list 'package-archives ...) in two calls did not seem, to work (although it is the right thing to do, of course). This was due to my (require package ) line was trying to do: (require auctex) at some point. See this post to see why that is a problem.

I read on the fact that (require 'package-name) is not needed after package installation. This post explains why. removing this line made sure that auctex was loaded correctly, because it was automatically loaded.

From then on, everything worked fine, no more errors or warnings. I then tried to remove my lines:

(add-to-list 'package-archives
             '("gnu" . "http://elpa.gnu.org/packages/"))

and auctex package was still loaded correctly.

Your

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

doesn't add 2 entries to the list: add-to-list only adds a single element to a list. In this case it will add the element (("melpa" . "http://melpa.milkbox.net/packages/") ("gnu" . "http://elpa.gnu.org/packages/")) which is not a valid element.

You'd want

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

But note that the second add-to-list should be redundant because package-archives by default contains ("gnu" . "http://elpa.gnu.org/packages/") already.

One more thing: you should not need to require those packages after installing them (like you do on the last line of your code), because the packages should come with enough autoloads to make such require unneeded (and harmful in the sense that it will slowdown your startup).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM