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

11
  • 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 and ask specific questions.
    – Tobias
    May 29, 2021 at 4: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, 2021 at 4: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, 2021 at 4: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, 2021 at 5: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, 2021 at 5:11

1 Answer 1

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.

3
  • 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, 2021 at 2: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. May 31, 2021 at 9: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 2, 2021 at 12:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.