##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! 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. http://vimeo.com/18787139 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 * cURL * git 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 http://vimeo.com/68724013 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.
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 http://vimeo.com/68728522 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