Updating packages in Emacs - emacs

I have the following setup for packages (not sure if there is a better recommended one):
(require 'package)
(setq package-archives '(("ELPA" . "http://tromey.com/elpa/")
("gnu" . "http://elpa.gnu.org/packages/")
("marmalade" . "http://marmalade-repo.org/packages/")))
; Apparently needed for the package auto-complete (why?)
(add-to-list 'package-archives
'("melpa" . "http://melpa.milkbox.net/packages/") t)
(package-initialize)
(setq url-http-attempt-keepalives nil)
I have three questions related to the installation and updating of packages.
Q1. Is there a way to update the list of available packages (and most recent versions) and update a specific package?
Q.2 What is the difference between the following package sources?:
ELPA,
GNU
marmalade
melpa
Q.3 Does it matter the order in which they are added to package-archives?

In order to automatically update the list of packages, only if there is no package list already, use the following:
(when (not package-archive-contents)
(package-refresh-contents))
In order to update all installed packages, type package-list-packages, which will take you to the *Packages* buffer (and also update the list of packages), and then type U x.
package-refresh-contents unconditionally tries to download a package list from all repos you've added to package-archives; package-archive-contents is non nil if you have already downloaded the package list.
ELPA is the original. I don't think it's really maintained anymore, but I'm not sure. I don't use it.
GNU is "official". It's maintained along with Emacs, which means things should always work but updates and new packages don't come very often.
Marmalade is basically a website where you can upload a complete package, and it will be added to the marmalade repo. You don't just submit a link to the package's upstream, and it doesn't quite automate the creation of the package completely. I think this is the Right Thing, because you don't necessarily want to track upstream. Unfortunately, it has been unmaintained for a while, but someone recently took it over so it should be back and better at some point.
Melpa takes a URL to e.g. the EmacsWiki lisp area or a github repo, and builds a package automatically from it. Thus it is usually at most a day behind whatever it is tracking. Although it tracks upstream, I've never had a problem in practice, and this is where most of my packages are from. There is also Melpa Stable, which is like Melpa but grabs tagged revisions of the upstream repo instead of the latest revision. Melpa stable has fewer packages than Melpa.
Org mode has its own package.el repo (http://orgmode.org/elpa/).
All of the package repos work the same, you just add them to your package-archives.
Here's a more in-depth blog post about this subject, which I mostly agree with.
I'm not sure, but I think if a package is duplicated in different repos, the order the repos appear in in package-archives determines precedence. I don't know if higher precedence is at the beginning or end of the list.
Update: In Emacs 25, there is a variable package-archive-priorities that you can use to prioritize your package repos (e.g. prefer ELPA over MELPA).
Here is the relevant section of my init.el, if you're interested:
(setq jpk-packages
'(
ac-dabbrev
...
yasnippet
))
(package-initialize)
(add-to-list 'package-archives
'("melpa" . "http://melpa.org/packages/"))
(add-to-list 'package-archives
'("org" . "http://orgmode.org/elpa/"))
;; install any packages in jpk-packages, if they are not installed already
(let ((refreshed nil))
(when (not package-archive-contents)
(package-refresh-contents)
(setq refreshed t))
(dolist (pkg jpk-packages)
(when (and (not (package-installed-p pkg))
(assoc pkg package-archive-contents))
(unless refreshed
(package-refresh-contents)
(setq refreshed t))
(package-install pkg))))
(defun package-list-unaccounted-packages ()
"Like `package-list-packages', but shows only the packages that
are installed and are not in `jpk-packages'. Useful for
cleaning out unwanted packages."
(interactive)
(package-show-package-list
(remove-if-not (lambda (x) (and (not (memq x jpk-packages))
(not (package-built-in-p x))
(package-installed-p x)))
(mapcar 'car package-archive-contents))))

Within Emacs, use M-x list-packages to list all packages which will automatically refresh the archive contents. Afterwards use U to mark all upgradable packages to be upgraded, and x to actually perform the new updates. Emacs will then fetch and install all upgrades, and ask you to whether to remove the old, obsolete versions afterwards.
You may also want to take a look at Carton which provides a more convenient way to manage your packages by declaring them in a dedicated file, and includes a convenient command line client to automatically install and upgrade packages declared in this way.
The order of package-archives does not matter. Emacs aggregates the contents of all archives into a single coherent list of available packages and their versions, stored in package-archive-contents.
Upon package-install, Emacs will simply pick the newest version of a package, regardless of the originating archive. For more control about package origin, MELPA provides the melpa package which allows to black- or whitelist packages from specified archives.

In terminal:
emacs
M-x list-packages
this puts you in the *packages* buffer
shift-u x
emacs will ask you (y/n), wait for updates
C-x k <ret>
this will kill the *packages* buffer and return you back to *scratch*
C-x-C-c
this will exit emacs, and let you relaunch via, but you might have to debug :(
emacs
my 2¢

This is more of an extended comment on jpkotta's answer.
This is an adjustment I am experimenting with for jpkotta's answer above:
(setq n 0) ; set n as 0
(dolist (pkg pkgs-2b-present) ; for each pkg in list
(unless (or ; unless
(package-installed-p pkg) ; pkg is installed or
(assoc pkg ; pkg is in the archive list
package-archive-contents))
(setq n (+ n 1)))) ; add one to n
(when (> n 0) ; if n > 0,
(package-refresh-contents)) ; refresh packages
(replacing (when (not package-archive-contents) (package-refresh-contents))).
The package-list was not refreshing sufficiently often enough for my use-case.
I haven't considered if there is a more efficient solution to my problem; first, I've to see if the problem goes away with this adjustment.

Related

Setting up emacs on new machine with init.el and package installation

Like presumably many emacs users I have my own emacs config file ~/.emacs.d/init.el for configuring emacs the way I like. So when I start using a new machine I copy my emacs config file to it. Now, the problem is that my emacs config file depends on a few packages that I have installed via the emacs package manager, but due to the missing packages I'm unable to successfully install packages.
I can of course start emacs without my config file (emacs -q), but then the problem is that only the default repo is available, so I cannot actually install the package I need to install in order to successfully start emacs with my config file.
So what I have usually done is to temporarily comment out stuff in my emacs config file, so that I'm able to successfully install the packages, and then I can uncomment it and restart emacs with my full config. But this is cumbersome, and usually takes a few tries before I comment out all the needed stuff. Surely there must be a better way that I'm missing?
What you can do is to declare packages you use. Then add some code that runs every time you open Emacs. It checks each package from that list if it has been installed or not. When it's not, it installs it.
A quick example from my config file:
;; first, declare repositories
(setq package-archives
'(("gnu" . "http://elpa.gnu.org/packages/")
("marmalade" . "http://marmalade-repo.org/packages/")
("melpa" . "http://melpa.org/packages/")))
;; Init the package facility
(require 'package)
(package-initialize)
;; (package-refresh-contents) ;; this line is commented
;; since refreshing packages is time-consuming and should be done on demand
;; Declare packages
(setq my-packages
'(cider
projectile
clojure-mode
expand-region
helm
jinja2-mode
magit
markdown-mode
paredit
wrap-region
yaml-mode
json-mode))
;; Iterate on packages and install missing ones
(dolist (pkg my-packages)
(unless (package-installed-p pkg)
(package-install pkg)))
And you're good.
You can place the initialization elisp that installs the packages you need in a separate file, then start emacs -q, then load and evaluate the packages elisp file.
I'm using the following code, in a file of its own, to handle packages. This code also defines the packages I'm using and allows for dynamically adding and loading packages.
If you load this file first thing from your init.el, then you would probably be able to just start Emacs as usual, and missing required packages will be installed automatically.
Update
I was somewhat bothered with the way I used defvar to define the package list variable. I've done some reading and fixed the code below—now it defvar a variable my-packages-package-list and then setq it to the list of packages to install. As far as I understand, this is a more idiomatic way of defining and using variables. As a result, this code now byte-compiled without any warnings.
For those who are interested, some information of using defvar and setq may be found here and in the Emacs` manual.
(require 'package)
(setq package-archives '(("gnu" . "https://elpa.gnu.org/packages/")
;; ("marmalade" . "https://marmalade-repo.org/packages/")
("melpa" . "https://melpa.org/packages/")
("org" . "https://orgmode.org/elpa/")))
(setq package-archive-priorities '(("melpa" . 10)
("gnu" . 5)
("org" . 2)
;; ("marmalade" . 0)
))
(package-initialize)
(when (not package-archive-contents)
(package-refresh-contents))
;; the following code will install packages listed in myPackages if
;; they are not already installed
;; https://realpython.com/emacs-the-best-python-editor/
(defvar my-packages-package-list "List of custom packages to install.")
;;; this allows for dynamically update and install packages while
;;; Emacs is running, by modifying this list, and then evaluating it
;;; and tha mapc expression below it
(setq my-packages-package-list
'(;; add the ein package (Emacs ipython notebook)
ein
;; python development environment
elpy
;; beutify python code
py-autopep8
;; git emacs interface
magit
;; debuggers front end
realgud
;; multiple major mode for web editing
;; multi-web-mode
;; major mode for editing web templates
web-mode
;; docker modes
docker-compose-mode
dockerfile-mode
;; list library for emacs
dash
;; collection of useful combinators for emacs lisp
dash-functional
;; major modes for yaml
yaml-mode
;; major modes for markdown
markdown-mode
;; major modes for lua
lua-mode
;; major modes for fvwm config files
fvwm-mode
;; treat undo history as a tree
undo-tree
;; flychek
;; flychek-clojure
;; flychek-pycheckers
;; Clojure for the brave and true - below; amit - some packages
;; commented out by me until I'll be sure they are needed
;; makes handling lisp expressions much, much easier
;; Cheatsheet: http://www.emacswiki.org/emacs/PareditCheatsheet
paredit
;; key bindings and code colorization for Clojure
;; https://github.com/clojure-emacs/clojure-mode
clojure-mode
;; extra syntax highlighting for clojure
clojure-mode-extra-font-locking
;; integration with a Clojure REPL
;; https://github.com/clojure-emacs/cider
cider
;; allow ido usage in as many contexts as possible. see
;; customizations/navigation.el line 23 for a description
;; of ido
;; ido-ubiquitous
;; Enhances M-x to allow easier execution of commands. Provides
;; a filterable list of possible commands in the minibuffer
;; http://www.emacswiki.org/emacs/Smex
;; smex
;; project navigation
;; projectile
;; colorful parenthesis matching
rainbow-delimiters
;; solarized theme
solarized-theme
;; edit html tags like sexps
;; tagedit
;; help finding keys
which-key
;; xkcd
xkcd
;; Clojure exercises
4clojure
))
(mapc #'(lambda (package)
(unless (package-installed-p package)
(package-install package)))
my-packages-package-list)

package-install can't find icicles

I'm trying to install icicles in Emacs because I've read it makes for a more clear emacs experience. The problem is, even though I'm loading the Melpa repositories, and Checked melpa for if the package was available (it was) If I try package-install on it, it returns [no match].
I've tried package-refresh-contents to na avail. Please help with this, I could do it manually, but AUGH!
Just for context, here's the contents on my .emacs:
;; packages
(require 'package)
(setq package-archives '(("gnu" . "http://elpa.gnu.org/packages/")
("org" . "http://orgmode.org/elpa/")
("marmalade" . "https://marmalade-repo.org/packages/")
("melpa" . "https://melpa.org/packages/")))
(add-to-list 'load-path "~/.emacs.d/elisp")
(defun require-package (package)
(setq-default highlight-tabs t)
"Install given PACKAGE."
(unless (package-installed-p package)
(unless (assoc package package-archive-contents)
(package-refresh-contents))
(package-install package)))
(package-initialize)
(load-theme 'zenburn t)
(require 'php-mode)
(eval-after-load 'php-mode
'(require 'php-ext))
(add-to-list 'auto-mode-alist '("\\.json$" . js-mode))
(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-safe-themes (quote ("f5eb916f6bd4e743206913e6f28051249de8ccfd070eae47b5bde31ee813d55f" 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.
;;
Thanks in advance, all help is very much appreciated
Icicles is no longer distributed on ELPA or MELPA:
NOTE:
Icicles, as well as my other libraries that are on EmacsWiki, used to be obtainable also from MELPA. You may still find some of them there, but they are likely not up-to-date.
As of 2017-10, MELPA has decided to no longer accept Lisp libraries from EmacsWiki. This includes my libraries, even though these libraries are read-only (administrator lock on the wiki pages). Too bad. This means that you must download Icicles and my other libraries only from Emacs Wiki. Sorry about that. I upload Icicles files only to the wiki.
https://www.emacswiki.org/emacs/Icicles_-_Libraries
Solved the problem by doing M-x eval-buffer on my .emacs.

How do I automatically (re)compile ELPA packages?

I'm now installing as much as I can through MELPA and Marmalade, and I manage my ~/.emacs.d using git. However, I have git ignore *.elc files.
This means that when I install a package on one system and then start using another system, git pull only gives me the *.el files. Using these files is often slower than using *.elc.
I tried adding the following to ~/.emacs.d/init.el:
;; load the packages we've installed. Note that package-install
;; byte-compiles the packages, but .elc is ignored by git so we force recompilation here
(byte-recompile-directory (expand-file-name "~/.emacs.d/elpa") 0)
(package-initialize)
Unfortunately, this isn't equivalent to the compilation done by package.el. For example, if I install emacs-eclim, package.el doesn't compile emacs-eclim/company-emacs-eclim.el, and I get the following error:
Leaving directory `/home/wilfred/.emacs.d/elpa'
Compiling file /home/wilfred/.emacs.d/elpa/emacs-eclim-20130310.1237/company-emacs-eclim.el at Mon Mar 11 15:40:01 2013
Entering directory `/home/wilfred/.emacs.d/elpa/emacs-eclim-20130310.1237/'
company-emacs-eclim.el:35:1:Error: Cannot open load file: eclim
Warning: reference to free variable `multiple-cursors-mode'
Warning: reference to free variable `mc--read-char'
Warning: assignment to free variable `mc--read-char'
Warning: reference to free variable `multiple-cursors-mode'
Warning: reference to free variable `mc--read-quoted-char'
Warning: assignment to free variable `mc--read-quoted-char'
Warning: reference to free variable `rectangular-region-mode'
Warning: reference to free variable `rectangular-region-mode'
How do I make Emacs byte-compile only the same files as package.el would?
package.el actually uses exactly the same method, it's just recompiling on startup makes the errors more noticable.
The function used is package--make-autoloads-and-compile, which calls:
(byte-recompile-directory pkg-dir 0 t)
So the original code in the question is correct. However, to recompile a directory that isn't yet compiled, you can do the following:
(require 'dash)
(require 'f)
(defun was-compiled-p (path)
"Does the directory at PATH contain any .elc files?"
(--any-p (f-ext? it "elc") (f-files path)))
(defun ensure-packages-compiled ()
"If any packages installed with package.el aren't compiled yet, compile them."
(--each (f-directories package-user-dir)
(unless (was-compiled-p it)
(byte-recompile-directory it 0))))
(ensure-packages-compiled)
I recommend not keeping the packages in version control (you wouldn't put .o files under revision control, would you?). Here is the code I use to keep my packages in sync:
(setq jpk-packages
'(
ac-dabbrev
...
yasnippet
))
(package-initialize)
(add-to-list 'package-archives
'("melpa" . "http://melpa.milkbox.net/packages/"))
(add-to-list 'package-archives
'("org" . "http://orgmode.org/elpa/"))
(when (not package-archive-contents)
(package-refresh-contents))
(dolist (pkg jpk-packages)
(when (and (not (package-installed-p pkg))
(assoc pkg package-archive-contents))
(package-install pkg)))
(defun package-list-unaccounted-packages ()
"Like `package-list-packages', but shows only the packages that
are installed and are not in `jpk-packages'. Useful for
cleaning out unwanted packages."
(interactive)
(package-show-package-list
(remove-if-not (lambda (x) (and (not (memq x jpk-packages))
(not (package-built-in-p x))
(package-installed-p x)))
(mapcar 'car package-archive-contents))))
I put the above in init.el (which of course is under version control), and it installs any packages that don't exist yet when emacs starts up. Updating is done from the buffer package-list-packages creates. package-list-unaccounted-packages shows all the packages that are installed but not in my jpk-packages list, and makes it easy to remove the ones that I took out of the list.
To answer your specific question, I'd just delete the elpa directory and reinstall everything (using the above code).
The solution I like to the more general question of "How do I automatically re-compile any outdated .elc file" is to use https://github.com/tarsius/auto-compile
I used to have a handful of custom solutions which covered most situations I ran into, but this library covers everything I was doing and more besides. Just initialise it before loading anything else, and you're sorted.
As to the original question of not compiling files which were not originally compiled, that's what the 0 argument to byte-recompile-directory does, so you're explicitly asking for that behaviour. The default behaviour is to only recompile existing outdated .elc files, so you should simply remove that 0.
I do not know the answer to your specific issue about byte-compiling packages exactly the same way package.el would.
However, for your more general problem about maintaining your emacs configuration directory in git while installing extensions through Melpa or marmalade, I suggest you give a look to pallet, which has been designed to solve this very problem.
(byte-compile-file) worked for me when I need to manually recompile packages I have previously installed using elpa.
Particularly for this situation: https://github.com/yjwen/org-reveal/issues/76t
The problem you have is simply that you byte-compile files before initializing your packages. If you switch the two lines, your problem should disappear. The error you get (Cannot open load file: eclim) is because ~/.emacs.d/elpa/emacs-eclim-20130310.1237/ is not yet in your load-path, and package-initialize will add it to your load-path (along with a few other things).

Customize the list of packages that emacs-prelude provides

I see at this link how emacs prelude ensures that a set of packages is installed when emacs starts. I was wondering if I could somehow extend the variable prelude-packages to add some other packages, without changing the prelude-packages.el file?
Barring that I was wondering how I could define a list of packages that are installed at start-up if they aren't currently installed.
You can place a .el file in personal/ directory in Prelude. Prelude loads any .el file it finds there in an alphabetical order. Below is the content of my personal/00-packages.el file.:
(require 'package)
(add-to-list 'package-archives
'("marmalade" .
"http://marmalade-repo.org/packages/"))
(package-initialize)
;; My packages
(setq prelude-packages (append '(
drupal-mode
nginx-mode
) prelude-packages))
;; Install my packages
(prelude-install-packages)
"00" is added to the file name to ensure that the file is loaded before all personal customizations. Add any new package you need to the list being appended to prelude-packages.
Also, if you want to use any mode that is not available in MELPA or Marmalade, you can simply drop the mode's file in personal folder and Prelude will pick it up while loading. If there are any customizations to that mode, simply create another .el file and add the Emacs Lisp code there.
Prelude recommends to use
(prelude-require-packages '(some-package some-other-package))
if you have several package. Or in case you want to add just one package:
(prelude-require-package 'some-package)
If you want you can still maintain your package list in a variable:
(setq my-packages '(drupal-mode nginx-mode toto-mode)
(prelude-require-package my-packages)
In your .emacs file you could add code like this (very similar to the code in the link you sent) to check if each package is installed and install it if is not:
(dolist (package '(eredis anything erlang elnode))
(unless (package-installed-p package)
(package-install package)))
In answer to your question there's no reason you can't do this after the prelude code has run.

What's the magic behind the ELPA?

I use Aquamacs, and I use ELPA that installs files in ~/.emacs.d/elpa?
What's the magic behind this ELPA? I mean, without ELPA, I should download and install the packages in a specific directory, and add those two lines in .emacs.
(add-to-list 'load-path "PACKAGE_DIRECTORY")
(require 'PACKAGE)
But, with ELPA, I don't see anything added to .emacs or /Users/smcho/Library/Preferences/Aquamacs Emacs/{Preferences.el, customizations.el}. How is this possible?
Added
This is what I found with Aquamacs.
Aquamacs reads ~/Preference/Aquamacs Emacs/Preference, and it has "(add-to-list 'load-path kitfiles-dir)(require 'init)", which reads start kit.
The init.el of start kit has the "(require 'package)(package-initialize)"
~/Library/Preferences/Aquamacs Emacs/aquamacs-emacs-starter-kit/vendor has the package.el
I guess the initialization files are not changed, but the package manager reads the ~/.emacs.d/elpd/* to initialize automatically, as I see ***-autoloads.el in each of it.
Added2
With emacs 24, it seems that package is pre-built. I need only to have these lines in .emacs or .emacs.d/init.el to get ELPA working. Hints from this site.
(require 'package)
(add-to-list 'package-archives
'("marmalade" . "http://marmalade-repo.org/packages/"))
(package-initialize)
(when (not package-archive-contents)
(package-refresh-contents))
(defvar my-packages '(clojure-mode
nrepl))
(dolist (p my-packages)
(when (not (package-installed-p p))
(package-install p)))
(package-initialize) will go through all the installed packages (in ~/.emacs.d/elpa/ or similar, depending on configuration), and add them to the load path. One you have run it, take a look at load-path (C-hvload-path), it will have all those subdirectories added. So at this point, file loading will use the normal mechanisms.
You have a (require 'package) (package-initialize) pair somewhere in your initialization files. Package.el does the magic :)