Installing Ruby on Rails

Introduction

Setting up a sane Ruby environment seems like an easy task. Let me tell you, for the uninitiated, it isn’t.

That’s one of the reasons why CodeLearn is so great; you can concentrate on learning to write code without dealing with installation issues!

The process is actually filled with all sorts of pitfalls and choices, but, with a little reading, it isn’t too bad.

In this article, we’ll discuss how to get Ruby as well as Ruby Gems working on your system (*nix or Mac OS X). We’ll cover some of the most common mistakes and also some differing approaches.

Windows – you’re covered!

Windows screenshot

It is true that most of the Ruby/Rails community doesn’t develop on Windows, but, that doesn’t mean that it’s been left entirely unsupported!

In fact, the guys over at Engine Yard (a popular hosting solution for Rails developers) have the RailsInstaller project, which is absolutely fantastic! It is an installer that groups together everything you need to get started with Rails on Windows.

I might offer a bit of an opinion, however; if you’re getting serious about developing with Ruby, installing Linux is a great choice (or, getting a Mac), simply because there’s a lot more documentation for people who are on those platforms and using Ruby. More documentation means that things will be easier to troubleshoot when they (invariably) breakdown.

If you still like to stick with Windows, head over to RailsInstaller, download the installer & follow the screencast below. A word of caution – you need to deal with a lot of of command prompt – something most of the Windows users are not used to.

Now, we’ll get into the Mac and Linux installations – the procedure is basically the same for both platforms.

For *nix based system (Linux & Mac to be precise)

Don’t use your OS package manager!

If you’ve ever used Linux, you’ve probably heard of the “aptitude package manager”, or its frontend, “apt-get”. There are similar package managers in nearly every distribution; pacman, YUM, etc. If you’re on Mac OS X, you have MacPorts at your disposal. These are fantastic; letting you quickly installing all sorts of libraries instead of having to compile them.

But, setting up a Ruby environment is one place where you never want to use a package manager! There’s several reasons why – let’s go through a few.

First of all, the repositories which package managers use often have versions of Ruby that are out of date. There was a time when the default Ruby in the Debian repositories was a 1.8 version, when the 1.9 had already been released. This might seem like a small difference, but it can cause real annoyance when you’re trying to hunt down a bug or trying to figure out an error which is caused by a version discrepancy!

When 1.8 was held as the “ruby” package in the debian repositories, you had to download the package “ruby1.9″ to force it to install Ruby version 1.9 – but, that meant that when you ran ruby command, it still ran the old 1.8 version. The ruby1.9 command had to be executed to run the 1.9 version!

But, that’s not the only issue. Once you’ve started using aptitude for Ruby, you’ll have to start using the aptitude version of everything, because otherwise the version differences will constantly cause problems. That means that the installation documentation provided by the developers of libraries is basically useless => you’re on your own! That’s another reason why package managers cause problems with Ruby environments.

Take home point: don’t use a package manager when installing Ruby or Ruby libraries (i.e. Gems). The only place where you can use a package manager is when you need to install a native library. For example, if you’re installing the “mysql2″ gem, you must have the mysql client libraries installed; for this, you can use your package manager.

Okay, so, in general, no OS package manager for installing Ruby. But, what’s the alternative? Downloading, compiling and managing versions of Ruby is tedious, especially when you have multiple versions to deal with.

Fortunately, since Ruby has an awesome community, and we have this problem solved for us! rbenv and RVM are both packages that allow you to install and manage versions of Ruby, keeping you free from the insanity that entails from using an OS package manager.

If you already have Ruby installed on your PC using your OS package manager & you would like to follow the steps below, uninstall the Ruby binary first.

You need to use the package manager to uninstall Ruby. For eg – in Ubuntu, running `sudo apt-get remove ruby` should do the trick.

Installing required Binaries

If you are on a Linux machine, install the following

If you are on a Mac, you should have following installed

  • Xcode
  • Homebrew
  • git (once you have Homebrew installed, brew install git should do the trick)

RVM

Let’s start out with RVM. RVM stands for “Ruby Version Manager” – it’ll help you download, compile and manage versions of Ruby. Before you install it, make sure your package manager is working (homebrew for Mac OS X) – the installer will use it to install dependencies, e.g. openssl.

To install RVM, just plug this into a Terminal:

curl -L https://get.rvm.io | bash -s stable

P.S. – make sure you have curl installed on your PC before you execute the above command. You should be able to use your OS package manager to install curl. Refer to the links at the bottom of the article.

