Active Rails

Generated from 8ad8287e5 on 2022-07-04

Rails Installation Guide - Mac OS X

OS X is the second easiest of the three operating systems to install Ruby onto. The reason it’s in second place is because it doesn’t come with a package manager like most flavours of Linux do. Instead, you must elect to install a tool called Homebrew to manage these packages.

This guide has been verified with Mac OS X 10.15, but will probably work on earlier versions of OS X 10.x.

Homebrew

To start with, we are going to install Homebrew, which bills itself as "The missing package manager for OS X". The features that Homebrew provides along with its ease of use means that it has quickly gained status as the tool for managing packages on OS X. Follow the installation instructions at the bottom of http://brew.sh to install Homebrew. The instructions say to run this command in Terminal:

$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

Go ahead and do that now.

This command will pull down the Homebrew installation script and run it using the version of Ruby which comes standard with every modern OS X install. Follow the prompts and allow the XCode tools to install. Once the XCode tools are installed, press any key in the Terminal to finish setting up Homebrew.

After we’ve installed Homebrew, we’ll need to install two packages using it:

brew install openssl readline

These packages will be used when we setup (and use) Ruby a little later on.

asdf

Next, we’ll need to install a tool that is used to manage the installation of different programming languages. We’re going to use this tool to install both the Ruby and Node.js programming languages.

Installation

To install asdf, you can go to https://asdf-vm.com/#/core-manage-asdf-vm and copy and paste the command at the top of the page. At the current time, that command is:

$ git clone https://github.com/asdf-vm/asdf.git ~/.asdf --branch v0.7.8

This will add some shell scripts to the ~/.asdf directory. We can add these shell scripts to our ~/.bash_profile file by running these commands:

$ echo ". $HOME/.asdf/asdf.sh" >> ~/.bash_profile
$ echo ". $HOME/.asdf/completions/asdf.bash" >> ~/.bash_profile

This will make it so that we have access to the asdf command in our terminal.

We will need to either restart our terminal, or reload this ~/.bash_profile file to load in these changes:

$ . ~/.bash_profile

To verify that this has been installed correctly, run:

asdf --version

You should see this, or similar:

v0.7.8-4a3e3d6

Ruby

To install Ruby using asdf, we first need to add the ruby plugin to asdf. Do that with this command:

$ asdf plugin-add ruby

Next, we can install Ruby by running:

$ asdf install ruby 2.7.1

asdf and its Ruby plugin knows where to get the source code for Ruby 2.7.1, and also what dependencies are needed to compile it.

Once it’s done all of that, it will start compiling Ruby, and you’ll see lots and lots of random output as checks a lot of things and then compiles all of the necessary files. There doesn’t appear to be an in-built way to silence this output, but you can just let it do its thing - go get a can of Diet Coke or something, it will take a few minutes.

When it’s done, you’ll see a nice happy:

Installed ruby-2.7.1 to /home/youruser/.asdf/installs/ruby/2.7.1

So you now have a ruby installed. How do you use it? The folder it installed to isn’t part of your path, so calling ruby will have no effect.

We will need to set a current version of Ruby using asdf:

$ asdf gobal ruby 2.7.1

Now when we run ruby -v, we should see the current version of Ruby:

ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-linux]

Node

Next up, we will need to install Node.js. This is used by the Webpacker gem, which is a dependency of Rails. The webpacker gem uses a JavaScript package called Webpack to compile the JavaScript assets for your application.

To install Node, we’ll also use asdf. We first have to install the Node plugin:

$ asdf plugin-add nodejs

And we have to run this command so that asdf can verify Node installation files:

$ bash ~/.asdf/plugins/nodejs/bin/import-release-team-keyring

Once those two steps are done, we can install Node:

$ asdf install nodejs 12.16.2

Then we will need to configure asdf to use this version of Node:

$ asdf global nodejs 12.16.2

To verify that this has been installed correctly, run node -v. You should see this:

v12.16.2
Yarn

The next thing we need to install for Node is a package manager called Yarn. This is used by Rails to install files for Webpacker. We can install Yarn with this command:

npm install -g yarn

Verify this has been installed by running yarn -v. As of this time of writing, Yarn’s version is 1.22.4. If you see any version number, then that is fine.

Rails

So now you have a working Ruby. What about Rails?

Ruby comes with its own package manager called RubyGems, and this is what we can use to install Rails. Gems are just little bundled-up packages of Ruby code, and Rails is just a gem.

$ gem install rails -v "~> 6.0.0" --no-document

This will install the latest from the 6.0.x branch of Rails and all of its dependencies. Whatever version this installs will be fine for this book. It’ll take a while, as it figures out the dependencies, installs the gems, and then parses and installs documentation. (You can skip the documentation install by running the command with --no-document, eg. gem install rails --no-document. But hey, you might want it one day!)

When it’s done, verify the installation by running rails -v. The output will look something like this:

Rails 6.0.2.1

Hooray!

Starting a new Rails app

Once all that complicated setup is done, starting a new Rails app is trivial. Simply enter in your terminal:

$ rails new my_awesome_app

Which will create a new app using SQLite, in the my_awesome_app directory of your current folder. This will also install Once that’s complete, you can start the Rails server:

$ cd my_awesome_app
$ rails server

Once Puma starts and tells you it has loaded on port 3000, you can open up a browser and visit http://localhost:3000.

You should see "Yay! You’re on Rails!"

Footnotes

© Ryan Bigg, Rebecca Le, Kieran Andrews & Robin Klaus