1. Installing Ruby on *NIX systems

So, you have decided to learn Ruby. Now what should you do? Well, it is simple, you go and learn Ruby! Easy, right?

Ok, enough joking. The first step you need to take to start learning Ruby is install Ruby on your machine, so that you can run you code. Ruby is an interpreted language, so you need to install the Ruby interpreter on your machine. The Ruby interpreter, which we will know as ruby, will run the code you write on your machine. It comes along with a whole set of libraries and tools for making your life easier as a programmer.

If you are on a Linux machine or a Mac, chances are, you probably already have ruby installed. But the problem is that it is outdated. So, you need to install a newer version of Ruby.

There are several ways to install Ruby on your machine. You can use Ruby Version Manager (rvm) or rbenv. While both of these approaches are good ways to install Ruby, I prefer to use ruby-install along with chruby.

Installing ruby-install

ruby-install is a piece of software which does the actual installation of Ruby. You can install different versions of Ruby on your machine at the same time, and then use chruby to manage which version of Ruby to be used on which project. I will describe this in a minute, but first, let’s install ruby-install.

Installing ruby-install is quite easy. First cd into the tmp directory (you don’t have to do this step, but it is recommended). So:

Now, download the latest version of ruby-install and extract the archive file:

And then, cd into the ruby-install directory and install it!

If you are on a Mac, and you are a Homebrew user (which you should be), you can install ruby-install using the following command:

And, boom. You are done! Well, not actually, but you have installed ruby-install.  Next, you need to install Ruby.

Installing Ruby

Now that you have installed ruby-install you can use it to install Ruby. First, let’s get the list of supported Rubies and their versions by running:

This will show you a list of available Ruby interpreters (yes there are several Ruby interpreters available) and their versions. As of this writing, running the above command shows me the following list:

We are interested in the ruby part and we want to install the latest version (2.5.1 in this case):

This will download Ruby 2.5.1 as an archive (tar.bz2 in my case), extract it and then, will install it on your system. The -c flag tells ruby-install to remove the archive file and the extracted directory when it’s done with the installation.

Great, you have successfully installed Ruby 2.5.1 on you machine! You can install different versions of different Ruby interpreters just like you did with the above command.

Installing chruby

Now that you have successfully installed ruby-install and Ruby 2.5.1, you need to install chruby. But before we begin the installation, let’s see why we need chruby.

When you advance in your career as a developer, there are times when you will be working on different projects and each of these different projects might need a different version of Ruby.

For example, as of this writing I have Ruby 2.2.3, 2.4.2 and 2.5.1 installed on my machine. Now, you might wonder how does our machines know when and where to use which version of Ruby?

Enter chruby. With chruby you can manage where to use which version of Ruby on your machine. I will describe how you can do that in a minute, but first, let’s install chruby!

Again, cd into the /tmp directory (if you are not already there):

Download the chruby archive and extract it:

then, cd into the extracted directory and install chruby:

When the installation is finished, open ~/.bashrc or ~/.zshrc or ~/.bash_profile file on you machine and append the following two lines to it:

Open a new terminal window and run chruby. Here is the output on my machine:

This shows the list of installed Ruby interpreters with their version number on my machine. So, I have 3 different versions of the ruby interpreter. The asterisk next to ruby-2.5.1 shows that Ruby 2.5.1 is the active Ruby version in my terminal session. If I want to change to, say, Ruby 2.2.3, I can do so with the following command:

Ok, you are done with the installation, and you can now go an learn Ruby. But before you do that, let me explain what the two lines of code that you added to your ~/.bashrc file do.

The first line

loads chruby when you open a new terminal session (that is when open a new tab or window in you terminal emulator), so you can run the chruby command.

The second line

is more interesting. This line enables the auto switching feature of chruby. I will explain the auto switching feature in the next section.

Auto switching Ruby versions

As I mentioned before, there will be times in your career life when you will be working on different projects and each project might require a different version of Ruby.

With chruby it is very easy to switch between different Ruby versions whenever you cd into a new directory.

Imagine we are working on an awesome project and we want to be sure that each time we cd into the project directory, Ruby 2.4.2 will be activated automatically. All we need to do is create a file with the name .ruby-version and specify the version number in that file. So, in our project, the contents of the .ruby-version file will look like this:

And that’s it! Now, every time we cd into our awesome project, Ruby 2.4.2 will automatically be activated.

If you want to specify a global Ruby version for your machine, all you need to do is create the .ruby-version file in your home directory.

So, if my ~/.ruby-version file looks something like this (note the ~ at the beginning of the file path):

when I open a new terminal session, Ruby 2.5.1 will be activated. If I cd into our awesome project, Ruby 2.4.2 will be activated and if I switch outside of the project, again, Ruby 2.5.1 will be activated. Isn’t that really cool?

Of course, to have this auto switching work, you should have enabled it as I described above (by appending the two lines to your ~/.bashrc file).

Conclusion

We learned how to install different versions of Ruby on our machine and how to manage the active Ruby version in our projects.

If you have any questions, feel free to ask me in the comments. Also, if you have any suggestions I’d be more than glad to hear them in the comments.

Leave a Reply

Your email address will not be published. Required fields are marked *