Scala Development Environment
Installation
We will use SdkMan! to install Scala, so if you haven't installed SdkMan yet, install it first. (SdkMan Installation)
Install Scala 2
sdk install scala 2.13.18
Downloading: scala 2.13.18
In progress...
######################################################################### 100.0%
Installing: scala 2.13.18
Done installing!
Setting scala 2.13.18 as default.
Install Scala 3
You may get an error like the following when you run scala 3 installed by sdkman.
/Users/USERNAME/.sdkman/candidates/scala/3.3.8/bin/common: line 33: /opt/local/bin/tput: Bad CPU type in executable
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
You need to check what you have in /opt/local. /opt/local might be just a leftover from installing Port or something like that. /opt/local/bin/tput: Bad CPU type in executable means you have incompatible binaries in /opt/local/bin.
Try
echo $PATH
which -a tput
which tput
Ideally, which tput should return:
/usr/bin/tput
If it's /opt/local/bin/tput, you may need to remove /opt/local.
For the safe removal, rename the directory instead of deleting it immediately:
sudo mv /opt/local /opt/local.old
Use your Mac for a few days. If nothing breaks, remove it permanently:
sudo rm -Rf /opt/local.old
Now, running the following command
which -a tput
should return
/usr/bin/tput
sdk install scala 3.3.8
Downloading: scala 3.3.8
In progress...
######################################################################### 100.0%
Installing: scala 3.3.8
Done installing!
Do you want scala 3.3.8 to be set as default? (Y/n): n
Different Java Versions
Would you like to install different Java versions? Check out the Java page.
sbt
Installation
You can install sbt with the following command.
sdk install sbt
Downloading: sbt 1.12.15
In progress...
######################################################################### 100.0%
Installing: sbt 1.12.15
Done installing!
Or you can install a specific version of sbt with
sdk install sbt 1.11.7
Downloading: sbt 1.11.7
In progress...
######################################################################### 100.0%
Installing: sbt 1.11.7
Done installing!
Do you want sbt 1.11.7 to be set as default? (Y/n): y
Setting sbt 1.11.7 as default.
Upgrade
Upgrade sbt can be done with
sdk upgrade sbt
Available defaults:
sbt (local: 1.11.7, 1.12.15; default: 2.0.6)
Use prescribed default version(s)? (Y/n): y
Downloading: sbt 2.0.6
In progress...
######################################################################### 100.0%
Installing: sbt 2.0.6
Done installing!
Setting sbt 1.10.11 as default.
List sbt
You can get the list of all available and installed sbt with
sdk list sbt
================================================================================
Available Sbt Versions
================================================================================
* 2.0.6 1.11.4 1.7.3 1.3.9
2.0.5 1.11.3 1.7.2 1.3.8
2.0.4 1.11.2 1.7.1 1.3.7
2.0.3 1.11.1 1.7.0 1.3.6
2.0.2 1.11.0 1.6.2 1.3.5
2.0.1 1.10.11 1.6.1 1.3.4
2.0.0 1.10.10 1.6.0 1.3.3
2.0.0-RC15 1.10.9 1.5.8 1.3.1
2.0.0-RC13 1.10.7 1.5.7 1.3.0
2.0.0-RC12 1.10.6 1.5.6 1.2.8
> * 1.12.15 1.10.5 1.5.5 1.2.7
1.12.14 1.10.4 1.5.4 1.2.6
1.12.13 1.10.3 1.5.3 1.2.5
1.12.12 1.10.2 1.5.2 1.2.4
1.12.11 1.10.1 1.5.1 1.2.3
1.12.10 1.10.0 1.5.0 1.2.1
1.12.9 1.9.9 1.4.9 1.2.0
1.12.8 1.9.8 1.4.8 1.1.6
1.12.7 1.9.7 1.4.7 1.1.5
1.12.6 1.9.6 1.4.6 1.1.4
1.12.5 1.9.5 1.4.5 1.1.2
1.12.4 1.9.4 1.4.4 1.1.1
1.12.3 1.9.3 1.4.3 1.1.0
1.12.2 1.9.2 1.4.2 1.0.4
1.12.1 1.9.1 1.4.1 1.0.3
1.12.0 1.9.0 1.4.0 1.0.2
1.12.0-RC1 1.8.3 1.3.13 1.0.1
* 1.11.7 1.8.2 1.3.12 1.0.0
1.11.6 1.8.1 1.3.11 0.13.18
1.11.5 1.8.0 1.3.10 0.13.17
================================================================================
+ - local version
* - installed
> - currently in use
================================================================================
Set Default
If you have multiple sbts installed, you can set the default sbt manually by
sdk default sbt 2.0.6
setting sbt 2.0.6 as the default version for all shells.
Uninstall sbt
To uninstall a specific version of sbt, you can use the following command.
sdk uninstall sbt SBT_VERSION_HERE
e.g.) To uninstall sbt 1.10.0, run
sdk uninstall sbt 1.11.7
removed sbt 1.11.7.
scala-cli
Installation
sdk install scalacli
Downloading: scalacli 1.15.0
In progress...
######################################################################### 100.0%
Installing: scalacli 1.15.0
Done installing!
Setting scalacli 1.15.0 as default.
Install a specific version
sdk install scalacli VERSION_HERE
e.g.) To install scalacli 1.14.0, run
sdk install scalacli 1.14.0
Downloading: scalacli 1.14.0
In progress...
######################################################################### 100.0%
Installing: scalacli 1.14.0
Done installing!
Do you want scalacli 1.14.0 to be set as default? (Y/n): y
Setting scalacli 1.14.0 as default.
Upgrade
sdk upgrade scalacli
Available defaults:
scalacli (local: 1.15.0, 1.14.0; default: 1.16.0)
Use prescribed default version(s)? (Y/n): y
Downloading: scalacli 1.16.0
In progress...
######################################################################### 100.0%
Installing: scalacli 1.16.0
Done installing!
Setting scalacli 1.16.0 as default.
Set Default
sdk default scalacli 1.16.0
setting scalacli 1.16.0 as the default version for all shells.
List available scala-cli versions
sdk list scalacli
Ammonite REPL
Sadly, Ammonite REPL is no longer maintained. 😭
Rrerequisites
Let's create a bin folder in your home directory and add it to your $PATH so that Ammonite REPL can be found when you run amm.
That $HOME/bin folder can be used for any other commands you want to add to your $PATH as well.
mkdir -p $HOME/bin
{ echo $PATH | grep -q "$HOME/bin" ; } || echo 'export PATH="$HOME/bin:$PATH"' >> $HOME/.zshrc
Ammonite REPL for Scala 3.5:
mkdir -p $HOME/bin; sh -c '(echo "#!/usr/bin/env sh" && curl -L https://github.com/com-lihaoyi/Ammonite/releases/download/3.0.9/3.5-3.0.9) > $HOME/bin/amm && chmod +x $HOME/bin/amm' && amm
Ammonite REPL for Scala 2.13:
mkdir -p $HOME/bin; sh -c '(echo "#!/usr/bin/env sh" && curl -L https://github.com/com-lihaoyi/Ammonite/releases/download/3.0.9/2.13-3.0.9) > $HOME/bin/amm && chmod +x $HOME/bin/amm' && amm
Try ammonite-repl
amm
Loading...
Welcome to the Ammonite Repl 3.0.9 (Scala 3.5.2 Java 21.0.12)
@
sbt plugins (Optional)
Global sbt plugins
You can set global sbt plugins by running the following command. Please choose the ones you want to have.
Not all of them work with sbt 2 so if you're using sbt 2, you may need to remove some of them.
echo '
addSbtPlugin("io.github.todokr" % "sbt-project-switcher" % "0.1.4")
addSbtPlugin("com.timushev.sbt" % "sbt-updates" % "0.7.0")
// Outdated
// addSbtPlugin("com.github.cb372" % "sbt-explicit-dependencies" % "0.3.1")
addSbtPlugin("net.nmoncho" % "sbt-dependency-check" % "1.9.2")
addSbtPlugin("com.github.sbt" % "sbt-license-report" % "1.10.0")
addDependencyTreePlugin
' >> $HOME/.sbt/1.0/plugins/sbt-plugins.sbt
sbt-project-switcher
sbt-tpolecat
sbt-tpolecat is an SBT plugin for automagically configuring scalac options
Add the following line to project/plugins.sbt.
addSbtPlugin("org.typelevel" % "sbt-tpolecat" % "0.5.7")
sbt-wartremover
WartRemover is a linting tool that can help you write safe and correct code.
Add the following line to project/plugins.sbt.
addSbtPlugin("org.wartremover" % "sbt-wartremover" % "3.6.1")
In build.sbt,
wartremoverErrors ++= Warts.all
Or more practical settings might be
wartremoverErrors ++= Warts.allBut(Wart.Any, Wart.Nothing, Wart.ImplicitConversion, Wart.ImplicitParameter)
Also add the following setting to exclude the source code generated by build from wart checking.
wartremoverExcluded += sourceManaged.value