Development with iRuby using Jupyter
The Jupyter Notebook (previously iPython) is a common go-to in the Python world, but did you know you could use Jupyter to test code in other languages as well? Today I want to bring up development with iRuby, a Ruby kernel for the Jupyter system.
The most important pre-requisite for Jupyter, is having a Python 3.6 environment, and if you don’t have one iRuby suggests using Anaconda. Personally I’m not a big fan of Anaconda, as it is quite bloated, and will add way more than you really need. If you are even somewhat familiar with the Python tools, or just adventurous, I would suggest creating your own.
The Python Environment
I won’t going into great depth here, but I will make a point to explain what is needed.
I like to keep my environments separate, so I prefer using Python’s virtualenv, and virtualenvwrapper to give iRuby its own virtual environment, making sure nothing outside can cause issues. If you want the same, check out how to use virtualenvwrapper to setup a Python 3.6 environment.
Once you have access to Python 3.6 in an environment of any kind, you can start to install Jupyter with pip.
$ pip install jupyter pyzmq
If you want, you can fire it up and see what sort of environment to expect, but with Python:
$ jupyter notebook
Now for the coup de grace. Before we even touch the Ruby dependencies, we need to add a few system packages. While this will be based around OSX, you can install the same packages if not similar deps for your OS. You can at least get an idea of what is required in the github readme.
$ brew install libtool autoconf autmake zeromq $ brew install czmq --HEAD
Before we move to the gems you will need to keep in mind that the kernel is based on 2.4.1, so that version should be used with iRuby. If you have rvm, or rbenv setup, now’s the time to setup a dedicated development folder for iRuby, and set the version to 2.4.1. Feel free to use whichever if you don’t have one setup, but I always recommend rbenv, even though both work equally as well.
With a 2.4.1 environment available, it’s now time for the ruby gems, which differ slightly from those in the readme:
$ gem install cztop ffi-rzmq iruby
The ffi-rzmq gem is actually what iRuby devs discussed switching from to move to rbczmq, but you may find some issues installing the latter. If you can install rbczmq, it will work for you just as well, but ffi-rzmq is always there if it doesn’t.
With the gems installed, it’s time to register the iRuby kernel with jupyter:
$ iruby register --force
Lastly we need one more addition not mentioned in the docs. Within the dedicated folder for our iRuby projects, we now need to add a Gemfile. Now this addition is a little redundant, but unfortunately there are many situations where iRuby will simply error without it (500 error). We need to add the following to our new Gemfile:
source 'https://rubygems.org' gem 'rspec', '~> 3.6.0' gem 'iruby' gem 'ffi-rzmq'
That’s it! We should be good to start iRuby now, and see what we can do. Keep in mind that we need to start iRuby in the dedicated folder with the 2.4.1 environment set:
$ iruby notebook
With iRuby started you will see a new page in your default browser showing the contents of the folder you started in. Keep an eye on the terminal, and if you find the kernel crashing in a new project, check out the notes below.
So here is the fun part every iRuby user has dealt with at least once, fixing some very common issues:
First off, if you do not see the kernel in the options you may need to run the register command again. You can find the kernel options in the New project drop-down:
One very common issue is seeing an error saying “No such File or Directory /usr/local/bin/iruby”. This is common when using a virtual environment, and the fix is nothing more than creating a symlink. First we need to find where iruby is:
$ which iruby
The above command will give the location of iruby, and with that we can then link it to the location jupyter is accessing.
$ ln -s /path/to/iruby /usr/local/bin/iruby
Of course you need to change the path to iRuby, which with rbenv would be ~/.rbenv/shims/iruby.
Working with Libraries
With iRuby you should be able to require libraries without issue, but if you find yourself unable to accomplish this, you can add the libraries you want to the Gemfile. Don’t worry, you don’t need to run bundle install, as long as the libraries are already installed in your 2.4.1 environment, you just need to have them in the file so iRuby has a way to access them. You can add these as much or as little as you want, and without the need to restart to the kernel to gain access.
So while there seems to be caveats to iRuby, it’s an amazing tool to have, making it worth the extra effort. As it may be known, the purpose of this article is to reduce these efforts, so feel free to let me know of any issues I may have missed. Since there is so little information outside of the project’s readme explaining how to get iRuby working, I’m always glad to update the article to cover all I can.