Join Kevin Skoglund for an in-depth discussion in this video Installing Ruby on a Mac, part of Ruby Essential Training.
In this movie, I am going to help Mac users get everything installed that they need to be able to program in the Ruby language. I am going to start by first giving an overview and then I'll walk you through it step-by-step. The first thing I want to do is point you to the primary resource for the Ruby language and that's the Ruby language website, ruby-lang.org. So if you go to that website, it will always have the latest information about the current version of Ruby and even some installation instructions and some links to download it. So if the things that I tell you about installation change in the future, let's say a new operating system version comes out or suddenly some kind of bug comes up, so that it's not working anymore, your first resource that you want to go to is that ruby-lang site and see if you can find the problem there, because they'll probably let you know, "look, this is what you need to do if you're using this operating system" and so on.
We'll take a look at that in a moment. The first of the pieces that we know we're going to need to be able to program in the Ruby language is the Ruby interpreter. It's going to interpret Ruby for us. If you're using Mac OS X, you probably already have a version of Ruby pre-installed. Now this may not be the correct version and you may need to update it. Let's take a look. So if you have 10.1, you may have problems running Ruby. Some people have had success, some people have had trouble. Hopefully, that's an operating system that's now seven or eight- years-old and you've upgraded to something else. 10.2 and 10.3, you'll want to either install or upgrade Ruby by following the instructions on that Ruby language site.
And if you're on 10.4, which is Tiger, or 10.5, Leopard, which is where I am guessing most of you are, you'll have a version of Ruby that's going to work perfectly fine for the purposes of our tutorial. You could go ahead and upgrade to a newer version. In fact, there is a newer version than 1.8.6 out and there may be other ones after this training is published, they come out. You can go ahead and use those newer ones if you want, but you'll want to make sure that you have something at least 1.8.2 before proceeding and that's what ships with those and no installation will be needed. So you'll be all set to go. That makes it nice and easy for you.
The other two pieces that we're going to need are the text editor and the command line. The text editor is going to let us write files in Ruby code. The command line is going to let us run those files and also interact with our Ruby interpreter in an interactive way. We'll see how to do that. Any text editor will really work. You don't want to use something like Microsoft Word or something; you want to use just a plain text editor that doesn't insert lots of formatting, or anything like that, just plain text, and the best ones are ones that are going to do code coloring for you. That is they're going to give color to the syntax on the page, so that you can quickly look at your code and tell what's what because things are of different colors.
It's a nice feature that most text editors have. The text editor that I always recommend is TextMate. I think it's a fantastic one and I think you can't go wrong with it. macromates.com is the company that makes it. You can just go to their website. It's not that expensive to buy, but you also can download a free trial, if you want to try it out and it does code coloring and there is lots of other nice features that I think you'll really come to appreciate over time. So do yourself a favor and check that out. And for the command line, we'll be using Apple's program called Terminal and that is inside the Applications folder, inside Utilities, you'll find the program called Terminal. We can run that to directly interact on the command line with the Unix operating system that's running Mac OS X.
So let's take a look now. Here I am on the Ruby Language website. You'll notice that there is a download link here and there is also one here. Those take me to the same place and that lets me download Ruby. You'll see that it gives me the Ruby Source Code if I want to download the full source. Ruby 1.9, it actually offers to give me in that format, or I can scroll on down past Windows and Linux, until I get to Ruby On OS X and it basically tells you the same information that I just told you, and gives you some more helpful links if you want to upgrade and I wanted to point out down here, probably the best things of all for Tiger and Leopard, these articles down here will take you to the steps of installing Ruby and also Rails.
Now we only want to follow the first couple of steps to get Ruby installed. It goes on and installs MySQL and Rails and everything else. We're just going to want to focus on Ruby but those can all be helpful for you, if you decide that you want to upgrade. Now let's take a look at our command line. If I go to Applications to my Applications folder, if I go into Utilities, inside my Utilities, if I scroll down, you'll see I have Terminal and this will open up our command line that will allow us to interact directly with the Unix operating system. Now I want to start out by just saying ruby and then dash v and that should work for you.
If you have Ruby installed, it should come up and tell you which version, just by typing ruby -v. If you didn't get that, then you don't have Ruby installed or else there is something that's gone wrong and you'll need to troubleshoot it. But on Tiger and Leopard, you should get back either Ruby 1.8.2 or something later than that by just issuing that command. We can also type in which ruby and it will actually tells us where it's located and that's also useful to know. It's inside usr/bin and that's where the program has actually been put. It's inside that folder.
So that's the version of Ruby that we are running and that's where it's located. So we know that we have Ruby. We know that we now have a command line. The last thing is just go to that MacroMates website that I told you about, macromates.com. Here it is for TextMate. You'll see we have a free 30-day trial. You can just download. It's a simple installer; you can go ahead and do that for yourself. I've already installed it and once I downloaded it and installed it, then I just simply drag a copy of it to my dock here, so that I have it nice and accessible. I have also got Terminal that I've just dragged to my dock so that that's accessible as well, and I can just switch between those to bring them up.
That's all we need to be able to use Ruby. It's that easy. Luckily, all this is a pre-installed with the Mac, so there is really just not that much to do. Now, we are ready to start using Ruby.
- Using Ruby in the Interactive Ruby Shell and in standalone scripts
- Learning to write custom code blocks to find, merge, and sort
- Using modules for namespacing or as mix-ins
- Reading from and writing to files
- Creating a full Ruby project from start to finish