Emacs: Byte Compile Elisp Files

By Xah Lee. Date: . Last updated: .

Emacs lisp files can be byte compiled.

Byte compiled elisp file has file name extension .elc . Normal elisp file is .el.

Advantage of Byte Compiled Elisp File

Byte compiled elisp files will load faster, and also run faster. (by a simple test of a loop, it seems to run about 4 times faster.)

Another advantage is that byte compiling will often tell you errors or warning in your elisp code that you normally wouldn't know.

emacs byte compile 2020-10-14 pCcgM
warnins and tips from byte compile elisp 2020-10-14

When you use ELPA to install packages, they are automatically byte compiled. [see Emacs: Install Package with ELPA/MELPA]

As of today (), for init files and light-weight packages, byte compile doesn't make any noticeable speed difference. But you should byte-compile big lisp packages. (example of heavy weight packages that need or must be byte-compiled: js2-mode, nxml-mode, auto-complete-mode.)

Loading Byte Compiled File

In your init file, when you use load, if you want emacs to load the byte compiled file if it exists, you should not include the “.el” suffix. For example, do it like this

;; load elisp file, use byte compiled version (.elc) if exist
(load "my_emacs_keybinding") ; no file name extension here

[see Elisp: load, load-file, autoload]

How to Byte Compile

There are several ways to byte compile elisp files. The simplest and most useful are:

Alt+x byte-compile-file
Compile a single file. It'll ask for a file name.
Alt+x dired-do-byte-compileB
Compile all marked files in dired.
Alt+x byte-recompile-directory
Batch compile all elisp files in current dir and subdirectory.

Recompile When Upgrade

When you upgrade to a new emcas version, or upgrade packages, or bring over your byte compiled elisp directory from one machine to another, you should recompile your elisp files, because often, emacs has some incompatible elisp changes, and big packages may fail without recompile.


(info "(elisp) Byte Compilation")

2011-07-15 thanks to Adolfo Benedetti [ https://twitter.com/adben ] .

Put $5 at https://www.patreon.com/xahlee , or goto paypal.com and pay to Xah@XahLee.org . Message me on XahLee discord, say so, ask me questions.
Or Buy Xah Emacs Tutorial

Emacs Tutorial

Emacs Init

Emacs Keys


ELisp Examples

ELisp Write Major Mode

Emacs Init

Init Basics


text editing