Published on

Using rbenv with IntelliJ


I have been working with Rails quite a bit more lately. As a result, I had to find a way to run Rails in my goto ide - IntelliJ.

IntelliJ with the official Ruby plugin does work but getting it setup is not immediately obvious especially if you use a Ruby version manager like rbenv. The official IntelliJ guide on setting this up does not really help much, it basically says you can set up the IDE to use rbenv and other Ruby version managers.

After a bit of fiddling and Googling I came up with the following steps which work:

  • Install rbenv. I found using the OS's official package managers was not ideal for this as there were issues getting the rbenv path in IntelliJ. Instead I installed it manually as per the instructions on the repo
  • Use rbenv to install the desired Ruby version, for example, to install 2.6.6 and set it as the global version, we would run the following 2 commands: rbenv install 2.6.6 and then rbenv global 2.6.6
  • Point IntelliJ to your rbenv path:
    • Open project settings (Ctrl+alt+shift+s)
    • Choose the SDK's tab on the left
    • Click the + button
    • Choose Add Ruby SDK... > new local version manager...
      • This is normally a symlink under /bin/rbenv (which symlinks to /usr/lib/rbenv/libexec/rbenv)
        • Once adding nothing may happen. Close and open the IDE again to fix this
  • Update your project structure to use the SDK globally
    • Open Project structure ctrl+alt+shift+s
    • Click Project in the left tab
    • For the first drop-down click the down arrow and choose the SDK you added previously
    • You may need to restart the ide for it to properly take effect (in my case I could click the debug button but the run button was greyed out until I restarted)

To run and debug the app in IntelliJ we need to create a run configuration:

  • Click the down arrow next to configurations, Edit configurations...
  • +
  • Choose Rails
  • Name the config whatever you want
  • Ensure port 3000 is used (assuming the app is running on 3000, the default)
  • For SDK choose the one you just added

The one issue I ran into was for the pg (postgres) gem. When trying to run bundle I received the error: libpq-dev : Depends: libpq5 (= 12.4-0ubuntu0.20.04.1) but 12.4-1.pgdg20.04+1 is to be installed. This ended up being really easy to fix using homebrew/linuxbrew to install postgres as follows: brew install postgresql

If the run configuration works properly you should see output similar to the below after executing it:

/usr/bin/zsh -c "bash -c 'env RBENV_VERSION=2.6.6 /usr/lib/rbenv/libexec/rbenv exec ruby /home/yourUser/Desktop/code/1-os/path/to/rails/app/bin/rails server -b -p 3000 -e development'"
=> Booting Puma
=> Rails application starting in development
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.12.6 (ruby 2.6.6-p146), codename: Llamas in Pajamas
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://
Use Ctrl-C to stop