I have a few issues with this kind of install system. In essence, you are taking a script from an unknown source and running it – not safe at all. I understand that just pasting a command like this into your terminal is easy, but, security should always come first.

I would suggest, for the sake of good practice, look at the actual script you’re running before you run it, with this command (obviously, it is many hundreds of lines long). Visit https://get.rvm.io in your browser to see the script.

So, if you ran those commands and followed the instructions, rvm should be running on your system.

But, there’s still a couple more steps. You must add rvm to the end of your .bashrc / .bash_profile / .profile file in your home directory. The actual file depends on your OS. If more than one exists, simply add to any of the file.

.bashrc

.
.
# Your .bashrc content here. Append the below line at the end
source ~/.rvm/scripts/rvm

.bashrc file is in your home directory. You can open it in your favorite text editor (lets say it is vim) by executing vim ~/.bashrc

Before you move on, run this command to check you have all the stuff you need to install Ruby:

rvm requirements

The above command will make your OS package manager install dependencies required to install Ruby

Now, all you have to do is pick the version you want to use:

rvm install 1.9.3
rvm use 1.9.3

The first command will download and compile a version (in particular, version 1.9.3) for you to use! The second will select it as the version you want to use right now.

To check if Ruby has installed successfully, run ruby -v in the Terminal & you should see Ruby with version 1.9.3 .

Say that you want to test your awesome Rails app with version 1.9.2; that’s easy:

rvm install 1.9.2
rvm use 1.9.2

rvm use 1.9.2 only sets the Ruby version to 1.9.2 for your current session. If you open a new Terminal, the version setting is gone. To make it the default version do

rvm use --default 1.9.2

If you want to change the default version to something else, you can re-run the command with different parameter (lets say 1.9.3)

The next step is to install gems. Ruby broadly classifies gems into two categories – global & local. Your Rails app would need gems (lot of it). Different Rails apps can have different version for the same gem depending on your requirement. These are local gems.

There are some Ruby/Rails related tasks that are global. bundle & rake are two such commands. These are global gems. They need to be installed first. Fortunately, you just need to install bundle.

gem install rubygems-bundler

Test if bundle installed by running bundle -v. You should see the bundle version. About rake, when you install gems of any Rails app using ‘bundle install’, bundle will automatically install rake for you.

Installing gems is what you’d expect (you don’t need “sudo” because rvm does not install them in a restricted directory):

gem install rails

The above command will install rails in your system. Again to test, run rails -v in Terminal. If everything goes fine, you can now create a new rails app by running rails new my_kickass_rails_app.

So, with RVM, you’ve set up yourself with a nice environment that works like everyone else’s, so you can be sure that version discrepancies will not trip you up. But, there are a few caveats to RVM.

Possible hiccups

1. ruby or rails or rvm command not working – All your binaries are installed inside .rvm folder in your home directory. If the binary is missing, either you have deleted the .rvm directory or you forgot to add source ~/.rvm/scripts/rvm to your .bashrc file

2. How do I start over ? – You might have deleted .rvm directory by mistake. Or something on the way did not work & you want to start over. Simply delete .rvm directory. To cross check if you are at point zero, open a new Terminal & make sure ruby -v, rails -v, rvm -v commands do not show anything.

3. ERROR: While executing gem … – While doing gem install rails (or any other gem), you might get the error. Most probably, your gem version is old. Run

gem install rubygems-update
update_rubygems

Picked from this ruby-forum topic

4. Weird errors while running rails commands

Ruby/Rails development happens at a pretty fast pace & it might lead to rvm requirements command failing to pick up the right dependencies.

One of the possible scenario is readline error while launching rails console . Usually rvm requirements is able to take care of such things, but in rare case it could not; you need to google for the missing dependency, install it via your OS package manager & then reinstall the Ruby version.

To reinstall a Ruby version you may do

 rvm remove 1.9.3
 rvm install 1.9.3

or you may do it in one line

 rvm reinstall 1.9.3

Problems with RVM

As I mentioned, the RVM install process is not something I’ve fond of because it entails running code from some random domain on your computer (i.e. look at where we run the installer script, we’re basically downloading a script from get.rvm.io and running it) – for all you know, it could clear your harddrive or steal your car!

Secondly, the RVM process actually modifies the “cd” command – the reasons why it does this are a little complicated, but, it needs to in order to make sure that Ruby versions are available to use.

