How to use Scala plugin for IntelliJ on Debian? - scala

I have IntelliJ Idea Community Edition v 123.72, with Scala Plugin 0.6.371 on an Debian unstable box that has packages scala and scala-library v 2.9.2+dfsg-1. When creating a new Scala module, I am asked for either "SCALA_HOME" or an "Existent Library" with two empty drop downs for "compiler library" and "standard library".
Is it possible to use the distro scala packages with the plugin? If so, how do I fill out the
"new project form"?
If not, where do I find a Linux binary distro of Scala?
(It all seems straight-forward, but my Google-fu fails me.)

Download the latest version of Scala here and then set scala home to the unzipped directory (can be anywhere). IntelliJ will take care of the rest.

Alternatively, recreate the directory structure that intelliJ is looking for by using mkdir and ln -s.
The Debian package manager installs bits and pieces of scala all over the root directory. I found the libs that intelliJ is looking for in /usr/share/java
To create system links and satisfy intelliJ you can try the following:
mkdir /usr/share/java/lib
ln -s /usr/share/java/scala-library-X.X.X.jar /usr/share/java/lib/scala-library.jar
ln -s /usr/share/java/scala-compiler-X.X.X.jar /usr/share/java/lib/scala-compiler.jar
export SCALA_HOME=/usr/share/java
Why aptitude drops scala nuggets all over the place, instead of a more common, central location such as /usr/lib/scala remains a mystery to me. Anyone?

Related

How to install scala 2.12

There are multiple binary incompatible scala 2 versions, however the document says the installation is either via IDE or SBT.
DOWNLOAD SCALA 2
Then, install Scala:...either by installing an IDE such as IntelliJ, or sbt, Scala's build tool.
Spark 3 needs Scala 2.12.
Spark 3.1.2 uses Scala 2.12. You will need to use a compatible Scala version (2.12.x).
Then how can we make sure the scala version is 2.12 if we install sbt?
Or the documentation is not accurate and it should be "to use specific version of scala, need to download specific scala version on your own"?
Updates
As per the answer by mario-galic, in ONE-CLICK INSTALL FOR SCALA it is said:
Installing Scala has always been a task more challenging than necessary, with the potential to drive away beginners. Should I install Scala itself? sbt? Some other build tools? What about a better REPL like Ammonite? Oh and before all that I need to install Java?
To solve this problem, the Scala Center contracted Alexandre Archambault in January 2020 to add a one-click install of Scala through coursier. For example, on Linux, all we now need is:
$ curl -Lo cs https://git.io/coursier-cli-linux && chmod +x cs && ./cs setup
The Scala version is specified in the build.sbt file so SBT will download the appropriate version of Scala as necessary.
I personally use SDKMAN! to install Java and then SBT.
The key concept to understand is the difference between system-wide installation and project-specific version. System-wide installation ends up somewhere on the PATH like
/usr/local/bin/scala
and can be installed in various ways, personally I recommend coursier one-click install for Scala
curl -Lo cs https://git.io/coursier-cli-linux && chmod +x cs && ./cs setup
Project-specific version is specified by scalaVersion sbt settings which downloads Scala to coursier cache location. To see the Scala version and location used by the particular project try show scalaInstance which
inspect scalaInstance
[info] Task: sbt.internal.inc.ScalaInstance
[info] Description:
[info] Defines the Scala instance to use for compilation, running, and testing.
Scala should be binary compatible within minor version so Spark 3 or any other software built against any Scala 2.12.x version should work with any other Scala 2.12.x version where we have major.minor.patch. Note binary compatibility is not guaranteed for internal compiler APIs, so for example when publishing compiler plugins the best practice is to publish it against full specific Scala version. For example notice how kind-projector compiler plugin is published against full Scala version 2.13.6
https://repo1.maven.org/maven2/org/typelevel/kind-projector_2.13.6/
whilst scala-cats application-level library is published against any Scala 2.13.x version
https://repo1.maven.org/maven2/org/typelevel/cats-core_2.13/
Similarly spark is published against any Scala 2.12.x version
https://repo1.maven.org/maven2/org/apache/spark/spark-core_2.12/
Regarding system-wide installation one trick I do for quick switching of versions is to put scala-runners on the PATH and then different versions can be launched via --scala-version argument
scala --scala-version 2.12.14
Using coursier or scala-runners you can even switch JDK quickly via -C--jvm for example
scala --scala-version 2.12.14 -C--jvm=11
For a project, there should be no need to download manually a specific version of Scala. sbt either directly or indirectly via an IDE will download all the dependencies behind the scenes for you, so the only thing to specify is sbt setting scalaVersion.
Using Python as analogy to Scala, and Pipenv as anology to sbt, then python_version in Pipfile is similar to scalaVersion in build.sbt. After executing pipenv shell and pipenv install you end up with project-specific shell environment with project specific Python version and dependencies. sbt similarly downloads project specific Scala version and dependencies based of build.sbt although it has no need for lock files or for modifying your shell environment.

