How do I setup the ruby SDK in IntelliJ IDEA?

13,763

Solution 1

I was able to solve this by doing:

Preferences | Plugins | Install JetBrains plugin

enter image description here

Solution 2

I ran into this problem with Intellij IDEA 2020.2. It took a while to resolve because the Intellij documentation seems to be missing one critical piece.

When you first open a project in IDEA, it assumes all your code is part of a Java module. With that module in place you cannot set a Ruby SDK at the module level. There's no option to do because the existing module is configured as Java.

Here's a screenshot of my example project with the default Java module. Notice the icon is a folder with a blue rectangle in the lower, right corner.

screenshot of Project Structure with default Java module

Here's what I had to do.

  1. Open the Project Structure dialog (File | Project Structure).

  2. In the Modules settings, highlight the top-level project module and click the delete button (looks like a minus sign).

  3. Click the plus sign to add a new module.

  4. From the pop-up click "New Module".

  5. In the "New Module" pop-up select Ruby and the correct Module SDK (e.g. rbenv: 2.5.0)

  6. When you are prompted to enter the Module name, Content root, and Module file location, make sure the directories are set to your project's root. When I entered the module name it appended the name to the project's root directory which is probably not what you want.

Once you've done that the module should appear with a Ruby icon on it and the rest should work as documented here: https://www.jetbrains.com/help/idea/configuring-language-interpreter.html

Here's a screenshot of my new Ruby module. You can see the Ruby icon replaced the blue rectangle.

screenshot of Project Structure with Ruby module

I hope this saves you some time!

Solution 3

In order to set Ruby SDK for your project in IDEA you need to go to File | Project Structure | Project Settings and set project and module SDK.

Solution 4

Olivia is correct, the "Project Structure..." (Mac shortcut Cmd+;) is the dialog for configuring the IDE to use different ruby installations.

The first requirement is that the ruby manager (chruby, rbenv, rvm) is configured properly.

Another concern is exactly where and how jetbrains expects the ruby installation to be organized. I wonder if Intellij is compatible with the way ruby-install lays out ruby? That's the one I used with chruby. I could not make it work.

The posted url to Opening Rails projects in IntelliJ IDEA helped me feel the most confidence that we are on the right track. :)

At the time, I was failing to get Intellij configured due to fact the gem files were not seen by the IDE. All but about 4 gems in my project's Gemfile was being highlighted as having an SDK problem.

To end this, I stopped using chruby and ruby-install. I am not blaming chruby, however I could not make the chruby system work properly with the IDE. It worked fine in the shell. Note, I am a previous user of rvm and rbenv. Switching back to rbenv, now. Note that I've also stopped using ruby-build directly.

Instead, I built the first ruby version directly from rbenv:

rbenv install 2.3.1

Next, I created the .ruby-version file in the root of my rails project directory by running:

rbenv local 2.3.1

To help with any confusion regarding the minimum support needed in the shell startup scripts. Do not alter PATH at all. Place the following in your shell startup script system, whatever that may be:

# rbenv config in my .bash_profile
# --------------------------------
if which rbenv > /dev/null; then
  eval "$(rbenv init -)";
fi

With the prerequisites out of the way...

I recommend doing the project import in the same way described in the jetbrains tutorial Opening Rails projects in IntelliJ IDEA.

In the first screenshot from thesowismine, I see two dialogs for different purposes that are simultaneously open. Unless I am mistaken, one of those dialogs should have received its info and closed before going forward, at least during the wizard-like process. Perhaps this was done and that dialog was reloaded later?

In any event, that is not where the Ruby SDK is associated to a Rails project. The screenshot indicates the User is browsing around the brew Cellar, which may indicate two things.

  1. Ruby was installed with a brew install <version> command; I installed ruby by calling the rbenv ecosystem.
  2. That particular dialog is for informing the IDE about the Rails project folder, not the Ruby kit.

Assuming the first dialog is provided with the root directory of the Rails app and next is clicked; then in the second dialog, I change nothing and click next. The 3rd dialog is where I confirm the project directory is correct and I can assign a more elaborate name (which is displayed in the IDE's project menu). Clicking next may prompt you to write over the ".idea/" directory. Say yes. The next dialog confirms that sources were found. Click next. Now Frameworks begin to be detected assuming the Ruby Manager is setup correctly. Click Finish.

Now, goto the "Project Structure..." dialog to set the SDK.

Before or after setting the SDK, go to the the project directory of your Rails project, run:

gem install bundle
bundle install

This will install all the gems your project requires including the rails gem, as presumably it is listed in your Gemfile.

Note that gemsets do not come up, here. In this config, the set of gems are associated to a particular Ruby installation. Bundler is your friend.

Solution 5

Languages & Frameworks > Ruby SDK and Gems

Share:
13,763
thesowismine
Author by

thesowismine

Updated on July 23, 2022

Comments

  • thesowismine
    thesowismine 10 months

    I've been using this guide

    Whenever I go to import the module I get this screen:

    enter image description here

    I used the following file path, maybe this is whats wrong? /usr/local/Cellar/ruby-build/20160130/share/ruby-build/2.3.0

    And got this error:

    enter image description here

    I'd appreciate any ideas anyone might have, i've been trying get this working all night!

    • Jon
      Jon over 7 years
      Try installing Rvm. Works seamlessly for me with IntelliJ
    • thesowismine
      thesowismine over 7 years
      @Jon i used rbenv instead, I followed this guide for installing setting up rails and would rather not stray from it at this point if possible
    • Jon
      Jon over 7 years
      I'm pretty certain the example screenshots in the jetbrains guide you're following also show Rvm being used.
    • thesowismine
      thesowismine over 7 years
      @Jon whoops, sorry, i forgot to link the guide I was talking about, which has nothing to do with intellij IDEA gorails.com/setup/osx/10.11-el-capitan
    • Jon
      Jon over 7 years
      Ah. I'd still use Rvm if I were you. Installation is trivial and IntelliJ works perfectly with it.
  • thesowismine
    thesowismine over 7 years
    I don't have that option, just a bunch of other stuff including but not limited to JavaScript, Play Configuration, Grails, OSGI, SQL Dialects, and XSLT
  • Josh Brody
    Josh Brody over 7 years
    Oh, oops; for whatever reason I assumed you're using RubyMine. Might want to give that a whirl; it's baked for Ruby. :)
  • thesowismine
    thesowismine over 7 years
    Yea I might end up doing that. I really want to get this to work though because I've done it before and would rather use the same IDE for everything if possible.
  • Josh Brody
    Josh Brody over 7 years
  • thesowismine
    thesowismine over 7 years
    Yea thats what I was using but it seems to varie a little when you go to import the path
  • ziff
    ziff about 7 years
    Thank you for following up so promptly. Good job out of you! That certainly completes the picture.
  • WebComer
    WebComer over 6 years
    For me, the mentioned Ruby option is missing in Languages & Frameworks in idea 15. Just cannot add Ruby facet yet Ruby sdk's listed in sdk list. Ruby files just a piece in java project.
  • akostadinov
    akostadinov over 1 year
    I'm using asdf-vm and IntelliJ recognizes it. Just FYI