DEV Community

kojix2
kojix2

Posted on • Updated on

Ruby kernel in Google Colaboratory.

Hello. I am a Ruby fan.
I tried running Ruby in Google Colaboratory.

image

Open Google Colaboratory.
Run the following commands.



!apt-get install ruby-dev
!sudo apt install libtool libffi-dev ruby ruby-dev make
!sudo apt install libzmq3-dev libczmq-dev
!gem install ffi-rzmq rake
!gem install specific_install
!gem specific_install https://github.com/SciRuby/iruby
!iruby register


Enter fullscreen mode Exit fullscreen mode

Install your favoirite gems for data science(Optional).



!gem install awesome_print
!gem specific_install https://github.com/SciRuby/daru
!gem specific_install https://github.com/SciRuby/daru-view
!gem install numo-narray
!gem install rumale


Enter fullscreen mode Exit fullscreen mode

Then, Upload a Ruby ipynb into Colab using File -> Upload Notebook.



{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "ruby.ipynb",
      "version": "0.3.2",
      "provenance": [],
      "collapsed_sections": []
    },
    "kernelspec": {
      "name": "ruby",
      "display_name": "ruby 2.5.1"
    }
  },
  "cells": [
    {
      "metadata": {
        "id": "BpxZ_k6Qv84W",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "puts \"hello world\""
      ],
      "execution_count": 0,
      "outputs": []
    }
  ]
}


Enter fullscreen mode Exit fullscreen mode

Now you can run your ruby scripts!

image

have a fun day!

Check if the ruby kernel is registered in jupyter (Python notebook)



jupyter kernelspec list


Enter fullscreen mode Exit fullscreen mode

Top comments (13)

Collapse
 
phillipmaire profile image
PhillipMaire

Hi! thank for this, is there anyway to set up google CoLab to boot up with ruby, for example have it preinstalled and that way we could just open up a ruby notebook

the google CoLab website links to this git

But I am sure how to install it. I've already tried and failed :(
either way thanks for this!!

Collapse
 
kojix2 profile image
kojix2

Hello. PhillipMaire
You are right. Now the method of installing IRuby needs to be changed.

  1. Upload IRuby notebook.
  2. Start IRuby notebook whith python (Ruby doesn't work, only python works.). Run the following commands.
!apt-get install ruby-dev
!sudo apt install libtool libffi-dev ruby ruby-dev make
!sudo apt install libzmq3-dev libczmq-dev
!gem install ffi-rzmq
!gem install specific_install
!gem specific_install https://github.com/SciRuby/iruby
!iruby register
Enter fullscreen mode Exit fullscreen mode
  1. Then, while keeping the notebook open, change the runtime to Ruby. I think this method will work.

If you can't start Ruby, you can use the following command to check if the kernel is installed.

!jupyter kernelspec list
Enter fullscreen mode Exit fullscreen mode

Regards.

Collapse
 
phillipmaire profile image
PhillipMaire • Edited

Hey so this worked great! I got the kernelspec list to list out ruby. I dont know how to initiate the ruby session though. you said to change the runtime to ruby but when I go to runtime dropdown box runtime seems to just be related to the processing unit type (GPU CPU TPU). How would I switch to ruby? also I am using a mac and I noticed when I run "!jupyter kernelspec list" I get

Available kernels:
ruby /root/.local/share/jupyter/kernels/ruby
ir /usr/local/share/jupyter/kernels/ir
python2 /usr/local/share/jupyter/kernels/python2
python3 /usr/local/share/jupyter/kernels/python3
swift /usr/local/share/jupyter/kernels/swift

as pictured. The ruby directory has a period before local, which I know for mac is indicating a hidden folder. I am wondering if there is an issue here?

thank you for your work on this!

dev-to-uploads.s3.amazonaws.com/i/...

Thread Thread
 
kojix2 profile image
kojix2 • Edited

It looks like you installed the ruby kernel correctly. In addition to the hardware accelerator, I think you'll see a comobobox to select a language.
(I tried to change the language of GoogleColab's settings to English to take the screenshot, but was unable to do so for some reason. Instead I found cat mode and corgi mode.)

Ruby's kernel isn't officially supported by Google, so the installation location is different, and that's okay I guess!

img

Click the save button to start a Ruby session.

Thread Thread
 
phillipmaire profile image
PhillipMaire

Oh I see. I dont have that option “Runtime type”. I looked into it and it seems I need a notebook which has this already, I think??? This link to stack overflow gives a link to a java notebook which activate that dropdown box for me. But it only has jar and python (even after I install ruby in that notebook).

stackoverflow.com/questions/623036...

here is an example of it working with that java notebook from the link
dev-to-uploads.s3.amazonaws.com/i/...

here is what mine looks like when I open a new notebook and install ruby
dev-to-uploads.s3.amazonaws.com/i/...

I think this has something to do with your tutorial where you upload the ruby notebook??? I saved it and uploaded it but I must not be doing it right because it just looks like this and doesnt affect anything

dev-to-uploads.s3.amazonaws.com/i/...
dev-to-uploads.s3.amazonaws.com/i/...
dev-to-uploads.s3.amazonaws.com/i/...

Thread Thread
 
kojix2 profile image
kojix2 • Edited

I see. That's odd....
You will need to upload a new notebook.
My article is out of date, so please use the latest version of the Notebook made by Goto, who wrote an article on how to use Ruby on Google colab yesterday. It might work fine.

gist.githubusercontent.com/ngoto/6...
(Don't worry about the Japanese comments in the notes.)

You may also find the article made by Goto helpful. (This is a Japanese article from a Dev.to-like site called Qiita, which you can read using Google Translate.)
qiita.com/ngotogenome/items/d95a7a...

Thread Thread
 
phillipmaire profile image
PhillipMaire

yesssssssssss!!!!! that worked! thank you so much for your guidance!!! I made a google colab file with english comments and sections for other people to use if they want to. credited the original author and also you since you helped me make it work! thanks again!!!!

colab.research.google.com/drive/1n...

Collapse
 
diprimasanto profile image
Santo Diprima • Edited

Hi, unfortunately I have a problem.
I have done all the steps. but when I run, the command puts "Hello World!" tells me:

File "", line 1
puts "hello world"
^
SyntaxError: invalid syntax

and then
Unrecognized runtime "ruby"; defaulting to "python3"

Anyone know why?

Collapse
 
davidprea profile image
David Rea

I just tried this and on the last step got this error:

OSError: [Errno 2] No such file or directory: 'ruby'

Did Google shut this down?

Collapse
 
kojix2 profile image
kojix2 • Edited

Hi David Rea

This trick still works. I updated the text.
If this does not solve the problem,

  1. please check your runtime type. If Ruby is installed on None, it will not work on the GPU runtime. I often make this mistake.
  2. please check jupyter kernelspec list. If the Ruby kernel is registered, just restart the runtime.

Thank you for your report.

Collapse
 
davidprea profile image
David Rea

Got it working, thanks!

Some comments may only be visible to logged-in visitors. Sign in to view all comments.