1

I am trying to understand the subtle nuances of Emacs' packaging system(s). I do not find that anyone has really clarified or explained the difference between two, what I assume to be, key components of Emacs package system: require and package-install.

I commonly see code like the following in many init.el files (illustrative example):

(require 'package) ; Use Emacs package system
(add-to-list 'package-archives '("melpa-stable" . "https://stable.melpa.org/packages/") t) ; Add MELPA package repository as a place to go lookup for packages
(package-initialize) ; Turns Emacs package system on
; ... refresh code omitted for brevity
(unless (package-installed-p 'helm) ; Only installed "helm" if we don't see it as already installed
    (package-install 'helm)
)

(require 'helm) ; Get helm to actually use
; ...

The thought process for understanding what is going on:

  • require seems to be called for package but the "package" for package does not need to be "installed". Is this internal somehow?
  • Is package needed for use-package to work but not require, correct?
  • For helm (in this example) we must do/use package-install (with some checks) to first "install" helm and then require to actually use it. Is this correct?
  • If we did (use-package helm ... ) then we would not need either require or package-install 'helm, right?

To extrapolate:

If one wanted to use any particular package, they first need to make sure they have an equivalent package-install call / statement prior to invoking require (or just utilize a use-package which will "merge" both steps) on that particular package, correct?

I just want to be sure I am properly setting up my usage of packages and so forth. Emacs is very confusing when it comes to packages, installing, requiring, etc. ... at least that I find.


Q/A Housekeeping Notes:

According to common advice, there are important differences in the Emacs major versions, so I would like an explanation for each (Emacs version 25, 26, and 27, respectively) for others who may come across the same question but on an older version (if it matters).

I am currently on Emacs 27.2 on macOS Catalina (just to be clear).

In an attempt to preempt "duplicate of ..." or "searching will find ...": In my searching, I found require vs. package-initialize? but it does not look like anyone really answered the question to satisfaction (or clearly).

Drew
  • 75,699
  • 9
  • 109
  • 225
David
  • 127
  • 7
  • This is a monster of a question. It will take one very much time to answer all the sub-questions and it is very likely that you do not get a definite answer but a large bunch of comments (if no one closes the question before). I suggest you try to split the question into several questions that are more likely to be answered. Try to identify exactly what is not answered by the excellent post [require vs. package-initialize](https://emacs.stackexchange.com/questions/44266/require-vs-package-initialize) and ask specific questions. – Tobias May 29 '21 at 04:50
  • @Tobias That other question never explained (and didn't really ask AFAIK) the difference between `require` and `package-install`. It covered another command. I am not sure what is _exactly_ overwhelming about my question here. "What is the difference between `require` and `package-install`?". I outlined my thoughts as I was trying to understand it. – David May 29 '21 at 04:57
  • Nevertheless, this question is perhaps a good summary of possible problems in understanding the Emacs package system. So, from my point of view it should **not** be deleted. I am also not sure about closing. A community answer could be the right way to go. Therefore, I do not yet throw a close request but I will flag the question for the moderators. – Tobias May 29 '21 at 04:58
  • @Tobias I added some edits to "clarify" what I am asking. I think it's very clear. I am using leading questions for the thought process and overall way I understand the command statements involved. I figured that would be easiest to simply reply with "Yes/No" (quoting each leading question) and if no, to explain what is really correct. – David May 29 '21 at 05:01
  • It may very well be that I am wrong with my previous comments and I am flagging the question for exactly that reason. The moderators and maybe others can decide. Pityingly, I actually wanted to give productive comments. (I got stuck with the definite answer.) – Tobias May 29 '21 at 05:11
  • `package` is part of Vanilla Emacs. You can determine that by `M-x locate-library`. In Emacs 27 you do not need to `require` it anymore. – Tobias May 29 '21 at 05:48
  • https://emacs.stackexchange.com/questions/44266/require-vs-package-initialize says: For most packages you do not need `require` because most commands are autoloaded. – Tobias May 29 '21 at 05:53
  • If you installed `helm` with `package-install` it is most likely that you do not need anything in your init file. Everything is auto-loaded. – Tobias May 29 '21 at 05:59
  • @Tobias Sounds like those last few comments would be a great start to an answer. :) – David May 29 '21 at 06:50
  • "...great start to an answer": Yes, that is exactly the problem with this question. It is difficult to write a complete answer. I hope you reckognize that these meta-comments of mine are ment purely constructive and by no means offensive. I want to help you to get more high-quality answers to your questions now and in the future. – Tobias May 29 '21 at 08:04
  • @Tobias No worries. I certainly appreciate the input / feedback (it's not personal at all). I am still new to Emacs so I am trying to formulate my question and understanding as best I can (I am used to "installing a package means it is then in use onward"). I apologize if my question does seem unclear (I don't know what I should be asking or _exactly_ how) or if there is too much :) Emacs is a _very_ confusing piece of technology with _very_ complex parts. – David May 29 '21 at 10:31

1 Answers1

3

Think of a package as a bunch of e-lisp and maybe other files. Many of these (such as the package package itself) come as part of your emacs (these are said to be built-in) and so need no further installation. Others, such as pdf-tools or helm are located in repositories on the interwebs such as melpa and so must be downloaded onto your computer before they are available to emacs. This is (part of) what package-install does.

A separate concern is to load a package into your running emacs so that you can use its functionality. This is what require does. It can only load stuff that is on your system. This is why there is a package-install that may have to be done first.

Finally use-package can do both things in one shot:

(use-package foo)

is essentially a fancy wrapper around (require 'foo) while

(use-package foo
  :ensure t)

calls package-install on foo, if necessary.

For a given package, you can do describe-package (bound to C-h P in vanilla emacs) to see where it comes from and whether it is installed.

There is more to say here: once a package is installed, the magic of autoloads means that require may be quite unnecessary: see the previously referenced post require vs. package-initialize? for this.

Fran Burstall
  • 3,665
  • 10
  • 18
  • The correct way is then to use `package-install ...` and then `require ...` because it is the only actual technique guaranteed to work all the time, correct? The others are far too unstable because of version incompatibility and package incompatibility, correct? The confusing part was that in Emacs land `package-install` does not actually mean "use the package" (otherwise why even install a package?). Ah, just another one of Emacs' archaic philosophies and wrong designs. Thanks for the explanation! :) – David May 31 '21 at 02:42
  • @David: That is the correct order of events but actually, for the most part, you should not be doing `package-install` in your init since it is a one-off. I use `use-package` myself and almost never run into version/package incompatibilities. As to why emacs does it this way: there are packages that I might want available to me that I don't want to load every time. – Fran Burstall May 31 '21 at 09:37
  • Thanks @FranBurstall. As I noted in my question, I surround every call for `package-install` with a matching `unless package-installed-p` check so that prevents any weirdness in the config with packages. Interesting point about wanting packages available but not to use or load every time. I cannot imagine in this is a common scenario but perhaps there are performance or compatibility reasons for it. I am going to mark your answer as the selected one :) – David Jun 02 '21 at 12:41