On your journey toward mastering GNU Emacs, it’s easy to get overwhelmed with all the built-in functionality. On top of that, you will likely see some advanced configurations out there that have incorporated outside functionality via package installs.
In this article, you’re going to learn how to reach out to Emacs package archives and install packages from them. Here is a summary of what we will cover:
Setting up the package archives
You will find some pre-configured package archives in the first code snippet of the “darkstar” config file. We have set up MELPA, ELPA, and also the “non-gnu” package (you’ll find out what that is later).
Notice the list of package archives and their respective URLs in quotes in the snippet below:
(require 'package) (setq package-archives '(("melpa" . "https://melpa.org/packages/") ("nongnu" . "https://elpa.nongnu.org/nongnu/") ("elpa" . "https://elpa.gnu.org/packages/"))) (package-initialize) (unless package-archive-contents (package-refresh-contents)) (unless (package-installed-p 'use-package) (package-install 'use-package)) (require 'use-package) (setq use-package-always-ensure t)
(Special thanks to David Wilson, because I copied this from his Emacs From Scratch config.)
The magic of “use-package”
As your configuration absorbs more packages over time, it can become a chore to remember exactly what you have installed. And if you switch to a different workstation, you will want a portable configuration you can take with you without missing a beat.
A package called “use-package” makes this super easy. “Use-package” simplifies the process of installing and configuring your packages via a simple command in your configuration file. For example, let’s say you want to sync your Emacs config on your home laptop and work laptop; all you need to do is copy your config file and let “use-package” download and configure your installs.
The darkstar config I provided in the previous article installs use-package as the first command. It first checks to see if you have “use-package” installed. If not, it will install it. That done, Emacs can proceed through the file and do the rest of the work.
From there, the first package we come to is “vertico.” Here is the “use-package” declaration for it:
(use-package vertico :ensure t :init (vertico-mode))
This is doing a few sophisticated things:
- Tell Emacs to “use” vertico, and install if necessary.
- “Ensure” that the package is installed via “ensure.”
- Enable
vertico-mode
on startup via “init.”
As you search through package documentation, you will find examples of use-package declarations, but the most basic kind, as seen above, will be sufficient in most cases.
Package refresh
It’s best practice to first download the latest package information from the archives before installing anything.
To do this, we just need to run an interactive function. (An interactive function is a function that can be invoked by pressing M-x
.) We will invoke the function package-refresh-contents
to download the latest package information:
M-x --> package-refresh-contents
(If you installed the “vertico” package, you should see the auto completion options for this and other functions.)
Emacs will reach out to your chosen repositories to grab new information. It shouldn’t take more than a few seconds, depending on your Internet connection.
Install packages
Then you just need to run a command to install the package.
And, of course, when you <RET>
, that’s referring to the Return or Enter key on your keyboard.
M-x package-install <RET> <package name> <RET>
With auto-completion options, names of packages will be presented. Just start typing, and your options will narrow down. Once you have found the desired package, press Enter again and the package will be installed.
Remember, just because is installed, Emacs will not always know what to do with it. Some packages need to be activated, as in the Vertico example above with use-package
. Always check the package documentation to learn about how to use it.