The other day I was working with another developer who primarily develops within the Microsoft space, such as .NET. My goal was to get him to try Rails, but the precursor to Rails is Ruby. Ruby isn’t hard to install at all, and some platforms, namely macOS and Linux, have excellent options available. This post is going to show you how to set up Ruby on your machine, regardless of your operating system.
We’ll get Windows out of the way first. It is a cinch to install Ruby on Windows. There are two easy options available. First is the Ruby Installer. You can just download their installer and be on your way with Ruby, easy peasy. As of this post, Ruby v2.5.0 is the latest version out.
Ok, you have Ruby installed on Windows. With Ruby Installer though, there’s a slight catch. It has to do with RubyGems. Gems (for short) are packages you can use. An example of one would be like the ruby_odata library that I wrote. This gem allows you to work with an OData server using Ruby instead of complex web calls. Another gem, called nokogiri allows you to navigate XML documents with ease. This is where the “catch” exists. This particular gem is written in C, not Ruby. Nokogiri isn’t the only C-based gem, there are others, and you’re bound to hit one when installing gems. To install these on your system, they need to be compiled. Windows doesn’t ship with build tools that can do this, so you need to install what’s known as the DevKit. It looks like if you installed Ruby 2.4 or greater, you can install the DevKit with a command bundled with the Ruby Installer.
Another option on Windows, that’s an all-inclusive installer (no need to install the DevKit separately) is the Rails Installer. As the name suggests, this will setup Rails for you as well, plus other tools like the DevKit. These installers are tied to specific versions of Ruby and Rails. As of this post, the newest Rails Installer is based on Ruby v2.3.3, not the latest version of Ruby (v2.5.0). Not the end of the world, but just be aware of your version.
If I had to choose on Windows, I’d go the Rails Installer route. I’ll talk about installing Rails in a different post, but if you want to use Rails out-of-the-box on Windows, you’re going to need things like SQLite. This and other tools are included with the Rails Installer, so it will get you going quickly.
MacOS has various options for installing Ruby, and in fact, most of these work on Linux too. First, let’s start with the Rails Installer. While I’d go with it for Windows, I wouldn’t choose the macOS versions. As of this post, the macOS options are pretty out of date. Besides, there are better options for installing Ruby on a Mac.
Let’s start with a precursor. If you’re on Mac, grab a copy of Xcode. It’s free in the Mac App Store. Why Xcode? Well, it comes with various command line tools that it will install the first time you run it. These tools make your Mac able to compile C-based gems. Now I know Xcode is large for just command line tools. Of course, there are other options. Another option that I just read is this:
“Apple now provides an official Command Line Tools for Xcode package that you can install without needing to install Xcode itself! You can install it with
xcode-select --installon Mavericks and Yosemite or download it from Apple’s developer site (free registration required) and search for “Command Line Tools” –OSX GCC Installer
Honestly, if you’re a software developer on macOS or OSX, you’re probably going to tinker with Mac or iOS development. If that’s the case, just grab Xcode. If not, try the command or download the tools from Apple’s Developer Site.
Now that we have the appropriate tools, we can install Ruby and even compile Ruby from source if needed. I’d recommend a Ruby Version Manager. There are two big players in this space, RVM or rbenv. Personally, I like RVM and haven’t tried rbenv at all. Rbenv can’t be installed alongside RVM, so it’s hard to test both without getting rid of the other. You’ll find developers who swear by both, but I’m going to go with RVM for this guide. But before I start, check out rbenv’s list of why you should choose it over RVM. Know that whatever one you pick, you’ll be able to switch between versions of Ruby with ease.
Follow my guide for using RVM below, the process is the same on both systems. I’d recommend that you review the Linux steps below as I dig into some of the prerequites of RVM a little deeper.
For Linux, we have most of the same choices as macOS, except for the Rails Installer (which again, I wouldn’t recommend on macOS due to its age (as of this post). On Linux, I’d recommend RVM again, but rbenv is available here as well. We’ll talk about installing RVM below, but here I will go over here is it’s requirements. Well, having just installed RVM on a Virtual Machine, I can tell you that RVM will prompt you for your password so it can install prerequisites for you. I’m assuming this requires sudo or root privileges. I had sudo rights on the VM, and I didn’t need to install the requirements manually. Pretty sweet, eh?
Maybe you don’t like that RVM does that for you automatically, or maybe you don’t have root permissions. That feature of RVM is called autolibs. You can disable these or pick and choose the scenarios you want RVM to handle. To do this during the install, before you install Ruby with RVM, type the command:
This will show you what RVM needs to be able to install and you can install these packages yourself or have someone with root permissions do it for you. Note that this is only for installing versions of Ruby. RVM’s requirements for installation are standard *nix tools, such as
With the prerequisites out of the way, let’s do this!
To install RVM, just follow the commands on the homepage. I’ll repeat them here, but know that the RVM site has the most up-to-date information. Ok, here we go, just copy and paste these two commands into Terminal window:
Read the instructions at the end of the install (the 2nd command), the easiest thing to do is just restart Terminal, or you’re able to load RVM manually just for your current session. Once that’s done, it’s time to install a version of Ruby.
Now, installing any version of Ruby is just a Terminal command away, for example, to install the latest (as of this post) 2.5.0, you can use the following command:
Now, if you want to use 2.5.0, you can issue a command to RVM:
Oh, what? Do you want that as your default version of Ruby? RVM has you covered:
Make sure you notice the two dashes before
That’s all there is to it. Now RVM comes with other tools that are available to us, but I’ll leave that for a future post.
There you have it; Ruby is now installed on your machine. Look for more posts in the future where we’ll explore installing Rails and friends on our machine. Yes, I did write about installing Rails eight long years ago, but that’s, obviously, a little out of date.
I hope you found this post helpful. If you have any problems installing Ruby, feel free to leave a comment below or contact us.