Skip to content
Jason Charney edited this page Apr 18, 2016 · 21 revisions

Ruby is a is a dynamic, reflective, object-oriented, general-purpose programming language. It was designed and developed by Yukihiro "Matz" Matsumoto.

You may have noticed that like with Python that Ruby was installed on your Pi if you have a Pi 2. Well that version is super old. Practically everything that it relies uses version 1.9.1, despite there being a newer version (2.3.0 as of 12/25/2015). There's also an easy way to install the new version and for both the old version and the new version to coexist.

Prerequirements

⚠️ WARNING! If you are running tmux inside the terminal you are using, exit that first! Some of the rbenv installation stuff might not install right within it running.

Use apt-get to make sure the following components are installed.

$ sudo apt-get install autoconf bison build-essential libssl-dev libyaml-dev libreadline-dev libreadline6-dev zlib1g-dev libncurses5-dev libffi-dev libgdbm3 libgdbm-dev

Install ruby

In previous incarnations of my projects, I told you to go use RVM. Considering all the places RVM decided to modify the $PATH variable, it was time for it to hit the road. (Check the other dot files in the home directory if you uninstalled RVM and make sure that RVM is not part of the $PATH in those files!) It turns out a lot of people don't like RVM any more and have moved on to rbenv and ruby-build. However rbenv is just as bad. The whole "shims" system is a sham. You try to install other software like [weechat] plugins and it will try to install Debian/Ubuntu/Linux Mint/Raspbian's really old version of Ruby (1.9.3) Never mind that as of this last update Ruby's latest stable is 2.3.0.

This leaves us with chruby as the Ruby manager. It's also important to note that ruby-install, the installer that complements chruby is decoupled and separate from chruby, so we will need to install that too.

I'm not sure why the postmodern guys would suggest doing a download of a .zip file when it is so much easier just to CLONE IT and do a git pull for upgrades later!

So we're going to tweak those instructions and make some major changes.

For starters, it is a pain in the butt trying to install .gem files locally because every ruby manager wants it done there way, either in some ~/.gem directory or somewhere that is more systemwide than local, so we're going to fix that. You also need to explicty put ruby it its own folder. chruby will recognize that you haver various versions of ruby in the directory /opt/rubies', but if you are not explict with saying where in that directory, it will scatter those files to several directories. This will be useful for when we install [JRuby](JRuby) later. I'm also hoping this will resolve some issue with gem installtion because I really want to use Jekyll, but like every other project that is out there, it seems to be written for the people who worked on the software not for the people who want to work with the software. Another thing we will do is add a file to /etc/profile.d/calledruby.sh` which will add our ruby path to the path. This worked quite well for Java and I figure using it for JRuby would be just as effective.

NOTE: I'm still trying to understand where I should put this. I don't know why when I put ruby into /opt/rubies/' it didn't just put everything into a ruby-2.3.0` folder.

$ cd ~/Software
$ cd mkdir rubies
$ sudo mv rubies /opt/
$ git clone https://github.com/postmodern/chruby
$ git clone https://github.com/postmodern/ruby-install
$ cd chruby
$ sudo make install
$ cd ..
$ cd ruby-install
$ sudo make install
$ exit                                   # leave the shell then log back in
$ cd ~/Software/ruby-install
$ ruby-install --prefix /opt/rubies/ruby-2.3.0/ ruby 2.3.0	# DO NOT ADD SUDO! A

# ruby-install --system ruby 2.3.0  # See the note below about this step.


# I'm starting to think adding "sudo" might not have been such a good idea, 
# especially since all the stuff we added is now owned by root instead of pi.
# This next command should fixt that.
$ sudo chown -R pi:pi /opt/rubies/ruby-2.3.0/
$ sudo reboot                            # Confirm the changes made by chruby
# INSTALL EVERYTHING ELSE AS GEMS!  These will be stored in /usr/local/source/ruby-2.3.0/gems or ~/.gems
# actually it would be /usr/local/lib/ruby/gems/2.3.0
# ...
# Test to see if Ruby and IRB work
$ which ruby
# /usr/local/bin/ruby
$ which irb
# /usr/local/bin/irb
$ which gem
# /usr/local/bin/gem
$ ruby --version
# ruby 2.3.0p0 (2015-12-25 revision 53290) [arm7l-linux-eabihf]
$ irb --version
# irb 0.9.6(09/06/30)
$ gem --version
# 2.5.1
$ gem list

*** LOCAL GEMS ***

bigdecimal (1.28)
did_you_mean (1.0.0)
io-console (0.4.5)
json (1.8.3)
minitest (5.8.3)
net-telnet (0.1.1)
power_assert (0.2.6)
psych (2.0.17)
rake (10.4.2)
rdoc (4.2.1)
test-unit (3.1.5)

$ gem install bundler             # A lot of projects use this one!

To get the version of ruby and the other in /opt/rubies/ working, we need to make a ruby.sh file in /etc/profile.d/

$ sudo vim /etc/profile.d/ruby.sh

Then in vim write

export RUBY_HOME=/opt/rubies/ruby-2.3.0
export PATH=$RUBY_HOME/bin:$PATH

It appears ruby-install still has some issues. Namely, using --latest is useless! If you use --latest it will install the oldest version of the newer Ruby versions can install rather than the newest version that you wanted. Be literal with the version! The second point about this step is that it will install any version of ruby to /home/pi/.rubies/ along with the other things. This is OK if you are developing for Ruby, but if you are trying to upgrade from 1.9.3, and tell all the future software to use the newer version of ruby, you will need to use the --system argument to put this newer version in /usr/local/ where it will override the 1.9.3. version in /usr/, assuming that in your $PATH variable, /usr/local/bin appears before /usr/bin, which it should! If you don't use the --system variable, it will add to the path /home/pi/.rubies/2.3.0/bin before /usr/local/bin and /usr/bin.

We can't remove the older version of Ruby (1.9.3) because of the stuff that Raspbian depends on it, but we can trick it into using a newer version. Hopefully DEBIAN will come to its senses as this is a problem that effects Ubuntu and Linux Mint as well. So if you are installing ruby with the chruby/ruby-install setup, you'll defintely want to use sudo ruby-install.

⚠️ DO NOT INSTALL JRUBY THROUGH RUBY-INSTALL ⚠️ See JRuby for how to install that.

If you using [Java] with Ruby using jRuby, I highly recommend you install ORACLE'S VERSION of Java, not OpenJDK. Oracle's is more up to date. But if you do it using ruby-install, it will not stop, not even with CTRL+C and try to install the older OpenJDK from Raspbian. You'll need to CTRL+Z then open htop to kill the 'apt-get' process, and then delete all the latest items that were recorded in /var/log/apt/history.log with sudo apt-get remove to roll back the stuff you didn't want to install in the first place.

IRB

Much like Python, Ruby has an interactive shell called irb.

Gems worth installing

🔧 TODO: Explain what at gem is.

  • compass
  • sass
  • haml
  • execjs
  • less
  • commonjs

Github Pages

OK, so you want to tinker with Github pages. Browser aside, this shout still be possible. The Github and Jekyll guys came up with a gem that installs all the ruby stuff you need for doing that. It comes with Coffescript, Sass, Yaml, and a bunch of other gems. But for some reason it didn't come with Haml or Bundler.

Simply just run this command in Bash.

gem install github-pages

Alternatively, add this line to your project's Gemfile.

$ gem 'github-pages'

As I previously stated, Bundler does not come with the 'github-pages' gems. I'd recommend installing it to manage your gems.

$ gem install bundler
$ bundle init                           # Creates the Gemfile file.
$ echo 'gem "rspec"' >> Gemfile         # Adds the rspec gem to the gem file.
$ bundle install                        # Creates the Gemfile.lock file.
$ bundle exec rspec                     # Execute (run) rspec

Of course, you will need to add the Gemfile and Gemfile.lock to your github repo.

git add Gemfile Gemfile.lock

As usual, commit then push.

See Also

External Links

Github pages/Jekyll links

Setup

  1. [Assemble the Hardware](Assemble the Hardware)
  2. [Install the Software](Install the Software)
  3. 🆙 [Setup your Raspberry Pi](Setup your Raspberry Pi)
  4. [Download the Missing Parts](Download the Missing Parts)

Typical Utilities

  • [Downloading and extracting with curl and tar](curl and tar)
  • [Browsing with ls and cat](ls and cat)
  • [Searching with grep and find](grep and find)
  • [Filtering with sed and awk](sed and awk)
  • [Piping with less, pv, and tee](less, pv, and tee)
  • Monitor your system with htop
  • Multiplex with tmux

Clone this wiki locally