Development with iRuby using Jupyter
The Jupyter Notebook is a common go-to in the Python world. Did you know you could use Jupyter to test code in other languages as well? Today we’ll cover development with iRuby, a Ruby kernel for the Jupyter iPython system.
The pre-requisite for Jupyter, is having a Python 3.6 environment. If you don’t have one, iRuby suggests using Anaconda. Personally I’m not a big fan of Anaconda, as it is quite bloated. It will add more than you really need. If you are familiar with the Python tools, I would suggest creating your own.
The Python Environment
Keeping environments separate is a good plan. Using Python’s virtualenv, and virtualenvwrapper, give iRuby its own virtual environment, so nothing outside can cause issues. Check out how to use virtualenvwrapper to setup a Python 3.6 environment.
With a Python 3.6 environment, you can install Jupyter using pip.
$ pip install jupyter pyzmq
Fire it up, to see the Python environment:
$ jupyter notebook
Installing iRuby
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 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, keep in mind that the kernel is based on 2.4.1, which will be used with iRuby. If you have rvm, or rbenv setup, setup a dedicated development folder for iRuby, setting the version to 2.4.1. I recommend rbenv, even though rvm works equally as well.
With a 2.4.1 environment available, it’s now time for some additional gems:
$ gem install cztop ffi-rzmq iruby
The ffi-rzmq gem is what iRuby discussed moving away from in favor of rbczmq. Though, you may find some issues installing the latter. Using rbczmq will work for you just as well, but ffi-rzmq is always there if it doesn’t.
So, with the gems installed, register the iRuby kernel with jupyter:
$ iruby register --force
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. While this addition is a little redundant, but unfortunately there are many situations where iRuby will simply error without it. So, 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 it can do. Don’t forget that we need to start iRuby in the dedicated folder, and 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.
Development with iRuby
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.
Summary
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.
No Comments