[Ruby] Use Ruby with Google Colab

3 minute read

I tried running Ruby on Google Colab was not working as it is as of July 2020, so I tried trial and error using Julia’s example. I will introduce it because it went well.

Easy execution

1. File download

Right-click This file and save it on your computer. Download to

2. Notebook upload

Open https://colab.research.google.com/ and “Upload” the file downloaded from the above (if you opened Google Collaboratory for the first time, “File” → “Upload notebook”).

3. Install Ruby and required software

A notebook with two pre-populated cells opens. Click the cell at the top and press Shift+Enter (or the button with the right triangle) to execute it. It will take some time. At the bottom `Runtime “ruby” is not recognized. I get the message “Connect to the default “python3”” but ignore it.

4. Install additional gems

After moving to the second cell and making edits that leave only the gem line you want to install, press Shift+Enter (or the button with the right triangle) to run it.

5. Switch runtime to ruby

Press Runtime → Change Runtime Type. This will bring up the “Notebook Settings” window with “ruby 2.5.1” selected as the “Runtime Type”. Make sure “ruby 2.5.1” is selected as the “Runtime Type” and press “Save”. (If it is not selected, select “ruby 2.5.1” from the list and save.)

6. Run the Ruby code

Now you have an environment in which to run your Ruby code. For example, if you type RUBY_VERSION and press Shift+Enter, you should see "2.5.1".


Manual operation

This is a method to do the above manually. Describe in case the environment on Google side changes.

1. Create a new notebook with Google Colaboratory

Open https://colab.research.google.com/ and press “Create new notebook” (“File” -> “Create new notebook” when Google Collaboratory is opened for the first time) to open a new notebook Create

2. Download as is in ipynb format

Download a blank notebook with no input from “File” -> “Download ipynb”.

3. Edit the downloaded file on your computer

This section is done with a text editor on your computer. Look for the following in the file, starting with "kernelspec":

    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }

Rewrite the above “python3" and "Python3" for Ruby as follows.

    "kernelspec": {
      "name": "ruby",
      "display_name": "ruby 2.5.1"
    }

After rewriting, save.

4. Upload the rewritten notebook

Upload the rewritten file from “File” -> “Upload Notebook” in Google Colaboratory of the web browser.

5. Install Ruby and required software

Copy and paste the following as it is into the cell (where you enter) of Google Collaboratory.

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

Then press Shift+Enter to execute. It will take some time. At the bottom `Runtime “ruby” is not recognized. I get the message “Connect to the default “python3”” but ignore it.

6. Install additional gems

Install additional gems as needed. Copy and paste the following, leaving only the gem line you want to install. Then press Shift+Enter to execute.

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

7. Switch runtime to ruby

Perform the same operation as “5. Switching the runtime to ruby” in the easy execution section.

8. Run the Ruby code

Now you have an environment in which to run your Ruby code.

References

  1. I tried running Ruby on Google Colab –This article couldn’t be realized without this article. The apt installation package and iruby installation method have been partially modified based on this article.

  2. Using Julia with Google Colab: Using an existing notebook –Rewriting jyn of ipynb and installing language and switching runtimes I referred to.