Skip to content

Instantly share code, notes, and snippets.

@shadaj
Last active January 18, 2024 13:29
Show Gist options
  • Save shadaj/323ad2393b46c1b71df435728a052c24 to your computer and use it in GitHub Desktop.
Save shadaj/323ad2393b46c1b71df435728a052c24 to your computer and use it in GitHub Desktop.
Install Scala in Colab.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Install Scala in Colab.ipynb",
"version": "0.3.2",
"provenance": [],
"private_outputs": true,
"collapsed_sections": [],
"toc_visible": true,
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"accelerator": "GPU"
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/shadaj/323ad2393b46c1b71df435728a052c24/install-scala-in-colab.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "zYzgRayyZ2z8",
"colab_type": "text"
},
"source": [
"# Scala Installer for Colaboratory\n",
"This notebook installs [Almond](http://almond.sh), a Scala kernel for Jupyter, into Colaboratory and configures it to make the preinstalled Python libraries accessible to Scala code (through [ScalaPy](https://github.com/shadaj/scalapy)).\n",
"\n",
"Because this installer is built on Almond, you get complete Scala support inside your notebooks, including the ability to get code completions and access to the [Jupyter-specific APIs](https://almond.sh/docs/api-jupyter). In addition, this installer configures Almond to be able to load preinstalled modules in Colab such as `numpy` and `tensorflow` through ScalaPy.\n",
"\n",
"If coming to this notebook from GitHub, make sure to **not reset your runtime** when running this notebook. In order to install the Scala kernel, the installer writes data to the runtime filesystem, so resetting would clear out the kernel.\n",
"\n",
"## Post-installation Setup\n",
"After installing the Scala kernel, you will need to **completely restart the Jupyter instance** for the Scala notebook you want to run. If you are not yet connected to a Jupyter server (check the top right of your screen), you are fine and can immediately start running code.\n",
"\n",
"However, if you previously started a Jupyter server, you must now go to `Runtime > Manage Sessions` and terminate the session for the Scala notebook. Then after reloading the page, you will see a button to connect to a server, which will start up a new Jupyter server that will now have the Scala kernel loaded.\n",
"\n",
"**Note:** if your session expires and the machine is shut down, you will need to re-run this installer because Colab resets the filesystem"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "QVJoUDPtb9gX",
"colab_type": "text"
},
"source": [
"Install the Almond kernel into the global Jupyter kernels."
]
},
{
"cell_type": "code",
"metadata": {
"id": "949yQj6ltEoW",
"colab_type": "code",
"colab": {}
},
"source": [
"%%shell\n",
"SCALA_VERSION=2.12.8 ALMOND_VERSION=0.3.0+16-548dc10f-SNAPSHOT\n",
"curl -Lo coursier https://git.io/coursier-cli\n",
"chmod +x coursier\n",
"./coursier bootstrap \\\n",
" -r jitpack -r sonatype:snapshots \\\n",
" -i user -I user:sh.almond:scala-kernel-api_$SCALA_VERSION:$ALMOND_VERSION \\\n",
" sh.almond:scala-kernel_$SCALA_VERSION:$ALMOND_VERSION \\\n",
" --sources --default=true \\\n",
" -o almond-snapshot --embed-files=false\n",
"rm coursier\n",
"./almond-snapshot --install --global --force\n",
"rm almond-snapshot"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "FXlAFUtPcEPo",
"colab_type": "text"
},
"source": [
"Finally, we modify the kernel definition to preload the Python 3 native library to enable usage of modules such as `numpy` and `tensorflow` in ScalaPy."
]
},
{
"cell_type": "code",
"metadata": {
"id": "1kVqkjNBU6K6",
"colab_type": "code",
"colab": {}
},
"source": [
"%%shell\n",
"echo \"{\n",
" \\\"language\\\" : \\\"scala\\\",\n",
" \\\"display_name\\\" : \\\"Scala\\\",\n",
" \\\"argv\\\" : [\n",
" \\\"bash\\\",\n",
" \\\"-c\\\",\n",
" \\\"env LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libpython3.6m.so:\\$LD_PRELOAD java -jar /usr/local/share/jupyter/kernels/scala/launcher.jar --connection-file {connection_file}\\\"\n",
" ]\n",
"}\" > /usr/local/share/jupyter/kernels/scala/kernel.json"
],
"execution_count": 0,
"outputs": []
}
]
}
@BilalKacel
Copy link

Hello Shadaj,
I hope you're well.
I would like to know if the installation procedure of the Scala kernel is still functional?
From Colab, there is no error when I run the Scala Kernel installation.
But, unfortunately, just running the "Hello World !" print does not work ...
Do you have any advice ?

Thank you.
Cheers,

@cedricmjohn
Copy link

Same as BilalKacel: the install seems to work, but the kernel does not load Scala. Tried to reload the page and restart the kernel but did not work for me.

@tensorvijay
Copy link

I would strongly encourage this to be resolved

@MielHostens
Copy link

This was possible earlier, but indeed fails now

@shadaj
Copy link
Author

shadaj commented Nov 28, 2020

Hmm, this is surprising. I just used the same initialization code for a couple other notebooks in the past week and it seemed to work fine. Perhaps the notebook needs to be set to the Scala kernel like in https://gist.github.com/shadaj/29d77180aeefc41a749273026f7d1fd9 to work?

@MielHostens
Copy link

MielHostens commented Nov 28, 2020 via email

@Platinum-Dragon
Copy link

This is so disappointing. I can whip together and train a deep learning model in Python / keras in a matter of minutes, but I can't find anything comparable for Scala :-(

@kirisakow
Copy link

kirisakow commented Feb 27, 2023

There is actually an important prerequisite to assure before you follow steps given by @shadaj: If you open a notebook with a text editor and compare the raw contents of a regular Notebook with that of a Shadaj's notebook, you'll see one crucial difference that you need to fix. So,

  1. You need to open your Colab Notebook with a text editor (use any of the relevant Google Drive apps) and make sure the kernelspec.name key is set to work with Scala, like so:
{
  
  "kernelspec": {
    "display_name": "Scala",
    "name": "scala"
  }
  
}

(This is a necessary step for any non-pythonic kernel — be it Scala, Go or whatnot. Check my notebooks for examples: https://gist.github.com/kirisakow)

  1. Save, close.

  2. Now open normally with Google Colab engine and start working.

@Platinum-Dragon
Copy link

Platinum-Dragon commented Mar 1, 2023 via email

@kirisakow
Copy link

kirisakow commented Mar 1, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment