Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Scala Installation and Environment Setup

Scala Installation

The Scala language can run on Windows, Linux, Unix, Mac OS X, and other systems.

Scala is built on top of Java and makes extensive use of Java's libraries and variables, so Java (>1.5) must be installed before using Scala.


Installing Scala on Mac OS X and Linux

Step 1: Java Setup

Make sure you have JDK 1.5 or above installed locally, and that the JAVA_HOME environment variable and the JDK's bin directory are set.

We can use the following command to check whether Java is installed:



$ 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 whether the Java compiler is installed. Enter the following command to check:



$ javac -version

javac 1.8.0_31

$

If it is not installed, you can refer to ourJava Development Environment Configuration。

To install Scala, it is recommended to use the Scala installer cs setup, powered by Coursier

On macOS, install using the brew command:

brew install coursier/formulas/coursier && cs setup

On Linux, use the curl command to download the source package for installation.

x86-64 architecture:

curl -fL https://github.com/coursier/launchers/raw/master/cs-x86_64-pc-linux.gz | gzip -d > cs && chmod +x cs && ./cs setup

ARM64 architecture:



curl -fL https://github.com/VirtusLab/coursier-m1/releases/latest/download/cs-aarch64-pc-linux.gz | gzip -d > cs && chmod +x cs && ./cs setup

can be used via uname -m command to check your server's CPU architecture:

# uname -m

x86_64

Installing from a Binary Package

We can download from the Github address https://github.com/lampepfl/dotty/tags the Scala binary package; in this tutorial we will download 2.11.7version, as shown below:

Click the version number you want to download, then scroll to the bottom of the page to see the download file package:

Unzip the package and move it to /usr/local/share:



mv scala-2.11.7 scala                   # 重命名 Scala 目录

mv /download/scalapath /usr/local/share # 下载目录需要按你实际的下载路径

Modify the environment variables. If you are not an administrator, you can use sudo to gain administrator privileges and modify the profile configuration file:



vim /etc/profile



或



sudo vim /etc/profile

Add at the end of the file:



export PATH="$PATH:/usr/local/share/scala/bin"

:wq! save and exit, restart the terminal, run the scala command, and if the following information is output, the installation was successful:



$ 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, garbled characters may appear if Chinese characters are present; see the solution:Scala Chinese Character Encoding Solution


Installing Scala on Windows

Step 1: Java Setup

The detection method was described earlier and will not be repeated here.

If it is not installed, you can refer to ourJava Development Environment Configuration。

Next, we can download from the official Scala website address http://www.scala-lang.org/downloads the Scala binary package (at the bottom of the page); in this tutorial we will download 2.11.7version, as shown below:

After downloading, double-click the exe file and install step by step; during installation you can use the default installation directory.

After Scala is installed, the system will prompt you; click finish to complete the installation.

Right-click My Computer and click Properties to enter the page shown below. Now start configuring environment variables: right-click My Computer -- Properties -- Advanced System Settings -- Environment Variables, as shown:

Set the SCALA_HOME variable: click New and enter in the Variable name field:SCALA_HOME: enter in the Variable value field: **D:\Program Files (x86)\scala **, which is the Scala installation directory; it varies from person to person. If installed on the C drive, just change D to C.

Set the Path variable: find Path under system variables as shown and click Edit. Add the following path at the very beginning of the Variable value field: %SCALA_HOME%\bin;%SCALA_HOME%\jre\bin;

**Note:** the semicolon after it ; should not be omitted.

Set the Classpath variable: find Classpath under system variables as shown and click Edit; if it does not exist, click New:

Note:Do not omit the leading .; in the Variable value. Finally, click OK.

Check whether the environment variables are set correctly: bring up cmd to check. Click Start, type cmd in the input box, then press Enter, type scala, and press Enter; if the environment variables are set correctly, you should see this information.

The following lists the directories for different systems (for reference):

System Environment Variables Value (Example)
Unix $SCALA_HOME /usr/local/share/scala
  $PATH $PATH:$SCALA_HOME/bin
Windows %SCALA_HOME% c:\Progra~1\Scala
  %PATH% %PATH%;%SCALA_HOME%\bin