Clojure environment on Linux with Emacs

This blog provides a comprehensive guide to setting Up a Clojure development environment on Linux with Emacs.

Install JDK (Preferably OpenJDK)

To get started, install OpenJDK. For example, download version 23.0.2 from the official site:

wget https://download.java.net/java/GA/jdk23.0.2/6da2a6609d6e406f85c491fcb119101b/7/GPL/openjdk-23.0.2_linux-x64_bin.tar.gz
tar -xvf openjdk-23.0.2_linux-x64_bin.tar.gz

Add JDK to PATH

To ensure your system recognizes the JDK, set up the JAVA_HOME environment variable and update your PATH.

For Bash, add the following lines to ~/.bashrc:

export JAVA_HOME=/path/to/your/jdk-23.0.2
export PATH=$JAVA_HOME/bin:$PATH

For Fish, update your ~/.config/fish/config.fish:

set -x JAVA_HOME /path/to/your/jdk-23.0.2
set -U fish_user_paths /path/to/your/jdk-23.0.2/bin $fish_user_paths

Install Clojure

To install Clojure, run the following commands:

curl -L -O https://github.com/clojure/brew-install/releases/latest/download/linux-install.sh
chmod +x linux-install.sh
sudo ./linux-install.sh

For more details, visit the official guide.

Install clojure-mode for Emacs

To enable Clojure support in Emacs, install clojure-mode:

M-x package-install [RET] clojure-mode [RET]

For more information, check the clojure-mode repository.

Install clojure-lsp

clojure-lsp provides language server support for Clojure. Install it using:

curl -s https://raw.githubusercontent.com/clojure-lsp/clojure-lsp/master/install | sudo bash

More details are available on the clojure-lsp installation page.

Configure clojure-lsp for Emacs

Using eglot:

If you prefer eglot, configure it as follows:

;; clojure-lsp configuration
(use-package eglot
  :ensure t
  :hook ((clojure-mode . eglot-ensure)
         (clojurec-mode . eglot-ensure)
         (clojurescript-mode . eglot-ensure))
  :config
  (setenv "PATH" (concat "/usr/local/bin" path-separator (getenv "PATH")))
  (add-to-list 'eglot-server-programs '(clojure-mode . ("clojure-lsp")))
  (add-to-list 'eglot-server-programs '(clojurec-mode . ("clojure-lsp")))
  (add-to-list 'eglot-server-programs '(clojurescript-mode . ("clojure-lsp")))
  (add-to-list 'eglot-server-programs '(clojurex-mode . ("clojure-lsp"))))

Using lsp-mode:

If you use lsp-mode, add the following configuration:

(use-package lsp-mode
  :ensure t
  :hook ((clojure-mode . lsp)
         (clojurec-mode . lsp)
         (clojurescript-mode . lsp))
  :config
  ;; add paths to your local installation of project mgmt tools, like lein
  (setenv "PATH" (concat
                   "/usr/local/bin" path-separator
                   (getenv "PATH")))
  (dolist (m '(clojure-mode
               clojurec-mode
               clojurescript-mode
               clojurex-mode))
     (add-to-list 'lsp-language-id-configuration `(,m . "clojure")))
  (setq lsp-clojure-server-command '("/path/to/clojure-lsp"))) ;; Optional: In case `clojure-lsp` is not in your $PATH

For further details, check the clojure-lsp Emacs client guide.


Go to top File an issue