4.5 Ways to Install Ruby in Userspace

TL;DR RVM is the fastest way to get things up and running. I really enjoy chruby and docker, try them if you have time ;)

Recently a Rubyist friend of mine got a new client. He liked the client’s software idea, but the software was written in Python. Because of that, he thought about rejecting the client or rebuilding it all in Ruby on Rails. However, rejecting the client is a bad idea for his ego and rebuilding is a bad idea for the client — after all it will take time.

Based on that I though about Ruby newbies: how can they start nowadays?

The following are five ways to install Ruby in your user space. They are especially useful when you need to deal with more than one version.

These steps will only work on Unix-based systems.

Compiling and configuring by yourself

I recommend that you install these dependencies (if you are using a debian-like):

build-essential patch bzip2 gawk libc6-dev libreadline-dev libreadline6-dev zlib1g-dev libssl-dev libyaml-dev autoconf libgmp-dev libgdbm-dev libncurses5-dev automake libtool bison pkg-config libffi-dev

Some other packages can be useful:

  • libsqlite3-dev (also recommended: sqlite3) to use sqlite3;
  • libmysqlclient-dev (also recommended: mysql-client) to access MySQL;
  • libpq-dev (also recommended: postgresql-client) to access PostgreSQL;
  • wget to download files from terminal;

Let’s install all the packages:

%  apt-get install --yes build-essential patch bzip2 gawk libc6-dev libreadline-dev libreadline6-dev zlib1g-dev libssl-dev libyaml-dev autoconf libgmp-dev libgdbm-dev libncurses5-dev automake libtool bison pkg-config libffi-dev libsqlite3-dev sqlite3 libmysqlclient-dev mysql-client libpq-dev postgresql-client wget

Go to Download Ruby and download the latest version, today it is 2.3.1:

```
%  wget https://cache.ruby-lang.org/pub/ruby/2.3/ruby-2.3.1.tar.gz
```

Extract it and compile to user space:
% tar xfz ruby-2.3.1.tar.gz
% cd ruby-2.3.1
% ./configure --prefix=${HOME}/local/ruby-2.3.1 --disable-install-doc --disable-install-rdoc && make && make install

The last things are all about configuration:

  1. Put ~/local/ruby-2.3.1/bin in your PATH:
    %  export PATH=${PATH}:${HOME}/local/ruby-2.3.1/bin
    
  2. Set GEM_HOME to ~/.gem/ruby/2.3.1:
    %  export GEM_HOME=${HOME}/.gem/ruby/2.3.1
    

    GEM_HOME is the place of new gems.

  3. Set GEM_PATH to ~/.gem/ruby/2.3.1 (your GEM_PATH) and
    ~/local/ruby-2.3.1/lib/ruby/gems/2.3.1:

    % export GEM_PATH=${GEM_HOME}:${HOME}/local/ruby-2.3.1/lib/ruby/gems/2.3.1
    

    GEM_PATH is the place in which ruby will look for gems.

Now you are ready to install gems like bundler or rails:
% gem install bundler rails
Don’t forget to add those export commands to your .bashrc or .zshrc.

Using RVM

RVM installer script uses GPG to check signatures; because of that, the first thing to do is to download the gpg key:
% gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
Next download and execute the script from https://get.rvm.io:
% \curl -sSL [https://get.rvm.io](https://get.rvm.io) | bash -s stable
You need to have curl to execute the command because the script is all based on it.

Now you can source the file:
% source ~/.rvm/scripts/rvm
Alternatively, you can reopen your shell session in order to load the configuration installed by RVM within your .bashrc.

With your terminal prepared let’s install the latest Ruby:

  1. Get the list of known Ruby versions:
    % rvm list known
  2. Install the latest:
    % rvm install 2.3
    One good thing about RVM is that it installs system dependencies for you.

Now you can, for example, install the rails gem and start a new project:
% gem install rails
% rails new my-blog

Using rbenv

NOTE: Neither rbenv nor ruby-build install any dependencies in your system. I recommend that you read the first section to know more about dependencies necessary to compile Ruby. Also, you need to install curl.

To install:
% git clone https://github.com/rbenv/rbenv.git ~/.rbenv
Add ~/.rbenv/bin to your PATH, I will add it only within my shell session:
% export PATH="${HOME}/.rbenv/bin:${PATH}"
I recommend that you add this line in your shell profile (like, .bashrc or .zshrc).

rbenv only sets your environment variables to use the Ruby version you choose. This means: you need to manually install Ruby OR to use rbenv and ruby-build together.

You already know how to compile Ruby from source, but now I will use
ruby-build:
% git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
To get the list of available Ruby versions:
% rbenv install --list
To install one of them:
% rbenv install 2.3.0

Using chruby

Download and install:
% wget -O chruby-0.3.9.tar.gz https://github.com/postmodern/chruby/archive/v0.3.9.tar.gz
% tar xfz chruby-0.3.9.tar.gz
% cd chruby-0.3.9
% make PREFIX=~/local install

Load chruby:
% source ~/local/share/chruby/chruby.sh
% source ~/local/share/chruby/auto.sh # if you like auto switching

Again: I recommend that you add it in your shell profile (like, .bashrc or .zshrc).

You already know how to compile Ruby from source or to use ruby-build, but now I will use ruby-install:
% wget -O ruby-install-0.6.0.tar.gz https://github.com/postmodern/ruby-install/archive/v0.6.0.tar.gz
% tar xzf ruby-install-0.6.0.tar.gz
% cd ruby-install-0.6.0/
% make PREFIX=~/local install

Don’t forget to add ~/local/bin to your PATH:
% export PATH=${PATH}:${HOME}/local/bin
Now you are ready to install Ruby:
% ruby-install ruby 2.3
Like RVM ruby-install installs system dependencies for you and by default chruby and ruby-install installs/looks for ruby in ~/.rubies.

Using Docker

If you haven’t already, follow instructions on this page to install docker: Install Docker Engine.

Despite the fact Docker runs as superuser we can call its API using our normal user.

This is the simplest way, I mean, I already have Docker for PostgreSQL, Redis, and a lot of other stuff.

Just run:
% docker run -it ruby bash
And you are already within a container with Ruby:
% docker run -it ruby bash
root@2b15fb3221ee:/# ruby --version
ruby 2.3.1p112 (2016–04–26 revision 54768) [x86_64-linux]
root@2b15fb3221ee:/# irb
irb(main):001:0> puts "Hi! :)"
Hi! :)
=> nil

