Install Ruby LSP in VS Code for Old Ruby

Pagorn Phusaisakul
2 min readOct 26, 2023

--

Regarding my Ruby by Peng Lv is deprecated. VS Code suggested me to install Ruby LSP. But it’s required ruby version 3 and above!

Image by Aleksey Nemiro from Pixabay

Good luck that we can force Ruby LSP to work on ruby 3.x.x project instead of the real project.

Step by Step

Step 1: Create a new ruby project version 3+

1/ Install and specify your ruby version

$ rvm install 3.1.4
$ rvm use 3.1.4

2/ Initial the new project

$ mkdir ruby-extensions
$ cd ruby-extensions
$ touch Gemfile
$ touch .ruby-version

3/ Edit Gemfile, and .ruby-version. They should looks like these

# Gemfile
source 'https://rubygems.org'

ruby '3.1.4'

# Ruby LSP
gem 'rubocop'
gem 'ruby-lsp'
# .ruby-version
3.1.4

Step 2: Bundle Install the project

$ bundle install

After this, we should get Gemfile.lock

Step 3: Install extensions to VS Code

Step 4: Setting Ruby LSP

In VS Code Settings, add/edit these with your own settings

  • rubyLsp.rubyVersionManager
  • rubyLsp.bundleGemfile
...
"[ruby]": {
"editor.defaultFormatter": "Shopify.ruby-lsp",
...
}
...
"rubyLsp.rubyVersionManager": "rvm",
"rubyLsp.bundleGemfile": "/Users/pagorn/ruby-extensions/Gemfile",
...

Step 5: Restart Vistual Studio Code

  1. Quite all VS Code.
  2. Open VS Code with the new project again.
  3. Click {} on the right bottom, we should see a popup without any error messages
How to check if Ruby LSP works correctly for the new project
How to check if Ruby LSP works correctly for the new project

Step 6: Let’s check the old ruby project

Same as Step 5, but this time we’ll open the old project which ruby version is 2.7.2 Check if Ruby LSP works and can Go to definition

How to check if Ruby LSP works well in the old project
How to check if Ruby LSP works well in the old project

--

--

Responses (2)