RVM, in general, is quite obtrusive – it requires control over every aspect of the environment in order to make sure nothing goes wrong. It relies on a lot of hackery and adjustments in order to get Ruby available everywhere. You can see the results of this when you constantly get warnings when switching projects.

Since this is Ruby and the community is fantastic, we have an alternative! It is called rbenv.

If you want to install rbenv on your PC & already have RVM, remove RVM first. `rvm implode` should do the trick.

RVM has installed all binaries like Ruby & Gems inside .rvm in your home directory. So you can simply remove .rvm directory (or rename it to something else) to get rid of RVM, Ruby & Gems installed from RVM. Make sure to remove `source ~/.rvm/scripts/rvm` from inside of .bashrc/.bash_profile/.profile file too

rbenv

Before you begin with rbenv, you need these stuff installed:

  • cURL
  • OpenSSL library

You can use your package manager to install these. To do this on, say, a Ubuntu/Debian derivative:

 sudo apt-get install curl
 sudo apt-get install libssl-dev

If you are on Mac OS X, check out the links section to install the Homebrew package management system, which you can use to install anything you might need (however, you should, for the most part, be ready to go already).

rbenv setup is significantly more involved than the RVM setup (note: if you have already installed RVM, do not install rbenv along with it – things will break); we start with (you must have git installed in order to do this – it is fine to use a package manager to install git, check out the links section below):

git clone https://github.com/sstephenson/rbenv.git ~/.rbenv

This will create many different files in the ~/.rbenv directory. Now, we want to make sure that when we type in “rbenv”, the system knows where to find the tool “rbenv”, so, we add it to our PATH:

echo 'export PATH="$HOME/.rbenv/bin:$PATH";eval "$(rbenv init -)"' >> ~/.bash_profile

If you are on Ubuntu, that should be “~/.profile”, not “~/.bash_profile”. In order for us to see the changes, you will have to “restart” your terminal. You may open a new Terminal & see if rbenv command exists by typing which rbenv.

Unlike RVM, by default, rbenv does not provide automatic compilation for Ruby – that means that you either have to compile them yourself, or use the ruby-build module. Clone the ruby-build repository from github:

git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build

That will add it to the existing ~/.rbenv directory. Now, you can install yourself a Ruby version:

rbenv install --list

The command will list Ruby versions for you.

rbenv install <pick your Ruby version from the list>

Obviously, replace the “<pick…" text with the version you would like to use. rbenv needs you to do one last step:

rbenv rehash

This allows rbenv to “rebuild” its memory, so to speak. That should give you a version of Ruby running on your system. You can pick a Ruby version by using:

rbenv local <a Ruby version that is installed using rbenv install & you want to make it default>

rbenv also supports specifying a version with a “.ruby-version” file. This might seem like a trivial concept, but, when deploying your awesome Rails app to a server, having a file that sets your version is much easier than having to set it up manually.

Overall, rbenv seems like a lot of work for nothing; it seems to be doing just about the same things as RVM. However, in my opinion, it accomplishes its goals with more concern for the well-being of your system in the future and it is better suited to deployment than RVM.

A Note on Dependencies

For many Linux systems, you should have all the dependencies you need in order to get up and running with a version of Ruby.

However rbenv does not install the dependencies on its own (unlike RVM) – you will have to do that yourself. It will tell you what packages you are missing, but, it does not offer a clue as to how to install them. To do this, you are free to utilize your package manager.

For example, you will likely need the OpenSSL libraries, so, if you’re on a Debian-derived system (e.g. Ubuntu):

sudo apt-get install libssl-dev

Typically, the “-dev” or “-development” packages are what you need. However, do not use your package manager to install “-ruby” libraries! These are typically versions of gems; you should just use the actual gem command in order to install those!

Wrapping it up

Hopefully this article helped you create your own Ruby environment – one that won’t lead to hours of confusion, waiting on IRC and questions on forums.

Quite literally, a third of the questions I’ve read on the #ruby channel on Freenode (an chat group) are about gems acting oddly due to “apt-get” gone rogue. You can now concentrate on building your awesome Rails app!

Links

Setting up XCode on Mac OS X.

Setting up GCC on Ubuntu.

Installing git on Mac OS X and Linux.

Installing cURL on Linux

RailsInstaller.

