Skip to content

Connect to Remote REPLλ︎

Using a remote REPL can provide a collaborative coding environment as well as a means for working within remote environments (docker images, cloud servers, etc).

Running a remote REPL can also provide longevity to the process, as many clients (editors, ssh terminals) can connect to the REPL over time, disconnecting from the running process without interrupting the process. This provides a very resilient way of running a REPL.

practicalli/clojure-deps-edn provides many aliases to configure a REPL to work with other community tools. These aliases are easier to manage when running a REPL outside of an editor which automatically injects its own configuration. This configuration can be installed into the remote environment and ideally added into the server build process.

Connecting to a remote REPL with a Clojure aware editor

Establish a remote environmentλ︎

A small Virtual Private Server (VPS) is enough for a remote Clojure environment.

Micro VPS on Google Cloud

How to set up a f1-micro VPS on Google Cloud

Services vCore RAM SSD Transfer Monthly
Google Cloud f1-micro VPS
Ionos 1 512Mb 10Gb £1
Amazon Lightsail 1 512Mb 20Gb $3.50
Time4VPS 1 x 2.6GHz 2Gb 20Gb 2TB E3.99

Configure local SSH connectionλ︎

Save the .pem file to ~/.ssh/ directory (or your preferred location).

Edit the ~/.ssh/config file (create the file if needed) and add a host configuration for the remote server.

Add the IP address of the remote server to HostName.

Use the LocalForward for the port used by the nREPL server.

Host remote-clojure-server
  HostName 99.99.99.99
  IdentityFile ~/.ssh/remote-server.pem
  User ubuntu
  PasswordAuthentication yes
  LocalForward 7888 localhost:7888
  Port 22

Configure Emacs to listen to nREPL portλ︎

Connect to a Clojure REPL that was started with nREPL and Cider middleware, e.g. :middleware/cider-clj from practicalli/clojure-deps-edn

Space f e d to open the Spacemacs configuration and add the following code to the dotspacemacs/user-config section

Spacemacs Configuration - dotspacemacs/user-config
  (setq nrepl-use-ssh-fallback-for-remote-hosts t)

=== Emacs Edit the Emacs init.el file and add the following configuration.

  (setq nrepl-use-ssh-fallback-for-remote-hosts t)

Start REPL on remote serverλ︎

The majority of Clojure aware editors can connect to an external REPL using the nREPL protocol. Emacs CIDER, VSCode Calva and NeoVim Conjure all use nREPL and the Cider middleware.

practicalli/clojure-deps-edn defines the :middleware/clj alias that includes several libraries required for the nREPL connection between CIDER and the REPL.

SSH into the remote server.

Clone an existing project with Git, or create a new project with :project/new alias

Change to a Clojure project

Start the REPL using the cider middleware on the same port as defined in LocalForward in the SSH configuration, using the -p to set the port number.

Open a terminal in the root directory of a Clojure project and run a non-interactive REPL process

clojure -M:middleware/cider-clj -p 7888

The REPL will start along with an nREPL server, showing the connection details.

Clojure Terminal REPL UI with Rebel readline

An .nrepl-port file is created in the root of the Clojure project, containing the port number the nREPL server is listening upon.

Using a REPL terminal UI

For a rich terminal REPL experience, start the REPL with rebel readline as well as nREPL and CIDER libraries.

clojure -M:repl/rebel-nrepl

Connect via Trampλ︎

Tramp enables Spacemacs to be used to open remote files as if they were local files, using an SSH connection to the remote server.

SPC f f to open a file and enter an ssh address to access the Clojure project files on the remote server

/ssh:username@remote-server-name:~/project/deps.edn

, ' to call sesman-start and choose cider-connect-clj which should find the remote host and port

Clojure Terminal REPL UI with Rebel readline

Confirm the host name, typically localhost when running the REPL locally

Clojure Terminal REPL UI with Rebel readline

Confirm the port number the nREPL server is listening too

Clojure Terminal REPL UI with Rebel readline

Spacemacs is now connected to the REPL and Clojure code can be evaluated in the source code buffers.