I want to declare all packages that I want to use in emacs in a init.el file. I wonder if its possible to load the missing packages with e.g. MELPA when I startup e
All of these answers will work, but I would highly recommend using use-package
found here: https://github.com/jwiegley/use-package
use-package not only will automatically install your missing packages, but it greatly simplifies your init.el.
Here's an example from my init.el
;; initial package setup
(push "path/to/use-package" load-path)
(require 'use-package)
(require 'package)
(mapc (lambda(p) (push p package-archives))
'(("marmalade" . "http://marmalade-repo.org/packages/")
("melpa" . "http://melpa.milkbox.net/packages/")))
(package-refresh-contents)
(package-initialize)
;; this will install undo-tree if it's not there
;; and it will set it up how I want
(use-package undo-tree
:init (global-undo-tree-mode 1)
:bind (("C-c j" . undo-tree-undo)
("C-c k" . undo-tree-redo)
("C-c l" . undo-tree-switch-branch)
("C-c ;" . undo-tree-visualize))
:ensure t)
Take a look at my init.el here: https://github.com/jordonbiondo/.emacs.d/blob/master/init.el
each use-package block will install the specified package if it is not there, and it encapsulates all my additional setup for packages like keybindings, hooks, and other customizations.