Hello! Welcome to your first lesson on learning Clojure with Emacs.
This course is designed to get you from the ground up to building practical applications. Given your extensive background in statistics, programming, and finance, we'll move at a pace that respects your experience, focusing on the unique aspects and idioms of Clojure and its ecosystem.
Today's lesson is all about setting up your workshop. The learning outcome is to install the Clojure CLI tools and configure Emacs with CIDER. This is the essential foundation for the interactive, REPL-driven development style that makes Clojure so powerful and productive. We will cover:
- Installing the necessary prerequisites: Java and the Clojure command-line interface (CLI).
- Configuring Emacs by installing CIDER, the bridge between your editor and a running Clojure process.
- Verifying that all the components are working together correctly.
Let's get started.
1. Prerequisites: Java and the Clojure CLI
Clojure is a hosted language that runs on the Java Virtual Machine (JVM). This gives it access to the vast ecosystem of Java libraries and the performance benefits of the highly-optimized JVM. The first step, therefore, is to ensure you have a Java Development Kit (JDK) installed.
- On macOS: You can use Homebrew:
brew install openjdk - On Linux (Debian/Ubuntu):
sudo apt update && sudo apt install default-jdk - On Windows: The recommended path is to use Windows Subsystem for Linux (WSL) and follow the Linux instructions.
With Java in place, the next step is to install the official Clojure command-line tools. These tools are used to manage dependencies, run projects, and start a REPL (Read-Eval-Print Loop). While you may encounter other build tools like Leiningen in older tutorials, the Clojure CLI is the modern, standard approach we will use in this course.
The following resource from Practicalli provides a comprehensive guide to installing the Clojure CLI.
This guide covers the installation of the Clojure CLI tools on various operating systems. It also introduces the deps.edn file, which is central to managing dependencies in Clojure projects.
First, read the introduction to understand the role of the Clojure CLI and deps.edn. Then, follow the installation instructions specific to your operating system (Linux, Windows, or macOS via Homebrew). Finally, read the 'Check Configuration' section and run clojure --version in your terminal to confirm that the installation was successful.
Once you have successfully installed the tools and verified the version, you have completed the first half of today's goal.
2. Configuring Emacs with CIDER
Now that the core Clojure tooling is installed, we need to equip Emacs to communicate with it. This is where CIDER comes in. CIDER stands for the Clojure Interactive Development Environment that Runs on Emacs. It turns Emacs into a full-featured Clojure IDE, allowing you to evaluate code, inspect data, run tests, and debug your programs interactively.
The official CIDER documentation provides the clearest instructions for installation.
This is the official installation guide from the CIDER documentation. It outlines the prerequisites and several methods for installing the CIDER package in Emacs.
Read the 'Prerequisites' section to confirm you have everything needed. Then, review the 'Installation via package.el' and 'Installation via use-package' sections. You only need to follow one of these methods.
Choosing an Installation Method
You have two primary options for installing CIDER.
Method 1: package-install (Direct)
This is the most straightforward method. You can run this command directly within Emacs:
M-x package-install <RET> cider <RET>
(Remember, M-x means holding Alt and pressing x). This will download and install CIDER and its dependencies.
Method 2: use-package (Declarative)
For a more maintainable and organized Emacs configuration, the use-package macro is highly recommended. It allows you to declare packages and their configurations in your init.el file. If you don't have use-package installed, you can add this to the top of your init.el:
;; Install use-package if not already installed
(require 'package)
(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/") t)
(package-initialize)
(unless (package-installed-p 'use-package)
(package-install 'use-package))
(require 'use-package)
Then, you can add the configuration for CIDER:
;; Install and configure CIDER
(use-package cider
:ensure t)
After adding this code to your init.el (usually located at ~/.emacs.d/init.el), you'll need to either restart Emacs or evaluate the expression for it to take effect.
Given your programming background and interest in system architecture, adopting use-package early on will pay dividends in managing your Emacs configuration as it grows.
3. Verifying Your Setup
With the Clojure CLI and CIDER installed, let's perform a quick check to ensure they are working together. The process involves asking CIDER to "jack-in," which means it will start a Clojure REPL process and connect to it automatically.
The following video demonstrates this process. While the presenter uses Leiningen, the CIDER command and the resulting interaction are identical for our Clojure CLI setup.
Emacs: introduction to Clojure (CIDER and clojure-mode)
This clip from a video by Protesilaos Stavrou, a well-known figure in the Emacs community, demonstrates how to start a CIDER session. Pay attention to the command he uses and the REPL buffer that appears.
Watch the section from 01:46 to 03:32. The key command shown is cider-jack-in-clj. Notice how Emacs starts a new process and then presents a REPL buffer, confirming the connection is live.
Now, try it yourself:
- Open Emacs.
- Create a new file by pressing
C-x C-fand name ittest.clj. The.cljextension is important, as it tells Emacs to activateclojure-mode. - With the
test.cljbuffer open, run the commandM-x cider-jack-in-cljand press<RET>. - CIDER will use the Clojure CLI to start a REPL. This may take a few moments the first time as it downloads Clojure itself.
- Once complete, you should see a new window with a buffer named
*cider-repl ...*. It will display a prompt likeuser=>.
If you see the user=> prompt, your installation is successful! You have a direct, interactive connection to a running Clojure environment, all from within Emacs.
Conclusion
In this lesson, you've laid the critical groundwork for your Clojure journey. You now have a complete, professional development environment configured.
Key Takeaways:
- Clojure runs on the JVM and requires a JDK.
- The Clojure CLI (
clojureorclj) is the standard tool for managing dependencies and running Clojure applications. - CIDER is the Emacs package that provides deep integration with a running Clojure process, enabling a powerful, interactive workflow.
- The
cider-jack-in-cljcommand is the primary way to start and connect to a Clojure REPL from Emacs.
You are now perfectly positioned for our next lesson. We will build on this setup to create your first Clojure project, understand the role of deps.edn and the src directory, and begin practicing the fundamental REPL-driven workflow by evaluating Clojure expressions directly from your source files.