Scala2.11.8 Installation tutorial

“This is the first day of my participation in the First Challenge 2022. For details: First Challenge 2022.”

About the author

  • The authors introduce

🍓 blog home page: author’s home page 🍓 Introduction: JAVA quality creator 🥇, a junior student 🎓, participated in various provincial and national competitions during school, and won a series of honors


Ubuntu16.04/Scala2.11.8 installation tutorial

(1) Installation of the Linux system

Scala runs on a Java Virtual Machine (JVM), so Scala programs can run on all operating systems, including Windows, Linux, Unix, Mac OS, and so on, as long as the appropriate Java VIRTUAL machine is installed. Follow-up Spark operations in this tutorial are performed on Linux

(2) Install JDK in Linux system

Install JDK1.8 and configure environment variables for Linux

(3) Install Scala in Linux system

Step 1: Visit the Scala website and download Scala. For Linux, download the.tgz installation package, which I downloaded to Scala-2.11.8.tgz. After the download is complete, switch to the ~/ download/directory, where ~ indicates the working directory of the current user. If you are logging in to the Linux operating system using the user name hadoop, the working directory of the current user is /home/hadoop/. The first step is to specify the Scala installation directory. In this case, the directory is /usr/local/, assuming that the current user login name is Hadoop.

Step 2: Unzip the scala-2.11.8. TGZ file to the /usr/local/ directory, change the folder name, and grant permissions to the Hadoop user as follows:

Sudo tar -zxf ~/ download /scala-2.11.8. TGZ -c /usr/local/sudo mv./scala-2.11.8 Scala sudo chown -r hadoop. /scala # Change the file permissions so that hadoop users can use the Scala directoryCopy the code

Step 3: You need to add the Scala command to the PATH environment variable. Here we set it in ~/.bashrc. Use vim editor to open. Bashrc file for operation:

vim ~/.bashrc
Copy the code

After opening the Vim editor, type a letter E on the keyboard to enter the editing state, and then modify the content. Then, at the beginning of the.bashrc file, change the path environment variable setting to add the scala command directory /usr/local/scala/bin to path as follows:

export PATH=$PATH:/usr/local/scala/bin
Copy the code

Note that there must be no space between PATH and equals, otherwise an error will occur. After modification, save and exit (the method is: first, press Esc key to exit the editing state of VIm, then hit the keyboard and enter “:wq”, and press Enter to save and exit).

Step 4: Validate the configured environment variables

Source ~/.bashrc # enable variable SettingsCopy the code

Once the setup is complete, verify that the setup is correct by typing the Scala command:

scala
Copy the code

After entering the Scala command, scala and Java version information is displayed on the screen, and the scala> prompt state is entered to start using the Scala interpreter. You can then enter Scala statements to debug the program code.

A successful run is indicated by the appearance as shown.

Use the Scala interpreter

Since the path variable is automatically set during installation, there is no need to give the full path of the Scala command again. In fact, the Scala command is in the bin directory of the Scala installation directory. Once you run the Scala interpreter, you can test it. If you type a statement, the interpreter immediately executes the statement and returns the result. This is what we call a REPL (read-eval-print Loop), which provides an interactive execution environment where expressions are evaluated and output the result. Instead of waiting for the entire program to run, you can view the interim results and make changes to the program in real time, which can greatly improve development efficiency. After you enter the scala command on the command prompt interface, the scala command prompt state (scala>) is displayed. You can enter the following command:

scala> 7*8+5
res0: Int = 61
Copy the code

To exit, use the command “:quit” to exit the Scala interpreter, as shown below:

scala>:quit
Copy the code

Introduction to Scala HelloWorld

The first program always starts with HelloWorld, so let’s write a HelloWorld in Scala to get you started.

(1) Learn how to use Scala interpreter through HelloWorld program

After you enter the scala command in the Shell command prompt interface, the Scala command prompt state is displayed

scala>
Copy the code

(2) Run the script file in the Scala interpreter

Import the script with the “:load” command and run multiple lines at a time:

Use a text editor (such as Vim) to create a code file called test.Scala

/ / code file to/usr/local/scala/mycode/Test. The scala println (" This is the first line ") println (" This is the second line ") println("This is the third line")Copy the code

Run the code file in the Scala REPL with the following command:

(3) Run Scala programs by compiling and packaging

/ / code file to/usr/local/scala/mycode/HelloWorld. Scala object HelloWorld {def main (args: Array[String]) { println("Hello, world!" ); }}Copy the code

Compile using the scalac command (resulting in Java bytecode)

cd /usr/local/scala/mycode
scalac  HelloWorld.scala
Copy the code

Run bytecode files using Scala or Java commands

scala -classpath . HelloWorld
java  -classpath  .:/usr/local/scala/lib/scala-library.jar  HelloWorld
Copy the code

\

After the language

The original intention of the director to write blog is very simple, I hope everyone in the process of learning less detours, learn more things, to their own help to leave your praise 👍 or pay attention to ➕ are the biggest support for me, your attention and praise to the director every day more power.

If you don’t understand one part of the article, you can reply to me in the comment section. Let’s discuss, learn and progress together!

Wechat (Z613500) or QQ (1016942589) for detailed communication.