Difference between revisions of "Package.el"

From WikEmacs
Jump to navigation Jump to search
(see also: Borg)
 
(49 intermediate revisions by 26 users not shown)
Line 5: Line 5:
 
|maintainer=[[FSF]]
 
|maintainer=[[FSF]]
 
|source=http://repo.or.cz/w/emacs.git/blob_plain/HEAD:/lisp/emacs-lisp/package.el
 
|source=http://repo.or.cz/w/emacs.git/blob_plain/HEAD:/lisp/emacs-lisp/package.el
|in_emacs=yes
+
|in_emacs=24
 
|Development status=active
 
|Development status=active
 
}}
 
}}
Line 12: Line 12:
  
 
== Install package.el on Emacs 23 ==
 
== Install package.el on Emacs 23 ==
<tt>package.el</tt> is bundled with Emacs 24, but it’s not bound to Emacs 23. Before it became part of Emacs it was an external package, known as ELPA (I guess that stood for Emacs Lisp Package Manager or something similar). So even if you’re an Emacs 23 user you can copy the latest version of <tt>package.el</tt> from [http://repo.or.cz/w/emacs.git/blob_plain/1a0a666f941c99882093d7bd08ced15033bc3f0c:/lisp/emacs-lisp/package.el here] and enjoy it.  
+
<tt>package.el</tt> is bundled with Emacs 24, but it’s not bound to Emacs 23. Before it became part of Emacs it was an external package, known as package.el that was tied to repository called ELPA ('''E'''macs '''L'''isp '''P'''ackage '''A'''rchive). So even if you’re an Emacs 23 user you can copy the latest version of <tt>package.el</tt> from [http://git.savannah.gnu.org/gitweb/?p=emacs.git;a=blob_plain;hb=ba08b24186711eaeb3748f3d1f23e2c2d9ed0d09;f=lisp/emacs-lisp/package.el here] and enjoy it.
  
 +
Alternatively, you can use the great [[el-get]], another package manager for emacs (23 and others) which uses a diversity of sources, included ELPA.
  
 
== Configuration ==
 
== Configuration ==
Line 22: Line 23:
 
</source>
 
</source>
  
== How it works ==
+
== Select Packages from the List ==
What <tt>package.el</tt> basically does is that it connects to a list of package repositories, retrieves the list of the packages there, presents it to you in a interactive fashion and lets you install the packages you like (of course you can also remove the once you don’t like). <tt>package.el</tt> understands the dependencies between packages and if one package requires others to run they will be installed automatically (which is really neat).
+
[[File:M-x package-list-packages.png|400px|thumb|right|M-x package-list-packages]]
 +
<tt>package.el</tt> connects to a list of package repositories, retrieves the list of the packages there, presents it to you in a interactive fashion and lets you install the packages you like (of course you can also remove the ones you don’t like). <tt>package.el</tt> understands the dependencies between packages and if one package requires others to run they will be installed automatically (which is really neat).
  
 
The magic starts with the command {{Command|package-list-packages}}. At this point you should see something in the lines of this.
 
The magic starts with the command {{Command|package-list-packages}}. At this point you should see something in the lines of this.
Line 29: Line 31:
 
You can navigate the list of packages, mark the ones you want to install with the “i” key or the ones you want removed with the “d” key and when you’re done you can press the “x” key to execute the scheduled actions.
 
You can navigate the list of packages, mark the ones you want to install with the “i” key or the ones you want removed with the “d” key and when you’re done you can press the “x” key to execute the scheduled actions.
  
Initially <tt>package.el</tt> didn’t provide the option to update a package, but that should be fixed in recent Emacs builds. According to this [http://lists.gnu.org/archive/html/emacs-devel/2011-09/msg00371.html thread] you can even update all of the installed packages by using the “U” key in the packages list view (I guess that a small “u” would update only one package). Unfortunately my build is lacking those capabilities so I cannot comment of their usability.
+
A little drawback is that some packages won't load automatically at startup. Be sure to read the package description (press Enter), you may have to add a few lines to your .emacs.
 +
 
 +
Initially <tt>package.el</tt> didn’t provide the option to update a package, but that is fixed by now. You can press “U” (<tt>package-menu-mark-upgrades</tt>) to mark all the packages that need an update. To execute all the updates press “x”.
  
 
== How to add additional repository ==
 
== How to add additional repository ==
You’d probably notice that your list of available packages is not particularly long. That’s because the official Emacs 24 package repository has a strict licensing (and source code) requirements to include a package there. Luckily there are a number of community-maintained <tt>package.el</tt> repos around with much more relaxed requirements. Probably the most popular of them is [http://marmalade-repo.org/ Marmalade], created by [http://nex-3.com/ Nathan Weizenbaum] of Sass and Haml fame. You can include it in your <tt>package-archives</tt> list like this:
+
The list of available packages is short because the official Emacs 24 package repository has strict licensing (and source code) requirements to include a package there. Luckily there are a number of community-maintained <tt>package.el</tt> repos around with much more relaxed requirements. The oldest such repository is [http://marmalade-repo.org/ Marmalade], created by [http://nex-3.com/ Natalie Weizenbaum] of Sass and Haml fame. You can include it in your <tt>package-archives</tt> list like this:
 +
 
 +
<source lang="lisp">
 +
(add-to-list 'package-archives '("marmalade" . "http://marmalade-repo.org/packages/"))
 +
</source>
  
<pre class="cl">(add-to-list 'package-archives
 
            '(&quot;marmalade&quot; . &quot;http://marmalade-repo.org/packages/&quot;) t)</pre>
 
 
Marmalade provides a web based UI for package upload and search (both quite buggy unfortunately) and the ability to share the maintenance of a package between several people, who’ll be able to upload new version of the package. There’s also a Emacs Lisp Marmalade tool, that allows you to submit packages directly from Emacs.
 
Marmalade provides a web based UI for package upload and search (both quite buggy unfortunately) and the ability to share the maintenance of a package between several people, who’ll be able to upload new version of the package. There’s also a Emacs Lisp Marmalade tool, that allows you to submit packages directly from Emacs.
  
== How to use it with a custom build ==
+
There's also [http://melpa.org MELPA], which is the largest and most comprehensive package repository: it builds packages directly from the source-code control systems of upstream developers, which can then conveniently be installed by end users via <tt>package.el</tt>.
 +
 
 +
== Install packages automatically on startup ==
 +
 
 
Using the <tt>package.el</tt> UI is ok if you’re a casual Emacs user, but what if you have a custom Emacs configuration, stored under version control, that you’d like to instantly deploy on any OS/machine (like Emacs Prelude). Here in play comes <tt>package.el</tt>’s programmer interface. In [https://github.com/bbatsov/emacs-prelude Emacs Prelude] I use the following code to install a list of required packages automatically on Emacs startup (if necessary):
 
Using the <tt>package.el</tt> UI is ok if you’re a casual Emacs user, but what if you have a custom Emacs configuration, stored under version control, that you’d like to instantly deploy on any OS/machine (like Emacs Prelude). Here in play comes <tt>package.el</tt>’s programmer interface. In [https://github.com/bbatsov/emacs-prelude Emacs Prelude] I use the following code to install a list of required packages automatically on Emacs startup (if necessary):
  
<pre class="cl">(defvar prelude-packages
+
<source lang="lisp">
 +
;; Comment out if you've already loaded this package...
 +
(require 'cl-lib)
 +
 
 +
(defvar my-packages
 
   '(ack-and-a-half auctex clojure-mode coffee-mode deft expand-region
 
   '(ack-and-a-half auctex clojure-mode coffee-mode deft expand-region
 
                   gist groovy-mode haml-mode haskell-mode inf-ruby
 
                   gist groovy-mode haml-mode haskell-mode inf-ruby
Line 47: Line 60:
 
                   sass-mode rainbow-mode scss-mode solarized-theme
 
                   sass-mode rainbow-mode scss-mode solarized-theme
 
                   volatile-highlights yaml-mode yari zenburn-theme)
 
                   volatile-highlights yaml-mode yari zenburn-theme)
   &quot;A list of packages to ensure are installed at launch.&quot;)
+
   "A list of packages to ensure are installed at launch.")
  
(defun prelude-packages-installed-p ()
+
(defun my-packages-installed-p ()
   (loop for p in prelude-packages
+
   (cl-loop for p in my-packages
        when (not (package-installed-p p)) do (return nil)
+
          when (not (package-installed-p p)) do (cl-return nil)
        finally (return t)))
+
          finally (cl-return t)))
  
(unless (prelude-packages-installed-p)
+
(unless (my-packages-installed-p)
 
   ;; check for new packages (package versions)
 
   ;; check for new packages (package versions)
  (message &quot;%s&quot; &quot;Emacs Prelude is now refreshing its package database...&quot;)
 
 
   (package-refresh-contents)
 
   (package-refresh-contents)
  (message &quot;%s&quot; &quot; done.&quot;)
 
 
   ;; install the missing packages
 
   ;; install the missing packages
   (dolist (p prelude-packages)
+
   (dolist (p my-packages)
 
     (when (not (package-installed-p p))
 
     (when (not (package-installed-p p))
 
       (package-install p))))
 
       (package-install p))))
 +
</source>
  
(provide 'prelude-packages)
 
;;; prelude-packages.el ends here</pre>
 
 
This code check if all of the packages in the list are installed and if any of them are not installed if refreshes the local package database (in the case a required package for recently added to the remote repo) and installs them.
 
This code check if all of the packages in the list are installed and if any of them are not installed if refreshes the local package database (in the case a required package for recently added to the remote repo) and installs them.
 +
 +
=== use-package ===
 +
 +
But the [https://github.com/jwiegley/use-package use-package] macro (by J. Wingley, Emacs maintainer) also helps in doing that, and more. It allows to declare packages, install and load them asynchronously and declare their configuration.
 +
 +
=== Cask ===
 +
 +
 +
[https://cask.github.io/ Cask] is a project management tool for Emacs that helps automate the package development cycle; development, dependencies, testing, building, packaging and more.
 +
 +
Cask can also be used to manage dependencies for your local Emacs configuration.
  
 
== How to publish it to Marmalade ==
 
== How to publish it to Marmalade ==
 
To be able to publish a package to Marmalade (or another repo) it should comform a standardized structure. A single-file package might look like this:
 
To be able to publish a package to Marmalade (or another repo) it should comform a standardized structure. A single-file package might look like this:
  
<pre>
+
<source lang="lisp">
 
;;; sass-mode.el --- Sass major mode
 
;;; sass-mode.el --- Sass major mode
  
;; Copyright 2007-2010 Nathan Weizenbaum
+
;; Copyright 2007-2010 Natalie Weizenbaum
  
;; Author: Nathan Weizenbaum <nex342@gmail.com>
+
;; Author: Natalie Weizenbaum <nex342@gmail.com>
 
;; URL: http://github.com/nex3/sass-mode
 
;; URL: http://github.com/nex3/sass-mode
 
;; Version: 3.0.20
 
;; Version: 3.0.20
Line 84: Line 105:
  
 
;;; sass-mode.el ends here
 
;;; sass-mode.el ends here
</pre>
+
</source>
  
 
A multi-file package should have an additional file named
 
A multi-file package should have an additional file named
 
`<name>-pkg.el` that should look like this:
 
`<name>-pkg.el` that should look like this:
  
<pre>
+
<source lang="lisp">
 
(define-package "sass-mode" "3.0.20"
 
(define-package "sass-mode" "3.0.20"
 
                 "Sass major mode"
 
                 "Sass major mode"
 
                 '((haml-mode "3.0.20")))
 
                 '((haml-mode "3.0.20")))
  
</pre>
+
</source>
  
 
== package.el Repositories ==
 
== package.el Repositories ==
  
 +
* [[ELPA]]
 +
* [[MELPA]]
 
* [[Marmalade]]
 
* [[Marmalade]]
* [http://elpa.gnu.org/packages/ ELPA]
+
* [http://slugelisp.ahungry.com/ slugelisp], an alternative website to browse all package.el packages, search by name and quickly see their documentation, and search in their documentation. It is slower than melpa.org but a bit more complete.
* [[MELPA]]
+
 
 +
== Try a package without installing it ==
 +
 
 +
That is the goal of [https://github.com/larstvei/Try Try] (on MELPA). Once installed, you can '''M-x try RET a-package [or url] RET'''. Packages from ELPA will be stored in a temporary directory by default.
 +
 
 +
== See also ==
 +
 
 +
=== [[El-get]], another excellent package manager ===
 +
 
 +
=== Automated package updates ===
 +
 
 +
with [http://melpa.org/#/auto-package-update auto-package-update], on MELPA.
 +
 
 +
=== Try packages without installing them ===
 +
 
 +
This is possible with the package [https://github.com/larstvei/Try Try] (on melpa). Packages will be stored in a temporary directory by default.
 +
 
 +
To try out a package you can run:
 +
 
 +
    M-x try RET multiple-cursors RET
 +
 
 +
you can also put an url pointing to a .el file.
 +
 
 +
=== Straight.el, an alternative package manager ===
 +
 
 +
[https://github.com/raxod502/straight.el straight.el] is a "Next-generation, purely functional package manager for the Emacs hacker".
 +
 
 +
The primary differences are that:
 +
 
 +
* straight.el clones Git repos and builds packages from source, instead of downloading tarballs from a central server
 +
* straight.el uses the init-file as a sole source of truth, whereas package.el stores mutable state outside the init-file
 +
* straight.el focuses on reproducibility and local modification, neither of which are supported in any way by package.el
 +
 
 +
=== Borg, packages as submodules ===
 +
 
 +
[https://github.com/emacscollective/borg Borg] assimilates packages into the ~/.emacs.d repository as Git submodules.  It provides only a few essential features and should be combined with other tools such as Magit, epkg, use-package, and auto-compile.
  
 
== External links ==
 
== External links ==
  
[[Category:Package Manager]]
+
* [http://batsov.com/articles/2012/02/19/package-management-in-emacs-the-good-the-bad-and-the-ugly/ Package Management in Emacs]
 +
 
 +
[[Category:Package Manager]][[Category:Built-in Package]]
 +
[[Category:Key Concepts]]

Latest revision as of 09:49, 15 November 2017

package.el
Description Package Manager
Author Tom Tromey
Maintainer FSF
Source http://repo.or.cz/w/emacs.git/blob_plain/HEAD:/lisp/emacs-lisp/package.el
Part of Emacs Since Emacs 24

package.el is the built-in package manager in Emacs 24.

Install package.el on Emacs 23

package.el is bundled with Emacs 24, but it’s not bound to Emacs 23. Before it became part of Emacs it was an external package, known as package.el that was tied to repository called ELPA (Emacs Lisp Package Archive). So even if you’re an Emacs 23 user you can copy the latest version of package.el from here and enjoy it.

Alternatively, you can use the great el-get, another package manager for emacs (23 and others) which uses a diversity of sources, included ELPA.

Configuration

Put the following snippet of code near the beginning of your Emacs config, since you’ll definitely want packages installed via package.el to be initalized before you start tweaking them.

(require 'package)
(package-initialize)

Select Packages from the List

M-x package-list-packages

package.el connects to a list of package repositories, retrieves the list of the packages there, presents it to you in a interactive fashion and lets you install the packages you like (of course you can also remove the ones you don’t like). package.el understands the dependencies between packages and if one package requires others to run they will be installed automatically (which is really neat).

The magic starts with the command M-x package-list-packages. At this point you should see something in the lines of this.

You can navigate the list of packages, mark the ones you want to install with the “i” key or the ones you want removed with the “d” key and when you’re done you can press the “x” key to execute the scheduled actions.

A little drawback is that some packages won't load automatically at startup. Be sure to read the package description (press Enter), you may have to add a few lines to your .emacs.

Initially package.el didn’t provide the option to update a package, but that is fixed by now. You can press “U” (package-menu-mark-upgrades) to mark all the packages that need an update. To execute all the updates press “x”.

How to add additional repository

The list of available packages is short because the official Emacs 24 package repository has strict licensing (and source code) requirements to include a package there. Luckily there are a number of community-maintained package.el repos around with much more relaxed requirements. The oldest such repository is Marmalade, created by Natalie Weizenbaum of Sass and Haml fame. You can include it in your package-archives list like this:

(add-to-list 'package-archives '("marmalade" . "http://marmalade-repo.org/packages/"))

Marmalade provides a web based UI for package upload and search (both quite buggy unfortunately) and the ability to share the maintenance of a package between several people, who’ll be able to upload new version of the package. There’s also a Emacs Lisp Marmalade tool, that allows you to submit packages directly from Emacs.

There's also MELPA, which is the largest and most comprehensive package repository: it builds packages directly from the source-code control systems of upstream developers, which can then conveniently be installed by end users via package.el.

Install packages automatically on startup

Using the package.el UI is ok if you’re a casual Emacs user, but what if you have a custom Emacs configuration, stored under version control, that you’d like to instantly deploy on any OS/machine (like Emacs Prelude). Here in play comes package.el’s programmer interface. In Emacs Prelude I use the following code to install a list of required packages automatically on Emacs startup (if necessary):

;; Comment out if you've already loaded this package...
(require 'cl-lib)

(defvar my-packages
  '(ack-and-a-half auctex clojure-mode coffee-mode deft expand-region
                   gist groovy-mode haml-mode haskell-mode inf-ruby
                   magit magithub markdown-mode paredit projectile python
                   sass-mode rainbow-mode scss-mode solarized-theme
                   volatile-highlights yaml-mode yari zenburn-theme)
  "A list of packages to ensure are installed at launch.")

(defun my-packages-installed-p ()
  (cl-loop for p in my-packages
           when (not (package-installed-p p)) do (cl-return nil)
           finally (cl-return t)))

(unless (my-packages-installed-p)
  ;; check for new packages (package versions)
  (package-refresh-contents)
  ;; install the missing packages
  (dolist (p my-packages)
    (when (not (package-installed-p p))
      (package-install p))))

This code check if all of the packages in the list are installed and if any of them are not installed if refreshes the local package database (in the case a required package for recently added to the remote repo) and installs them.

use-package

But the use-package macro (by J. Wingley, Emacs maintainer) also helps in doing that, and more. It allows to declare packages, install and load them asynchronously and declare their configuration.

Cask

Cask is a project management tool for Emacs that helps automate the package development cycle; development, dependencies, testing, building, packaging and more.

Cask can also be used to manage dependencies for your local Emacs configuration.

How to publish it to Marmalade

To be able to publish a package to Marmalade (or another repo) it should comform a standardized structure. A single-file package might look like this:

;;; sass-mode.el --- Sass major mode

;; Copyright 2007-2010 Natalie Weizenbaum

;; Author: Natalie Weizenbaum <nex342@gmail.com>
;; URL: http://github.com/nex3/sass-mode
;; Version: 3.0.20
;; Package-Requires: ((haml-mode "3.0.20"))

;; Code goes here

;;; sass-mode.el ends here

A multi-file package should have an additional file named `<name>-pkg.el` that should look like this:

(define-package "sass-mode" "3.0.20"
                "Sass major mode"
                '((haml-mode "3.0.20")))

package.el Repositories

  • ELPA
  • MELPA
  • Marmalade
  • slugelisp, an alternative website to browse all package.el packages, search by name and quickly see their documentation, and search in their documentation. It is slower than melpa.org but a bit more complete.

Try a package without installing it

That is the goal of Try (on MELPA). Once installed, you can M-x try RET a-package [or url] RET. Packages from ELPA will be stored in a temporary directory by default.

See also

El-get, another excellent package manager

Automated package updates

with auto-package-update, on MELPA.

Try packages without installing them

This is possible with the package Try (on melpa). Packages will be stored in a temporary directory by default.

To try out a package you can run:

   M-x try RET multiple-cursors RET

you can also put an url pointing to a .el file.

Straight.el, an alternative package manager

straight.el is a "Next-generation, purely functional package manager for the Emacs hacker".

The primary differences are that:

  • straight.el clones Git repos and builds packages from source, instead of downloading tarballs from a central server
  • straight.el uses the init-file as a sole source of truth, whereas package.el stores mutable state outside the init-file
  • straight.el focuses on reproducibility and local modification, neither of which are supported in any way by package.el

Borg, packages as submodules

Borg assimilates packages into the ~/.emacs.d repository as Git submodules. It provides only a few essential features and should be combined with other tools such as Magit, epkg, use-package, and auto-compile.

External links