# 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**](https://jdk.java.net/23/):

```bash
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`:

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

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

```bash
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:

```bash
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**](https://clojure.org/guides/install_clojure).

## **Install clojure-mode for Emacs**

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

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

> For more information, check the [**clojure-mode repository**](https://github.com/clojure-emacs/clojure-mode).

## **Install clojure-lsp**

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

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

> More details are available on the [**clojure-lsp installation page**](https://clojure-lsp.io/installation/).

## **Configure clojure-lsp for Emacs**

### **Using** `eglot`:

If you prefer `eglot`, configure it as follows:

```lisp
;; 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:

```lisp
(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**](https://clojure-lsp.io/clients/#emacs).
