34

I've installed the solarized theme package via MELPA. I can select one of the two solarized themes via `customize-theme and it activates it. When I save the theme settings it adds the following to my init.el file:

(custom-set-variables
 ;; custom-set-variables was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(custom-enabled-themes (quote (solarized-dark)))
 '(custom-safe-themes
   (quote
    ("8aebf25556399b58091e533e455dd50a6a9cba958cc4ebb0aab175863c25b9a4" default))))
(custom-set-faces
 ;; custom-set-faces was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 )

When I launch Emacs it doesn't load the theme but I don't get an error message either. In fact running eval-buffer in the init.el file loads the theme.

user2249626
  • 661
  • 1
  • 5
  • 6

5 Answers5

31

add to your init.el

(load-theme 'solarized-dark t)

you can ignore stuff emacs added there, just delete it.

Łukasz Gruner
  • 1,008
  • 8
  • 16
  • 1
    This might work to work around the problem, but it won't solve it. If his custom configs aren't getting loaded, that's something to get to the bottom of. – Malabarba Oct 29 '14 at 15:37
27

By default, Emacs only autoload the packages after processing the init file. So when it was trying to set theme, the theme package wasn't loaded.

Rather than making all packages load in your init file (like what you seem to be doing), you can also load the them after package autoloading by adding a hook to after-init-hook, since packages are autoloaded just after the init file and before after-init-hook. Note that it will not work if you try to load-theme directly in the init file because the package hasn't been loaded at that point.

So alternatively, add this to your init file:

(add-hook 'after-init-hook (lambda () (load-theme 'solarized-light)))
Xinan
  • 371
  • 3
  • 2
  • I think this is the correct solution and works in variety of scenarios (like for me I am using https://github.com/bbatsov/prelude and it works like a charm there too) – Amol Gawai Nov 09 '16 at 16:38
  • This is the correct solution, and should be the accepted answer – Dodgie Jan 10 '17 at 06:11
  • @Dodgie This works but I still get the error message "Failed to enable theme: dracula". The theme does load though. Should I be worried about that error message? – Quinn Culver Mar 07 '21 at 22:31
22

I've added the following to my init.el file (I don't have a .emacsfile in ~).

(setq package-enable-at-startup nil) (package-initialize)

and then at the end

(custom-set-variables
 ;; custom-set-variables was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(custom-enabled-themes (quote (solarized-dark)))
 '(custom-safe-themes
   (quote
        ("8aebf25556399b58091e533e455dd50a6a9cba958cc4ebb0aab175863c25b9a4" default))))
(custom-set-faces
 ;; custom-set-faces was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
)

That seems to work. I'm not very familiar with emacs so I have no idea whether this is a bad workaround...

polypoyo
  • 5
  • 4
user2249626
  • 661
  • 1
  • 5
  • 6
9

I think the important part is (package-initialize). I'm not so sure you need the package-enable-at-startup bit. So:

(package-initialize)
(load-theme 'ample t)

You can also tell emacs to trust all your themes by default so you don't get prompted each time:

(setq custom-safe-themes t)

The thing to keep in mind is that the order to everything is important. Depending on where in the file you're trying to load a theme, some of the above suggestions may or may not be necessary.

I initialize packages and the MELPA repo at the very beginning of my .emacs file which allows me to more easily reference MELPA loaded packages later in the file. I also set custom-safe-themes so I don't have to worry about the fact that Custom is adding the trust info at the end of the file. Here's what I have:

(when (>= emacs-major-version 24)
  (require 'package)
  (package-initialize)
  (add-to-list 'package-archives
      '("melpa" . "http://melpa.milkbox.net/packages/") t)
)
(setq custom-safe-themes t)

..later in the file..

;; Load a nice theme if in GUI
(when (display-graphic-p)
  (load-theme 'ample t)
  )
foobrew
  • 221
  • 2
  • 4
-1

The first line should be to know where to look:

(add-to-list 'custom-theme-load-path "~/.emacs.d/themes/")

then the others can go :)

Stefan
  • 26,154
  • 3
  • 46
  • 84
Gibzo
  • 1