I am using Emacs 29.1, which ships with use-package
, and I want to install and load the MELPA bm package through the init file.
I am using the following test-init.el
init file:
(defun my-packages ()
(require 'package)
(add-to-list 'package-archives '("gnu" . "https://elpa.gnu.org/packages/"))
(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/"))
(package-initialize)
(require 'use-package)
(setq use-package-always-ensure t)
(use-package bm))
(my-packages)
and I start Emacs (using the new --init-directory
switch) with:
/usr/bin/rm -r /tmp/emadir/*; emacs --init-directory /tmp/emadir -q --load /tmp/test-init.el
That is supposed to start Emacs with test-init.el
and using /tmp/emadir
as init directory, after emptying it.
Emacs gives the following error:
⛔ Error (use-package): Cannot load bm
A possible workaround is to move the (use-package ...)
expression at top level, that is:
(defun my-packages ()
(require 'package)
(add-to-list 'package-archives '("gnu" . "https://elpa.gnu.org/packages/"))
(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/"))
(package-initialize)
(require 'use-package)
(setq use-package-always-ensure t))
(my-packages)
(use-package bm)
That gives no errors, however, this is inconvenient for me, as my real init file is huge and I need to use functions to organise it.
What can I do to nest (use-package ...)
calls inside functions?
(package-install 'bm)
just above(use-package bm)
in your first block fixes it. I don't immediately understand whyuse-package
does not pick up the(setq use-package-always-ensure t)
, but for sure there must be some expert here who can provide us with an explanation :)(unless (package-installed-p 'bm) (package-install 'bm)) (require 'bm)
, do this for every package, and put offuse-package
.(use-package bm)
then it will also work...