Scala is based on java virtual machine, 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 same as below.
- Open a terminal ( Linux, MacOS ) or dos window ( Windows ) and run
java -version
command 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 below messages, you need to install jdk.
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)
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 below steps.
2.1 Install Jdk On Ubuntu.
- First run
sudo apt-get update
command with root permissoin in a terminal to update package index.$ sudo apt-get update [sudo] password for zhaosong:
- Then run
sudo apt-get install default-jdk
command to install the default openjdk.$ sudo apt-get install default-jdk Reading package lists... Done ......
- After install, run
java -version
to get the installed jdk version.
2.2 Install Jdk On Windows.
I had write an article about install jdk on Windows before, you can read it. The article is Beginner’s guide for install jdk(java delvelopment kit) and eclipse in windows
2.3 Install Jdk On MacOS.
- Go to oracle jdk download page to download 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
$ sudo apt-get install scala
command to install scala on ubuntu. - When above installaion process complete success, run command
scala
in terminal, if you see 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 below command, you will get result follow it. This is similar with 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 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 dot after the variable then press tab key to list all it’s method.
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 click enter.scala> :q
3.2 Install Scala On Windows.
- There is a windows installer that can help you install scala successully. In Windows OS web browser, go to 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 scala windows installer.
- After download it, just double click it to install. Click Next button during the installation process until the last step.
- When you complete install it, open a dos window, and input scala, then you will go into 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 Scala download page.
- Please scroll down on the Scala download page until you see the download link like below, this is similar with 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 goto your home directory.$ cd ~ $ pwd /Users/jerry
- Edit .bash_profile file use vim.
$ vim .bash_profile
- Click i 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
- Click esc —> :wq! 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 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