Installing IntelliJ + Scala under Ubuntu

I am a bit confused right now. I have installed IntelliJ and the Scala SDK using the installation wizard provided by IntelliJ. I selected the latest version 2.11.8 but were not able to run it in the terminal.
After that I ran
sudo apt-get install scala
which is apparently version 2.9.2
scala -version
Scala code runner version 2.9.2 -- Copyright 2002-2011, LAMP/EPFL
It appears that I now have installed scala under
/home/<user>/.ivy2/cache
(which comes from IntelliJ and has version 2.11.8) and also
whereis scala
scala: /usr/bin/scala /usr/bin/X11/scala /usr/share/man/man1/scala.1.gz
which comes from the package manager and hast version 2.9.2.
How would I clean up this mess?
This is not really a mess. It's perfectly possible to install a system-wide Scala and one based on your projects. The system-wide one really makes little sense except for you to run shell scripts using scala script.scala. You can also see that those Debian and Ubuntu packages are horribly outdated.
Since Scala is not an end-user application but a compiler or library required by specific project builds, one usually does not need and/or use a global installation. When you build a project using sbt or IntelliJ, your project can freely choose the Scala version it wants to use, whether that's 2.9.2, 2.10.x, 2.11.x or 2.12.0-Milestone... And therefore, these installations are all kept concurrently in a cache such as the Ivy cache you are seeing from IntelliJ/sbt.
$ ls -la ~/.ivy2/cache/org.scala-lang/scala-compiler/jars/
... scala-compiler-2.10.0.jar
... scala-compiler-2.10.2.jar
... scala-compiler-2.10.3.jar
... scala-compiler-2.10.4.jar
... scala-compiler-2.10.5.jar
... scala-compiler-2.10.6.jar
... scala-compiler-2.11.0.jar
... scala-compiler-2.11.1.jar
... scala-compiler-2.11.2.jar
... scala-compiler-2.11.5.jar
... scala-compiler-2.11.6.jar
... scala-compiler-2.11.7.jar
... scala-compiler-2.11.8.jar
... scala-compiler-2.12.0-M2.jar
... scala-compiler-2.12.0-M3.jar
... scala-compiler-2.7.3.jar
... scala-compiler-2.8.0.jar
... scala-compiler-2.8.2.jar
... scala-compiler-2.9.0.jar
... scala-compiler-2.9.2.jar
If you want a global installation because you want to run scripts without setting up a minimal sbt project, I recommend not to use apt (you can uninstall scala again) but simply download the latest version from http://scala-lang.org/download/ - I'm not aware of any .deb package depending on a .deb Scala installation.

Multiple scala libraies causing error in intellij?

