From the course: Installing and Running Ruby on Rails 6

Install Ruby on macOS for a Rails project - Ruby on Rails Tutorial

From the course: Installing and Running Ruby on Rails 6

Start my 1-month free trial

Install Ruby on macOS for a Rails project

- [Instructor] Once we have Homebrew installed, the next step is to ensure that we have a compatible version of Ruby that we can use with Ruby on Rails. Ruby on Rails 6 requires Ruby version 2.5 or later. If you're using macOS Catalina or later, then you should have Ruby 2.6.3, that's what's installed by default. And that would certainly be new enough to use with Ruby on Rails 6. This is also referred to as System Ruby, a version of Ruby that Apple provides with the operating system. It is notable that that in the macOS Catalina release notes, Apple warns that future versions of the operating system may not include Ruby by default. There may not be a system, you may have to install something to begin with. It's great to have that version of System Ruby there to get us started, but it's also not a great idea to rely on it, after all, if Apple updates the operating system, the version of Ruby that's included would be upgraded as well, and that may not be something that you want to do. Your project may depend upon having certain versions of Ruby, so it's better if we can control and manage our own version of Ruby, and that's what we're going to see how to do. There are a few different ways that you can do that. You can go to the Ruby language website and download installers from there, you can use Homebrew to actually install a version of Ruby, or what most Ruby developers do is they use either RVM, short for Ruby Version Manager, or rbenv. Both are small package managers that specialize in just managing the version of Ruby that we're using. What's great about both of those is it allows us to control the Ruby version that we're using, to have multiple versions installed, to switch between them, and even to have some projects use one version, while another project uses a different version of Ruby, and that's very useful when you have some older Legacy projects that are still using older versions of Ruby on Rails that need an older version of Ruby. But you can still then have newer projects that use newer versions of Ruby on Rails and newer versions of Ruby. Let me show you how to install rbenv, that's the one I prefer. To do that, I'm going to go to the command line, you can see I've already checked to see what version of Ruby that I have. I'm going to type brew update, 'cause I'm going to use Homebrew in order to do the installation. It's going to go out and check and see if there's anything new that it needs to download. It did find something new and it downloaded that, but no other changes were needed. So, now I'll say brew install and then rbenv, with E-N-V. Now if I type that, it's going to go back out and get the package for rbenv and download it. Now at the same time, it's also going to download something else called Ruby Build, and Ruby Build is going to allow us to install new versions of Ruby. We need both of those together. At this time, we want to initialize it, but we don't want to just initialize it this one time, we want the initialization to happen every time we launch a new window. So, in order to do that, I'm going to go in and edit a configuration file. First, let's make sure that we're in our user directory using cd, space, and the tilde symbol. Now from that directory, if I type ls, space, dash la, we'll get a list of the files and you'll see that I have a file here called bash profile. If you don't have it, don't worry about it. It's a small file in the user directory where you can put configurations and those configurations get run every time we open up a new window in terminal. I'm going to make an edit to that file and if you don't have it, you'll type the same command, whether it exists or not. I'm going to type nano, which is a small little text editor program, and then space, .bash, underscore, profile. Make sure you spell it correctly. Now you can see I've already got a few configurations in here. I've got something that changes the way my command line looks, that's what this first line does. I've got an alias here so that I can have a keyboard shortcut ll, it's going to be the same thing as typing ls with a few options. And I've got something here that just takes away some warnings that it gives me about using the z shell instead of the bash shell. None of those are really that important, but what I do want to do is come down here and add a new line and this is important, I'm going to do eval, and then a space, and then inside double quotes, a dollar sign and parenthesis, rbenv, space, init, space, dash, and close the parenthesis and close the double quotes. What that's going to say is tell rbenv to initialize. But it's going to run it as a command every time that I open up this window. So, once I'm done with that, I'm going to hit Control X, you'll see that down in the very bottom, it tells me that's how to exit out. It'll ask me if I want to save my changes and I'll type a Y for yes. It'll ask me where I want to save it, make sure that the name is correct, it's spelled correctly, .bash_profile and hit return. Now, in order to get things started off, you'll either close this window and open a new one, or I can type source.bash_profile and it will run it for the first time. So that should've initialized rbenv for us. Now we can check that by saying rbenv install, dash, dash, list. We get a long list of all the versions of Ruby that it knows about. There's a whole bunch of different ones there. The main ones are going to be right up here, that's just numbers. You don't need to worry about these specialized versions. And you can see right now as I'm recording us, 2.65 is the newest current version and there's a few preview versions at 2.7, because it's just about to come out. Anything you use is fine, don't worry about it, as long as it's newer than 2.5, you're fine. I'm going to use 2.65. So to install, I would just type rbenv and then space, install, and then 2.6.5. It'll take a few minutes for it to install. When it's finally done, you want to make sure that you type rbenv, space, rehash. That's a command you're always going to run every time you install a new version of Ruby. It's just going to make sure that all of the paths to the files are correct. Then we're going to type rbenv versions to see the versions that we have installed. And we can see that I have two versions installed, I have that System Ruby and I have 2.6.5. Notice that there's a star next to system. If I type ruby -v, it still tells me to pass the version of Ruby I'm using. In order to switch that star and to switch versions of Ruby, I have to use another command, rbenv, global, and then the name that I want, in this case it's 2.6.5. So that's going in and I'l switch it, I say ruby -v again, now I'm using Ruby 2.6.5, and rbenv versions. Also, it moves that star, which shows me that I'm using the new version as well. Now the old Apple version is still there, every time we update our operating system, Apple can update it, but we're using a version that we have installed. And as I said, you can install multiple versions, and you can even have different projects point to different ones. There are the global kind here, but there's another one that rbenv local, followed by, let's say system, and that will tell the current directory that I'm at or the current project that I'm in to use a certain version of Ruby, local 2.6.5, we tell it to use this newest version inside that project. So I can set them globally or on a project by project basis. But now, I have a good updated version of Ruby to use with my Ruby analyze project.

Contents