How to Fix Emacs Package Installation Errors — ELPA and MELPA Setup
In this tutorial, you'll learn about How to Fix Emacs Package Installation Errors. We cover key concepts, practical examples, and best practices.
The Problem
Emacs fails to install a package:
Failed to download 'melpa' archive.
or:
Package `company-0.9.13' is unavailable
Quick Fix
Step 1: Configure package archives
WRONG — only GNU ELPA:
(require 'package)
(package-initialize)
RIGHT — add MELPA:
(require 'package)
(add-to-list 'package-archives
'("melpa" . "https://melpa.org/packages/") t)
(package-initialize)
Step 2: Refresh package list
M-x package-refresh-contents
Or in init.el:
(unless package-archive-contents
(package-refresh-contents))
Step 3: Fix GPG key errors
gpg --homedir ~/.emacs.d/elpa/gnupg \
--keyserver keyserver.ubuntu.com \
--recv-keys 066DAFCB81E42C40
Or disable GPG checking (not recommended):
(setq package-check-signature nil)
Step 4: Install packages programmatically
(unless (package-installed-p 'company)
(package-install 'company))
Step 5: Use use-package
(use-package company
:ensure t
:config
(global-company-mode))
Step 6: Fix dependency issues
M-x package-list-packages
Press I to mark dependencies, X to execute.
Prevention
- Add MELPA to access more packages.
- Use
use-packagewith:ensure t. - Import the GNU ELPA GPG key to avoid signature warnings.
Common Mistakes with package install
- Using
foldlinstead offoldl'causing stack overflow on large lists - Forgetting
deriving (Show, Eq)on custom data types needed for debugging - Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
These mistakes appear frequently in real-world EMACS code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro