I have noticed recently, the Emacs always shows a list of "unneeded" packages, which could be "auto-removed". The packages it suggests as not needed are those that were installed manually, with package-install.

It turns out, the variable package-selected-packages is not saved in the custom.el file after package installation. Why doesn't this work?

The variable's value is updated, and I could save it to the custom.el by manually going to the customization UI for that variable and clicking [save]. It worked automatically before, though.

1 Answer 1


What is the value of C-h v custom-file? This is the file where customisations will be saved, if its not defined then the settings won't be saved.

You can...

(setq custom-file "~/config/emacs/custom.el")

...adjusting for the path you want to save to.

Something to perhaps consider which might help....

You are using and customising Emacs on your computer. The configuration evolves over time and you may wish to have a record of these changes, a backup and the ability to install everything anew, whether that is on the same system or a new one.

To this end version controlling your configuration using Git or similar is a good approach to use.

How does this help with installing packages? Well one approach is to use the use-package to load your packages because it has a neat feature that if a package isn't installed it will install it automatically.

For every package you want to use have a bare minimum in your config of

(use-package <pkg-name>
   :ensure t)

use-package makes it very straight-forward to configure each package with :config, :hooks and more, see the documentation for further details.

You can set things up with regards to which package repository to use in order of preference and load and configure use-package first.

I use ~/.config/emacs/ for all of my configuration and have it under Git version control (you can view the repository on Gitlab if curious).

I use...

(setq package-archives
      '(("GNU ELPA" . "https://elpa.gnu.org/packages/")
        ("NonGNU ELPA"  . "https://elpa.nongnu.org/nongnu/")
        ("MELPA Stable" . "https://stable.melpa.org/packages/")
        ("MELPA"    . "https://melpa.org/packages/"))
      '(("MELPA" . 10)
        ("GNU ELPA" . 5)
        ("NonGNU ELPA"  . 5)
        ("MELPA Stable" . 0)

(when (not package-archive-contents)

;; SETUP use-package, will install if not already present
;;   https://ianyepan.github.io/posts/setting-up-use-package/
(unless (package-installed-p 'use-package)
  (package-install 'use-package))
  (setq use-package-always-ensure t
        use-package-expand-minimally t))

And I then have separate configuration files for different things, some on a broad theme, e.g. settings/magit.el for all Git related settings and settings/lsp.el for all LSP settings and settings/theme.el for my theme/UI customisation.

An advantage of this is that even if a package isn't saved in my ~/.config/emacs/custom.el if for example I added it to the configuration on one system (e.g. home server), committed the changes and pushed them to GitLab then pulled those changes down to another (e.g. work laptop) then it doesn't really matter, when I next start Emacs (or evaluate ~/.config/emacs/init.el) if I'm not booting the second system up the package will be downloaded and installed.

(I should probably one day switch to a fully literate org-mode based configuration but....time!)

Your Answer

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

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