Question

When you install Scala using Homebrew you are told

To use with IntelliJ, set the Scala home to:
  /usr/local/opt/scala/idea

What exactly does this mean? Is there a global setting in IntelliJ where you enter this path?

Specifically I'm confused because I'm trying to create SBT-backed projects in IntelliJ. In IntelliJ I do New Project -> Scala:SBT and let the IDE create a default project for me. I get a build.sbt that looks like this:

name := "HelloScala"

version := "1.0"

which looks correct. However, the dependencies are on Scala 2.10.2, while the latest Homebrew-installed Scala on my machine (the one that gets used if I type scala at the command line) is 2.11.0. I assume the problem is that I don't have /usr/local/opt/scala/idea properly set, but I don't see where in the IntelliJ UI to enter this path information. Specifically, none of the IntelliJ Preferences have a text box for "path to Scala distribution" or some such.

How do I properly set up Homebrew Scala with IntelliJ?

IntelliJ 13.1.2, OS X 10.9.2

Was it helpful?

Solution 2

You need to set scalaVersion in your sbt file or else sbt will use the scala version it was built against.

scalaVersion := "2.11.0"

This will make sbt automagically download the scala 2.11 jars into your ivy cache. Both SBT and IDEA will then use that to compile and run your scala code.

So you wouldn't actually need to install it with homebrew at all unless you want to be able to run the scala repl in a console/manually run scalac etc.

It's only when you do not use the SBT integration you need to point out your scala installation in IDEA.

OTHER TIPS

If you want to use the installed version of Scala instead of managed versions by Intelij you do the following:

Open Project Structure in Intelij and edit Dependencies (use a shortcut on mac: ⌘+↓).

Then when you want to add a dependency, the finder won't show directories like /usr etc. A workaround is to temporarily add this folder to the shortcuts sidebar in Finder, like this:

open a terminal and run: open /usr/local/opt/scala/. This will open a finder and show the idea directory. You can drag this to the shortcut sidebar.

Now go back to Intelij and add the directory to Dependencies. I decided to do this under Global Settings (in the left pane). Then next I went to Project Settings > Modules and removed any existing source of scala and clicked on + to add and selected "Library..". Here you should see the scala version you added under a section called Global Library.

Also see https://www.jetbrains.com/help/idea/2016.3/working-with-scala-tests.html

now is 2021 year, for my case:

  • IntelliJ IDEA: IntelliJ IDEA 2020.3.2 (Community Edition)
  • OS: macOS 10.14.6

answer is: NO need set scala location (such as /usr/local/opt/scala/idea)

just need:

  • in IntelliJ IDEA, open *.scala file -> No Scala SDK in module -> click Setup Scala SDK -> Add Scala Support -> for No Library selected, clcik Create

  • selected (here already use brew) installed (by brew install scala) scala SDK

    • enter image description here
  • IntelliJ will auto recognize it -> after indexing then worked -> can debug scala

    • enter image description here

I'm using IntelliJ IDEA 2022.3.2

If you're managing packages on your Mac using Homebrew, the path to specify on x86 Mac is typically /usr/local/opt/scala/idea

Select Scala SDK

Go to File > Project Structure > Global Libraries > Press the + button > Select Scala SDK > Browse > Specify the path "/usr/local/opt/scala/idea"

Specify path installed by brew

Select "Apply" and you should be all set!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top