Buffer Yard
Language setup

Emacs Ruby Setup: A Language Server, a Console and Tests

Updated

You open a Rails app’s app/models/user.rb in a fresh Emacs, type def, and nothing helpful happens. Syntax colouring works, indentation mostly works, and that is the end of it. Ruby support in stock Emacs is a good editor mode with no project awareness, no console and no idea that your project has a .rubocop.yml it should respect.

Fixing that takes four pieces: a major mode, a REPL, a language server and a test runner. The order below is the order that pays off fastest, because a live console is useful before anything else is configured.

Pick the major mode

Emacs ships two Ruby modes. ruby-mode is the long-standing one, written in Emacs Lisp, with its own indentation engine and a decent Flymake backend. ruby-ts-mode arrived with Emacs 29.1 and uses the tree-sitter Ruby grammar for highlighting and indentation. On a 29 or newer build with the grammar installed, ruby-ts-mode is the better default: highlighting of heredocs, nested string interpolation and block arguments is more accurate, and it is faster on long files.

Install the grammar once with M-x treesit-install-language-grammar and answer ruby. If Emacs asks for a repository URL, give it the official tree-sitter/tree-sitter-ruby repository on GitHub. You need a C compiler on the machine for this step; on macOS that means the Xcode command-line tools.

Files without a .rb extension (Gemfile, Rakefile, *.gemspec, config.ru) need their own auto-mode-alist entries, which the snippet further down adds.

A console first: inf-ruby

inf-ruby, from MELPA, runs IRB (or Pry, or rails console) inside an Emacs buffer and adds a minor mode to your Ruby buffers that sends code to it. It is the piece that makes Emacs feel like a Ruby environment rather than a text editor that knows Ruby syntax.

Once inf-ruby-minor-mode is active, C-c C-s starts a console, C-c C-z jumps from your source buffer to the running one, C-c C-r sends the active region, C-M-x sends the method around point and C-c C-k loads the current file. That last one is the quickest way to try a class you just wrote without a test.

In a project, prefer M-x inf-ruby-console-auto. It reads the project root and decides between a Rails console, a Bundler console for a gem, or plain IRB, so the code you send runs with the right load path. Running bundle exec irb by hand inside M-x shell works too, but you lose the send commands.

The language server

Two servers matter for Ruby. Solargraph is the older one, a gem you install with gem install solargraph; it offers completion, go-to-definition and documentation from YARD comments. ruby-lsp, from Shopify, is newer and moves faster. It reads your Gemfile, runs RuboCop for diagnostics and formatting when RuboCop is in the bundle, and needs very little configuration. For a config you build yourself today, ruby-lsp is the better starting point.

Install it either globally (gem install ruby-lsp) or, better for teams, as a development dependency in the Gemfile so everyone gets the same version. eglot, built into Emacs since 29.1, then talks to it. Recent eglot releases already know about Ruby servers, but listing the one you want removes any guessing about which binary it picks.

(use-package ruby-ts-mode
  :mode ("\\.rb\\'" "\\.rake\\'" "\\.gemspec\\'" "Gemfile\\'" "Rakefile\\'" "config\\.ru\\'")
  :hook ((ruby-ts-mode . eglot-ensure)
         (ruby-ts-mode . (lambda ()
                           (add-hook 'before-save-hook #'eglot-format-buffer nil t)))))

(use-package eglot
  :config
  (add-to-list 'eglot-server-programs
               '((ruby-mode ruby-ts-mode) "ruby-lsp")))

(use-package inf-ruby
  :ensure t
  :hook (ruby-ts-mode . inf-ruby-minor-mode))

For Solargraph, replace "ruby-lsp" with "solargraph" "stdio". The :ensure t line needs MELPA in your package archives; the package setup guide covers that in a few lines.

Completion popups come from whatever completion UI you already use. eglot feeds completion-at-point, so Corfu or company show server candidates without extra glue; the completion page explains how to wire that up once for every language.

RuboCop and formatting

With ruby-lsp and RuboCop in the bundle, the before-save-hook line in the snippet is enough: eglot-format-buffer asks the server to format, the server applies RuboCop’s layout rules, and the buffer is saved in house style. Diagnostics from RuboCop show up through Flymake with the rest of the server’s warnings.

If you stay on Solargraph, formatting is less direct. The simplest route is the rubocop package on MELPA, which wraps rubocop commands for the current file, project or directory, or a plain M-x compile with bundle exec rubocop -a on the file. Use -a for safe autocorrections; -A also applies the unsafe ones and deserves a look at the diff afterwards.

One caution. If your project has no .rubocop.yml, RuboCop applies its defaults, and saving an old file can rewrite hundreds of lines at once. Commit before the first save in a legacy codebase.

Running tests from the buffer

Most Ruby projects use RSpec or Minitest, and each has a small MELPA package.

rspec-mode gives you C-c , v to run the spec for the current file, C-c , s for the single example at point, C-c , a for the whole suite and C-c , t to toggle between a class and its spec. Output lands in a compilation buffer, so M-g n walks the failures.

minitest (the minitest package) follows the same C-c , prefix with verify, verify-single and verify-all commands, which makes switching between projects painless.

(use-package rspec-mode
  :ensure t
  :hook (ruby-ts-mode . rspec-mode))

Both packages detect Bundler and run through bundle exec when a Gemfile is present. If your specs need Docker or a custom wrapper script, both packages let you override the command, and a project-local .dir-locals.el is the right place for that so it does not leak into other projects.

Where this leaves you

The result is small: one major mode, one server, one console, one test package, all of it readable in a single screen of init.el. It is the same shape as the Go setup and the Python setup, which is deliberate, because the less each language section differs, the easier it is to debug later.

The languages overview lists the other guides. If you write ERB views as well as Ruby, web-mode handles .html.erb templates well; the HTML and CSS page shows how to hand those files to it and still keep ruby-ts-mode for everything under app/models.

Questions readers ask

Should I use ruby-lsp or Solargraph with eglot?

For a new setup ruby-lsp is the easier pick: it is actively developed by Shopify, picks up RuboCop from your bundle and formats through the server. Solargraph is older and still useful for its type-documentation features, and switching later only means changing one entry in eglot-server-programs.

Does inf-ruby work with Rails and Bundler projects?

Yes. M-x inf-ruby-console-auto looks at the project root and starts the matching console, such as rails console for a Rails app or a Bundler-aware IRB for a gem. If detection fails, M-x inf-ruby always gives you a plain IRB.

Can I keep ruby-mode instead of ruby-ts-mode?

You can. ruby-mode is still maintained, works without a tree-sitter grammar and everything on this page applies to it as well. Swap the mode name in the hooks and keep both modes in the eglot-server-programs entry.