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

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.

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

  1. 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: 
    
  2. 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
    ......
  3. 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.

  1. Go to the Oracle JDK download page to download the 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.

3. Install Scala.

3.1 Install Scala On Ubuntu.

  1. Open a terminal and run the command $ sudo apt-get install scala to install scala on Ubuntu.
  2. 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> 
    
  3. 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
    
  4. 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
    
  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 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
  7. 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.

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

  1. Download the Scala macOS installer from the Scala download page.
  2. Please scroll down on the Scala download page until you see the download link, this is similar to windows.
  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 go to your home directory.
    $ cd ~
    $ pwd
    /Users/jerry
  6. Edit .bash_profile file use vim.
    $ vim .bash_profile
  7. Press the key i to 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. Press key Esc then press key :wq! in sequence 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 the 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 thoughts on “How To Install Java Jdk And Scala On Ubuntu Linux, Windows, MacOS”

  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)

Leave a Comment

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.