Interested in contributing similar blog on Codelearn that reaches 10k users, contact pocha@codelearn.org . For a limited time, we are offering $100/blog post.
  • Nilesh Trivedi

    It would have been nice if you listed all the native libraries that need to be install for Ruby compilation and installation of typical gems to succeed. Overall, a great post! :)

    • http://www.codelearn.org/ Ashish Sharma

      If you using RVM, rvm requirements should take care of binaries installation for Ruby. For gems like mysql2, mysql client would be needed. A comprehensive lists of binaries for the default Rails app gems can be done. Good point. Will ask Dhaivat to add a separate section.

  • Pingback: Starting with Codeschool « Codelearn Blog

  • Sıtkı Bağdat

    Good post, but it didn’t works for Linux Mint. Maybe this link helps who is using my OS: http://www.andrehonsberg.com/article/install-rvm-on-linux-mint-12

    • http://www.codelearn.org/ Ashish Sharma

      What part did not work for Linux Mint ? Mind elaborating ? You can probably put the missing instructions for Linux Mint in the comment here.

      • Sıtkı Bağdat

        I come across a problem with rvm installation on this command: “curl -L https://get.rvm.io | bash -s stable”

        • http://www.codelearn.org/ Ashish Sharma

          I dont see a difference between the statement in the blog &

          bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)

          get.rvm.io eventually redirects to the url you talked about. Do you mean that curl -L does not work in Linux Mint ?

          curl -L is suppose to follow redirects & it will eventually resole to the github url you have mentioned.

          • Sıtkı Bağdat

            I don’t know what the problem is with curl -L, but it raises a certificate error related to secure connection despite i have installed these. Also I tried –insecure option, but can’t install.

          • http://www.codelearn.org/ Ashish Sharma

            I think its an issue with your system certificates. Check https://groups.google.com/forum/#!msg/rubyversionmanager/WkLW8uIw7PA/HXFVS9aYHIYJ

          • Sıtkı Bağdat

            Thanks, but i tried this.

  • http://jitendravyas.com/ Jitendra Vyas

    I already have ruby, devkit and git installed in my Windows 7 PC because I use Jekyll. Now if i want to setup Rails environment and Install Railsinstaller would it be fine if it will have different version of ruby, devkit and git?

    • dhaivatpandya

      This depends on how ruby, devkit and git were installed originally. I think you may have a problem with your PATH variable since Windows will find two versions of Ruby once you use RailsInstaller. I don’t think devkit should be a problem. Git is not likely to be a problem since RailsInstaller will just overwrite the old version if you tell it to. However, two different versions of Ruby can definitely be an issue.

      In fact, this is exactly the use case where RVM was originally conceived (although not on Windows) – it allows you to handle version mismatches really easily/well.

      I would suggest (if you want to stick with Windows) uninstalling ruby, devkit and git and reinstalling them with RailsInstaller (which should get the PATH working, etc.). Even more than that, if you’re getting into Ruby/Rails more, setting up a Linux machine even on a VM (VirtualBox is what I use) shouldn’t take too much time and will make your Rails journey considerably easier.

      I hope I answered your question – if you have any others, just drop a comment!

      Thanks,

      Dhaivat

      • http://jitendravyas.com/ Jitendra Vyas

        Hi Dhaivat,

        I appreciate quick reply.

        1) After a long efforts anyhow i’m able to install and use Jekyll on my windows so if I would uninstall everything and reinstall it might stop to work
        2) I have 128 GB SSD and only 20GB is left now so I don’t know how much space will required to run linux from VM.

        3) I want to stick with windows because I also use photoshop and some other software which are not available on Linux and I don’t have money to buy a Macbook

        If it would be really impossible to run rails project on my windows then i would think to sell my windows laptop and add more money to buy macbook

        Please let me know your thoughts.

        • dhaivatpandya

          Hi,

          I see your problem.

          1 – You can reinstall Jekyll and that should the problem, atleast temporarily. I admit it is not a simple process (made complicated by the fact that RedCloth has native extensions), but, I think with devkit, git, etc. properly installed with RailsInstaller, you should be fine.

          2 – A Linux VM (I use Linux Mint or Arch Linux) shouldn’t take up more than 8GB or so of space.

          3 – Windows support in the Rails community is shoddy at best. The problem is that very few gems are actually tested on Windows, so, even if you do get Rails and Jekyll working at the moment, you will likely have trouble as you move along your Rails app development. You can get a server with DigitalOcean so that you can use Linux without taking up disc space on your laptop. Or, you could get an external hard drive, move some of your files over?