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 ;)

Dmitry L Rocha
The Miners
6 min readSep 12, 2016

--

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:

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

Extract it and compile to user space:

The last things are all about configuration:

1. Put ~/local/ruby-2.3.1/bin in your PATH:

2. Set GEM_HOME to ~/.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:

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

Now you are ready to install gems like bundler or 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:

Next download and execute the script from https://get.rvm.io:

You need to have curl to execute the command because the script is all based on it.

Now you can source the file:

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:

2. Install the latest:

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:

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:

Add ~/.rbenv/bin to your PATH, I will add it only within my shell session:

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:

To get the list of available Ruby versions:

To install one of them:

Using chruby

Download and install:

Load chruby:

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:

Don’t forget to add ~/local/bin to your PATH:

Now you are ready to install Ruby:

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:

And you are already within a container with Ruby:

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:

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:

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

To build:

To generate a Rails app:

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:

https://gist.github.com/dmitryrck/c82f7e1076442a6aa79a57ad37f0031c

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 :)

--

--