I am using intellij 14 with scala 2.11.6 installed using home brew and symlink using
ln -s /usr/local/Cellar/scala/2.11.6/libexec/src /usr/local/Cellar/scala/2.11.6/src
ln -s /usr/local/Cellar/scala/2.11.6/libexec/lib /usr/local/Cellar/scala/2.11.6/lib
mkdir -p /usr/local/Cellar/scala/2.11.6/doc/scala-devel-docs
ln -s /usr/local/Cellar/scala/2.11.6/share/doc/scala /usr/local/Cellar/scala/2.11.6/doc/scala-devel-docs/api
I tried running a simple hello world but run into the following issue.
Error:scalac: Multiple 'scala-library*.jar' files (scala-library.jar, scala-library.jar, scala-library.jar) in Scala compiler classpath in Scala SDK scala-sdk-2.11.6
Edit:
So I check the compiler class path on global libraries and apparently there are multiple scal-library.jar
file:///usr/local/Cellar/scala/2.11.6/idea/lib/scala-library.jar
file:///usr/local/Cellar/scala/2.11.6/lib/scala-library.jar
file:///usr/local/Cellar/scala/2.11.6/libexec/lib/scala-library.jar
Does anyone know why?
Maybe you have used
/usr/local/Cellar/scala/2.11.6/
as the path for Scala SDK?
When you install scala with homebrew that path will contain not only the scala libraries, but also a symlink with the relevant libraries for intellij. So if you use the top-level install directory intellij will find the libraries twice.
Instead you should use
/usr/local/Cellar/scala/2.11.6/idea/lib
I had the same issue than you experimented and the solution, actually very easy, was actually erasing the .idea folder from the project, the problem is that the configuration inside of this folder (containing the set ups for example for the test, VCS, the runs, etc) gets corrupted with double entries (probably cos you update your Scala version), once you do this and reopen the project in Intellij the IDEA will generate a fresh new configuration for you.
This worked for me.
I'm using Idea 2019.2.2 and Windows 10.
In the folder .idea/libraries/ I had two files: sbt__org_scala_lang_scala_library_2_13_0_jar.xml and sbt__org_scala_lang_scala_library_2_13_0_jar2.xml.
I deleted the second file.
Then I opend the first one and there were duplicate lines:
<root url="file://$USER_HOME$/AppData/Local/Coursier/cache/v1/https/repo1.maven.org/maven2/jline/jline/2.14.6/jline-2.14.6.jar" />
<root url="file://$USER_HOME$/AppData/Local/Coursier/cache/v1/https/repo1.maven.org/maven2/org/scala-lang/scala-compiler/2.13.0/scala-compiler-2.13.0.jar" />
<root url="file://$USER_HOME$/AppData/Local/Coursier/cache/v1/https/repo1.maven.org/maven2/org/scala-lang/scala-compiler/2.13.0/scala-compiler-2.13.0.jar" />
<root url="file://$USER_HOME$/AppData/Local/Coursier/cache/v1/https/repo1.maven.org/maven2/org/scala-lang/scala-library/2.13.0/scala-library-2.13.0.jar" />
<root url="file://$USER_HOME$/AppData/Local/Coursier/cache/v1/https/repo1.maven.org/maven2/org/scala-lang/scala-library/2.13.0/scala-library-2.13.0.jar" />
<root url="file://$USER_HOME$/AppData/Local/Coursier/cache/v1/https/repo1.maven.org/maven2/org/scala-lang/scala-reflect/2.13.0/scala-reflect-2.13.0.jar" />
So I deleted duplicates and errors disappeared.
Hope this will help someone else.
I also ran into that error. The fix I found was to remove the duplicate scala-library in the .iml file generated by intellij.
Basically I located the relevant .iml file by grepping the scala version, and found that two scala-library in that file. I removed the scala 2.11 version, and then it works.
Remove the following line in build.sbt:
...
scalaVersion := "2.13.0"
...
Try rebuilding and running it again
2019 update... I'm running Ubuntu Linux and IntelliJ community 2019.1 with sbt 2.13.1 and got the exact same error. I also found that if I built directly from sbt with "runMain package.MyClass" it worked, so I knew it must be an IntelliJ problem, not a "real" problem.
Anyway, I found the file .idea/libraries/sbt.. lots of crazy long name ... scala_library_2_13_1_jar.xml.
In it, there was an XML block , and in that block, two entries were duplicated:
I first noticed the scala-library one, and after deleting that, the error report came up about the scala-compiler duplicate too.
After deleting both duplicates, my build is now working.
To fix the problem go to project structure in intellij and go to global libraries
It should be like this
After that remove all the scala SDKs by hitting the - mark
Next, click the + and choose your Scala SDK version to add
After that please make sure to apply the changes and re-run the program
You have JAR files from multiple versions of scala-library.jar. In order to make the error go away you will have to delete the duplicates. To figure out which version you want to keep, you can view the manifest file inside each JAR:
META-INF/MANIFEST.MF
Inside the manifest file you should see something like this:
Manifest-Version: 1.0
Class-Path:
Implementation-Title: Scala-Library
Implementation-Version: 2.11.4
The error is happening because IntelliJ cannot figure out which version of a given Scala class to use.
Delete multiple versions of scala-library in sbt, leaving one.
Having similar symptoms, but on a Ubuntu machine, not using brew:
I am using /usr/share/sbt/bin/sbt-launch.jar as launcher. To fix the mentioned problem, I've had to purge the directories 1) project, 2) target and 3) .idea of the relevant Scala projects, doing sbt refresh in IntelliJ (View - Tool Windows -> sbt, hit the Reimport all sbt projects icon), and rebuild all modules afterwards.
As a last step, when the error occurs further, switch over to Ubuntu shell / terminal, and do a sbt clean compile in the problematic project folder. Fix the compile issues if some are occuring. If this does not help, change scalaVersion in build.sbt e.g. from 2.12.9 to 2.12.8 (the error occurs more often with 2.12.9), remove scalaOrganization definition (but keep organization). Repeat the sbt refresh of IntelliJ. OMG.
This worked for me:
In IDEA
Preferences -> Plugins -> Scala -> Update
Restart IDEA

Why does activator fail with "Could not retrieve Scala 2.11.5: missing scala.Option, scala.tools.nsc.Global" at startup?

