How To Install Java Jdk And Scala On Ubuntu Linux, Windows, MacOS

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.

  1. 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.

  1. 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: 
    
  2. Then run sudo apt-get install default-jdk command to install the default openjdk.
    $ sudo apt-get install default-jdk
    Reading package lists... Done
    ......
  3. 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.

  1. Go to oracle jdk download page to download macos version jdk installer.
  2. Click the downloaded dmg file to install jdk.
  3. Follow the instructions in the installer dialog to finish the jdk installation.
READ :   Java Create Directory Examples

3. Install Scala.

3.1 Install Scala On Ubuntu.

  1. Open a terminal and run $ sudo apt-get install scala command to install scala on ubuntu.
  2. 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> 
    
  3. 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
    
  4. 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
    
  5. 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
  6. 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
  7. If you want to quit scala interactive console, just input :quit or :q, then click enter.
    scala> :q

3.2 Install Scala On Windows.

  1. 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.
    other ways to install scala on windows
  2. After download it, just double click it to install. Click Next button during the installation process until the last step.
  3. 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.

  1. Download the Scala MacOS installer from Scala download page.
  2. Please scroll down on the Scala download page until you see the download link like below, this is similar with windows.
    download scala for macos version
  3. 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.
  4. Now you should add bin folder in above directory ( /Users/jerry/Documents/Tool/scala-2.12.8/bin ) to the PATH environment variable value.
  5. Run cd ~ in a terminal to goto your home directory.
    $ cd ~
    $ pwd
    /Users/jerry
  6. Edit .bash_profile file use vim.
    $ vim .bash_profile
  7. Click i go to insert mode.
  8. 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
  9. Click esc —> :wq! to save the changes.
  10. 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
  11. Run $ source .bash_profile to make above changes take effect.
  12. 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> 
    

2 Comments


  1. 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)

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.