Scala Installation
The Scala language can run on systems such as Windows, Linux, Unix, and Mac OS X.
Scala is built on top of Java, extensively utilizing Java's class libraries and variables. Before using Scala, Java (version >1.5) must be installed first.
Installing Scala on Mac OS X and Linux
Step 1: Java Setup
Ensure that JDK version 1.5 or higher is installed on your local machine, and that the JAVA_HOME environment variable and JDK's bin directory are set.
We can check if Java is installed using the following command:
$ java -version
java version "1.8.0_31"
Java(TM) SE Runtime Environment (build 1.8.0_31-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.31-b07, mixed mode)
$
Next, we can check if the Java compiler is installed by entering the following command:
$ javac -version
javac 1.8.0_31
$
If not yet installed, you can refer to our Java Development Environment Setup.
For installing Scala, it is recommended to use the Scala installer supported by Coursier with the command cs setup
.
On macOS, use the brew
command to install:
brew install coursier/formulas/coursier && cs setup
On Linux, use the curl
command to download and install the binary package:
curl -fL https://github.com/coursier/launchers/raw/master/cs-x86_64-pc-linux.gz | gzip -d > cs && chmod +x cs && ./cs setup
Binary Package Installation
We can download the Scala binary package from the GitHub address https://github.com/lampepfl/dotty/tags. In this tutorial, we will download version 2.11.7, as shown in the following figure:
Click on the version number we want to download, then scroll to the bottom of the page to see the download file package:
Decompress the file package and move it to /usr/local/share:
mv scala-2.11.7 scala # Rename the Scala directory
mv /download/scalapath /usr/local/share # The download directory should be based on your actual download path
Modify the environment variables. If you are not an administrator, use sudo to enter administrator privileges and edit the configuration file profile:
vim /etc/profile
or
sudo vim /etc/profile
Add the following at the end of the file:
export PATH="$PATH:/usr/local/share/scala/bin"
Save and exit with :wq!, restart the terminal, and execute the scala command. The following output indicates a successful installation:
$ scala
Welcome to Scala version 2.11.7 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_31).
Type in expressions to have them evaluated.
Type :help for more information.
>
Note: When compiling, if there is Chinese text, garbled characters may appear. The solution can be found in: Solving Scala Chinese Garbled Characters
Installing Scala on Windows
Step 1: Java Setup
The method of detection has been described earlier and will not be repeated here.
If not yet installed, you can refer to our Java Development Environment Setup.
Next, we can download the Scala binary package from the official Scala website address http://www.scala-lang.org/downloads (at the bottom of the page). In this tutorial, we will download version 2.11.7, as shown in the following figure:
After downloading, double-click the exe file and install step by step. During the installation process, you can use the default installation directory.
After installing Scala, the system will automatically prompt you to click finish to complete the installation.
Right-click on My Computer, and click "Properties" to enter the page as shown in the figure. Below, start configuring the environment variables, right-click on [My Computer] - [Properties] - [Advanced System Settings] - [Environment Variables], as shown in the figure:
Set the SCALA_HOME variable: Click New, enter in the variable name column: **SCALA_HOME** : In the variable value column, enter: D:\Program Files (x86)\scala which is the Scala installation directory. It varies according to individual circumstances; if installed on the C drive, change D to C.
Set the Path