I'm using Typesafe Activator 1.3.2 on Windows 8.
Whenever I try to start activator using activator command, I am getting the following error:
C:\path>activator
Getting Scala 2.11.5 (for activator-launcher)...
:: retrieving :: org.scala-sbt#boot-scala
confs: [default]
0 artifacts copied, 6 already retrieved (0kB/15ms)
Error: Could not retrieve Scala 2.11.5: missing scala.Option, scala.tools.nsc.Global
Can any one help me on this issue?
Deleting ~/.sbt/boot fixes this issue.
I'm pretty sure the following directories have been corrupted and should be removed in that order:
~/.ivy2
~/.sbt
You may also want to rename them first before deleting (in case deleting them does not help and could be restored easily rather than downloading the whole Internet again).
I'm not on Windows and can't tell you exactly what ~ is resolved to on Windows 8 - it's the home directory of the logged-in user, and unless I'm mistaken it was something like %userprofile%.
Uninstall and Install
1) Scala plugin in intelliJ
2) SBT plugin in intelliJ
Restart the intelliJ and run the apps agian.
The below solution has worked for me.
NOTE: Below answer is specific to IntelliJ. But trying the same approach in Eclipse or other IDEs will also work.
In most cases your project JDK is not compatible with Scala or sbt version, try to change the Scala Compile Server SDK.
Press Ctrl+Alt+S to open Settings/Preferences dialog.
From the options on the left, select Build, Execution, Deployment | Compiler | Scala | Scala Compiler Server.
In the JVM SDK field specify the appropriate SDK.
I recommend that you switch to Java 8 SDK since the Scala version 2.12, and sbt versions 1.10 and later are not compatible with older Java SDK versions.
Deleting below two folders fixed the intelliJ error for me
rm -rf ~/.ivy2
cd ~/.sbt
rm -rf boot

Compiling Scala^Z3 on Windows

I tried to compile Scala^Z3 on Win XP using Cygwin and JDK 1.7.0 but it didn't work out as expected.
I did the following:
- Use SBT 0.7.4
- Use current Scala^Z3 revision from github
- Use Cygwin and its gcc
- Use JDK 1.7.0 (javac)
"sbt update" was successfull.
"sbt package" end up in several errors stating undefined references like the following:
\psuter-ScalaZ3-35cb691\src\c/z3_Z3Wrapper.c:10: undefined reference to `_Z3_mk_config'
In order to make it work at all I changed ....\PSuterScalaZ3\psuter-ScalaZ3-35cb691\project\build\scalaz3.scala line 74 to:
lazy val gcc : ManagedTask = if(isUnix || is32bit) {
On the homepage it is stated that it should work for Windows, too. Does it at all?
Is there a precompiled jar available?
I saw a z3.jar here: http://lara.epfl.ch/~psuter/jniz3/z3.jar
This is a Linux Version too, I guess? Because it didn't work for me either...
Scala^Z3 is a really good piece of code (if i can get it to work ;))
Sorry about that, the sbt script indeed currently only works for Linux (and as you can tell by the absolute path, we're not quite used to having external users yet).
Here are the steps I use to compile it under Windows:
compile all Java sources with javac (there are no dependencies)
generate the header files using javah
compile all the Scala sources with scalac (using only the Java .class files as dependencies)
compile the .c + .h files with Visual Studio
manually create a jar file with everything
We also hope to release a precompiled .jar file with the shared library for Linux and Windows once we adapted Scala^Z3 to the new changes in Z3 3.1.
EDIT The GitHub repository now contains a precompiled .jar file prepared for Scala 2.9.1 and Z3 3.2. It works under Windows and Linux (32bit). The repository also contains more detailed instructions on how to compile the shared library in Windows, using MinGW instead of Visual Studio (hence removing the need for VS runtime libraries).
I had a similar problem some months back and here is what it I had to do in order to compile it with Visual Studio 2010. I am not sure if it is still relevant since Scala^Z3 and Z3 itself changed a lot, but I hope it nevertheless is helpful.
Created a new Visual C++ Win32 project (.NET Framework 4) for
creating DLLs.
Added all .h and .c files in the src/c/ directory. VC somehow
complained about the "inline" modifier and a work mate suggested to
remove them, which I did.
Added z3.h from Z3 2.19, Z3 2.16 wasn't accepted. Also added the
corresponding z3.lib (x86, haven't tried x64 yet). VC wouldn't accept
z3.dll and complaint about the file being corrupt. No clue why, Z3
itself works fine for me.
The project compiles with 13 warnings and a dll is created which
apparently MUST be named scalaz3.dll.
sbt compile, adding scalaz3.dll to lib-bin, jar the whole thing
together to scalaz3.jar
'scala -classpath scalaz3.jar test.scala' with scalaz3.jar and z3.dll
in the current folder works