Scala is based on java virtual machines, so if you want to learn scala programming, you need first install JDK, then install scala. This article will tell you how to install JDK and scala on both Ubuntu Linux, Windows, and macOS.
1. Verify JDK Has Been Installed Or Not.
The steps to verify whether JDK has been installed on your os (Linux, Windows, macOS) or not is the same as below.
- Open a terminal ( Linux, macOS ) or dos window ( Windows ) and run the command
java -version
to check whether JDK has been installed or not. If you see messages like below, it means JDK has been installed already. If you do not see the below messages, you need to install JDK.
The below message is for Linux, macOS$ java -version openjdk version "1.8.0_191" OpenJDK Runtime Environment (build 1.8.0_191-8u191-b12-2ubuntu0.18.04.1-b12) OpenJDK 64-Bit Server VM (build 25.191-b12, mixed mode)
The below message is for Windows.
C:\Users\jerry>java -version java version "1.8.0_211" Java(TM) SE Runtime Environment (build 1.8.0_211-b12) Java HotSpot(TM) 64-Bit Server VM (build 25.211-b12, mixed mode)
2. Install JDK.
If JDK is not installed, you need to install it follow the below steps.
2.1 Install JDK On Ubuntu.
- First, run the command
sudo apt-get update
with root permission in a terminal to update the package index.$ sudo apt-get update [sudo] password for zhaosong:
- Then run the command
sudo apt-get install default-jdk
to install the default OpenJDK.$ sudo apt-get install default-jdk Reading package lists... Done ......
- After installation, run
java -version
to get the installed JDK version.
2.2 Install JDK On Windows.
I had written an article about install JDK on Windows before, you can read it. The article is Beginner’s guide for install JDK(java development kit) and eclipse in windows
2.3 Install JDK On macOS.
- Go to the Oracle JDK download page to download the macOS version JDK installer.
- Click the downloaded dmg file to install JDK.
- Follow the instructions in the installer dialog to finish the JDK installation.
3. Install Scala.
3.1 Install Scala On Ubuntu.
- Open a terminal and run the command
$ sudo apt-get install scala
to install scala on Ubuntu. - When the above installation process complete success, run the command
scala
in a terminal, if you see a message like below, it means scala has been installed successfully.$ scala Welcome to Scala 2.11.12 (OpenJDK 64-Bit Server VM, Java 11.0.2). Type in expressions for evaluation. Or try :help. scala>
- Then it will go to scala’s interactive console, input the below command, and press Enter key, you will get the result below the source code immediately. This is similar to python REPL.
scala> println("hello world"); hello world scala> 1+1 res1: Int = 2 scala> res1 * 9 res2: Int = 18
- In scala, you can use the java library easily. For example, you can import java.util.Date as below.
scala> import java.util.Date; import java.util.Date
- You can declare a variable use val and create an instance of java.util.Date class also.
scala> val date = new Date(); date: java.util.Date = Mon May 06 14:12:46 CST 2019
- If you want to list the date variable’s method, just enter a dot after the variable name then press the tab key to list all it’s methods.
scala> date. != -> asInstanceOf compareTo equals getDate getMinutes getTime hashCode notify setHours setSeconds synchronized toLocaleString → ## == before ensuring formatted getDay getMonth getTimezoneOffset isInstanceOf notifyAll setMinutes setTime toGMTString toString + after clone eq getClass getHours getSeconds getYear ne setDate setMonth setYear toInstant wait
- If you want to quit scala interactive console, just input
:quit
or:q
, then press enter key.scala> :q
3.2 Install Scala On Windows.
- There is a windows installer that can help you install scala successfully. In the Windows OS web browser, go to the scala download page, then scroll down the page until you see text Other ways to install Scala. There is a link for you to download the scala windows installer.
- After download, just double-click it to install. Click the Next button during the installation process until the last step.
- When you complete install it, open a dos window, and input command scala, then you will go into the scala interactive console.
C:\Users\jerry>scala Welcome to Scala 2.12.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_131). Type in expressions for evaluation. Or try :help. scala>
3.3 Install Scala On macOS.
- Download the Scala macOS installer from the Scala download page.
- Please scroll down on the Scala download page until you see the download link, this is similar to windows.
- The downloaded file is a zip file with .tgz extension, unzip it to a local folder such as /Users/jerry/Documents/Tool/scala-2.12.8.
- Now you should add bin folder in above directory ( /Users/jerry/Documents/Tool/scala-2.12.8/bin ) to the PATH environment variable value.
- Run
cd ~
in a terminal to go to your home directory.$ cd ~ $ pwd /Users/jerry
- Edit .bash_profile file use vim.
$ vim .bash_profile
- Press the key i to go to insert mode.
- Insert Scala bin directory in the PATH environment variable value.
# Setting PATH for Python 3.7 # The original version is saved in .bash_profile.pysave PATH="/Users/jerry/Documents/Tool/scala-2.12.8/bin:/Library/Frameworks/Python.framework/Versions/3.7/bin:${PATH}" export PATH
- Press key Esc then press key :wq! in sequence to save the changes.
- Run
cat .bash_profile
to see the profile content.$ cat .bash_profile # Setting PATH for Python 3.7 # The original version is saved in .bash_profile.pysave PATH="/Users/jerry/Documents/Tool/scala-2.12.8/bin:/Library/Frameworks/Python.framework/Versions/3.7/bin:${PATH}" export PATH
- Run
$ source .bash_profile
to make the above changes take effect. - Now input scala in the terminal, you will go to the Scala console.
$ scala Welcome to Scala 2.12.8 (Java HotSpot(TM) 64-Bit Server VM, Java 12.0.1). Type in expressions for evaluation. Or try :help. scala>
Jerry, can you tell me what the ‘(build 25.211…)’ part means when the Java version info is returned please?
> Java HotSpot(TM) 64-Bit Server VM (build 25.211-b12, mixed mode)
The (build 25.211-b12) part means the jdk build number i think, you can refer below article on stackoverflow. https://stackoverflow.com/questions/47156016/what-does-the-4th-number-mean-in-java-9s-version-string-scheme