Unfortunately, it will be a bit tricky to generate a new Rails app.

I will show you two ways to do that.

The first one is to start the container, install the rails gem and
run rails new:
% docker run -v $PWD:/app -it ruby bash
root@c75224ef6368:/# gem install --no-ri --no-rdoc rails
[…]
root@c75224ef6368:/# cd /app
root@c75224ef6368:/app# rails new my-blog

But we all know that docker will lose the installation of the gem and the created app’s directory will be owned by the root user.

Another way is using a Dockerfile to create a “rails image”, this is the
Dockerfile:
“`Dockerfile
from ruby

run useradd -m -s /bin/bash -u 1000 saymyname

run gem install --no-ri --no-rdoc rails

run chown -R saymyname.saymyname /usr/local/bundle # it is not the better way to do that, though

user saymyname
```

If you are the second or third user of the machine you need to change the user id (1000) according.

To build:
% docker build -t rails .
Sending build context to Docker daemon 114.7 kB
Step 1 : FROM ruby
---> e0f509c5f9ee
Step 2 : RUN useradd -m -s /bin/bash -u 1000 saymyname
---> Using cache
---> 210c4e35f2c3
Step 3 : RUN gem install --no-ri --no-rdoc rails
---> Using cache
---> ff4f2a90873b
Step 4 : RUN chown -R saymyname.saymyname /usr/local/bundle
---> Using cache
---> 81464adc4d3b
Step 5 : USER saymyname
---> Using cache
---> 2ebcf66aa79e
Successfully built 2ebcf66aa79e

To generate a Rails app:
% docker run -v $PWD:/app -w /app rails rails new my-blog
And once you are within your Rails app I recommend that you use
docker-compose. With docker-compose you can start the Rails app, Redis, PostgreSQL/MySQL, ElasticSearch and whatever you want 🙂

See this gist for an example of Dockerfile and docker-compose.yml:

version: "2"

services:
  db:
    image: postgres
    volumes:
      - /var/lib/postgresql/data
    expose:
      - '5432'

  web:
    build: .
    volumes:
      - .:/app
    ports:
      - "3000:3000"
    links:
      - db

from ruby:2.3.0

env DEBIAN_FRONTEND noninteractive

run sed -i '/deb-src/d' /etc/apt/sources.list
run apt-get update
run apt-get install -y build-essential postgresql-client nodejs

env PHANTOMJS_VERSION 1.9.8

run wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-$PHANTOMJS_VERSION-linux-x86_64.tar.bz2 -O- | tar xf
run ln -s /usr/local/phantomjs-$PHANTOMJS_VERSION-linux-x86_64/bin/phantomjs /usr/local/bin/phantomjs

workdir /tmp
copy Gemfile Gemfile
copy Gemfile.lock Gemfile.lock

run bundle install

workdir /app

cmd ["rails", "server", "-b", "0.0.0.0"]

Conclusion

I totally recommend that you to use RVM if you are a completely beginner in Ruby. It even has an implode command to help you reinstalling in case you run into trouble.

I really like to use Docker, but for me it is the secondary way to use Ruby, just because I like to have Ruby in my user space. For example: sometimes I just open irb to try out a method.

I hope you find this useful and if you have any questions please leave them in the comments.

References

Thank you Thiago Araújo Silva and Will Soares for reviewing this post 🙂