Install the latest version of Ruby using Homebrew. Ruby on Rails 5 requires Ruby 2.2.2 or greater.
- [Narrator] In this movie, we'll make sure that you have the correct version of Ruby installed. Chances are you're working with some version of Ruby 2, Ruby 2.0.0 is what was included with Mac OS 10.11 El Capitan, that certainly could be upgraded by Apple at some point if they wanted to, but by default that's probably what you have if you have El Capitan installed. Version 2.2.2 is the minimum that's required for running Ruby on Rails 5, in order to get all the Ruby features that we need such as the new advanced garbage collection that's part of Ruby 2.2, we need to be using 2.2.2 or greater.
However, Ruby development has continued past that, and it's actually at 2.3.1 at the time that I'm recording this, and they're already previewing version 2.4, so that may be right around the corner as well. So you wanna use the latest and greatest Ruby is what's recommended, but 2.2.2 is a minimum that we must meet, so we wanna make sure that we at least have that. Now we'll check what version we have and get it upgraded in a moment, but first I just wanna mention that there are a couple of options for installing a new version of Ruby. The first and most basic is just to go to the main Ruby website, and that's ruby-lang.org.
If you go there, you can click on a link for install, and it'll give you various install options, and you can actually download the package yourself and get it installed. However, remember we just installed Homebrew to help us install other software, and we can use Homebrew to install Ruby. Homebrew knows about Ruby, it can install it for us, and that version would supersede the version that's installed by default on our Mac. It won't replace it, it'll just supersede it. But there's an even better way than doing it with those two, and that is to use a version manager, and there are two that are very popular.
One is Ruby Version Manager, or RVM for short, and the other is rbenv, which people pronounce rb-env. So RVM or rbenv, they sound very similar, but they're different. Those are two ways of managing Ruby versions. What's great about them is they allow you to have multiple versions of Ruby installed, and to even have different versions of Ruby assigned to different projects. So you could have one website that's using Ruby 2.2.2, another one that's using 2.3, and another one where you're experimenting with using the new Ruby 2.4 that's about to come out, and each one of those can be pegged to a different version of Ruby and have the appropriate Gems and code that goes with it all installed and compartmentalized with each version of Ruby.
So it's very nice, and they've become very popular. I think that rbenv is slightly more popular, and that's the one that I like, and that's what we'll be using here. All right, let's switch over to our command line and take a look at the Ruby that we already have installed. In order to find out what version of Ruby you have installed, you can just type ruby -v, and it will come up and it will tell you. See, I have 2.0.0, and if I say which ruby, it'll tell me where that's installed. This is the default one that Apple gives me when I install Mac OS 10. I wanna upgrade that.
I need to have something newer than 2.0.0, so let's go over to the Ruby Lang website, ruby-lang.org, and let's take a look at what the newest version is. Now you'll see if you scroll down the list here, it'll tell you which versions have been released. And here's the preview version, we don't want that though, we want 2.3.1, the newest final version that's been released. You can see 2.2.5 was released, you can scroll down and see older versions, sometimes those older versions will include something like a patch level, it'll say, "2.3.1-p345," let's say, and that will be a specific number letting you know about some patches that have been applied, some minor, minor edits that have been made to that.
That's all part of the version number. So 2.3.1 is the latest version as I'm recording this, and that's the one that I wanna use. Okay, now we could go over here and actually download it or get it installed, but I don't wanna do it from there. Instead, I'm gonna switch back to my command line and we're gonna start by getting rbenv installed, and I'm gonna do that using Homebrew. Homebrew will do it. Let's start with brew update, that's always a good first step, make sure that my Homebrew is up to date and it has all the latest information it needs about which packages are available, and then I'll do brew install rbenv, it'll take a moment while it installs that, and the rbenv is actually just a manager for it, it just manages switching between different versions of Ruby.
It doesn't actually do the installing for us. In order to do the installing, we're gonna install a second little program that's called Ruby-Build. So as soon as rbenv is all installed, that'll be the next step to install the Ruby-Build library. And it says here it's a dependency. Oh, it's actually gonna install it for us. There we are. So we don't actually need to install it. It did it for us automatically. That's nice. So now that everything is there, the next step is that we need to enable rbenv, and we do that by adding it to our Bash profile.
You can run the rbenv int command that it suggests, and it will tell you about this, but what we really wanna do is if we remember we have ls -la, we have this Bash profile that loads every time we open a new window. We wanna use nano and edit that file again. Now under here under this line, I'm gonna add a new line that is eval space, and then double quotes, then a dollar sign and open parentheses rbenv, space init, space and then a dash, close the parentheses, close the double quotes.
Make sure you've got that right. What it's saying is, "Run this command," right? "From the command line, run the rbenv initialization," and that's what it'll do. Every time we open a new window, it'll initialize rbenv for us. Now if we wanna stop using rbenv in the future, we just take this line out, and it'll stop initializing it for us, so it's kind of nice. All right, so we'll do Control + X, like it says at the bottom in order to exit, we'll type a Y to save our changes, and return to accept the file name. All right, if we take a look with cat .bash_profile, you'll see that our change is there and it is spelled correctly.
Now we don't have rbenv actually working yet, 'cause we need to close a window and reopen it in order to get the init script to kick in, or another alternative is you can type source, and then put a space, tilde to target our home directory, and then .bash_profile. And that will just rerun that .bash_profile command for us. Now we have rbenv initialized. So the first step we wanna do, let's do rbenv install --list.
This is going to return a list of the available versions of Ruby that rbenv knows about. So you can see there's a lot of versions that are available here, and there's lots of different things and lots of nuisances to it, jruby, things like that. You wanna look at the main versions, the ones that are just numbers. So here's the ones we've got. You can see when I get up here we've got 2.3.1, right? You see there's other versions here like I was talking about that had patch levels, that's the -p594. You can install very specific versions of Ruby, but the one we care about is this one, 2.3.1, the most recent one that is not a preview release.
So I'm just gonna copy that just to make sure that I have that in my clipboard and ready to type. Let's clear our screen, and let's type rbenv, and then install, and now instead of typing list to list out the installs, we're gonna actually tell it to install 2.3.1. I'll hit return, rbenv will go out, it will get the package the same way the Homebrew goes out and gets a package for us. It'll go out, it'll grab that, bring it back, and install it for us. This installation process is going to take a little while, so this is a good time for you to pause the movie, go stretch your legs, get a drink, and then come back and check on it in five or 10 minutes.
Once it's done, we now have Ruby 2.3.1 installed and available on our machine, but there are a few more commands that we need to know. The first is rbenv rehash. This is a command that you should run anytime that you install a new version of Ruby or you install a RubyGem that provides additional commands to the command line. It essentially just says, "Hey rbenv, "be aware of all these different commands "that might be out there that might get issued." So we wanna go ahead and use that every time.
And then we can use rbenv versions to see which versions are installed. You can see I have my system version, and you can see I have 2.3.1. Now notice that there's a star next to system, right? That's the one that's being used, the one that has the star next to it, and sure enough, if I type ruby -v, you see I'm still using that system version. I'm not using the new 2.3.1 that I just installed. In order to do that, I need to tell it to use it, and I do that with rbenv global 2.3.1.
Now let's switch back to rbenv versions. Now you see that the star has moved, and my new Ruby version, you see that I am getting 2.3.1, okay? Now that sets it at a global level. I also have two other options for that. One is to set it at the local level by using rbenv local 2.3.1, or whichever version I want, and that's gonna set it for a particular directory, typically for a specific project that you're working on, and it will store a little notification in a dot file in the directory to keep track of which version it should use for that directory.
So you can have different projects that use different ones by using local. And then the third option, instead of global or local, is shell, and that says, "Just change it right now, "just for the current window that I'm working in, "don't save this and remember it for the future. "Just real quick pop over to this other version of Ruby "and let me use that in the current window." And then the one last command that you need to know is just like we had rbenv install, you can also have rbenv uninstall, and it would uninstall a version of Ruby, as well.
All right, well now we have rbenv installed, and we also have the version of Ruby we want installed and working. We're ready to move onto installing RubyGems.