Installing Ruby On Rails For Mac



May 16, 2020 Install Ruby on Rails 6.0 on macOS 10.15 Catalina. Up-to-date and detailed instructions, plus troubleshooting, for the Rails newest release. How to set up and install Rails 6.0, the newest version of Rails, on macOS 10.15 Catalina. The most complete and up-to-date installation guide for Ruby on Rails on macOS and a favorite of Rails developers. See full list on ruby-lang.org. From Start to Finish: Install Ruby on Rails on Mac, Deploy on Heroku. Code rails ruby. Tyler (284) Total time: 20 minutes.

Introduction

Ruby on Rails is a popular application stack for developers looking to create sites and web apps. The Ruby programming language, combined with the Rails development framework, makes app development quick and efficient.

One way to install Ruby and Rails is with the command-line tool rbenv. Using rbenv will provide you with a well-controlled and robust environment for developing your Ruby on Rails applications, allowing you to easily switch the version of Ruby for your entire team when needed.

rbenv provides support for specifying application-specific versions of Ruby, lets you change the global Ruby for each user, and allows you to use an environment variable to override the Ruby version.

In this tutorial, you will use rbenv to install and set up Ruby on Rails on your local macOS machine.

Prerequisites

To follow this tutorial, you will need:

  • One computer or virtual machine with macOS installed, with administrative access to that machine and an internet connection. This tutorial has been tested on macOS 10.14 Mojave.
  • Node.js installed on your macOS machine, as explained in How to Install Node.js and Create a Local Development Environment on macOS. A few Rails features, such as the Asset Pipeline, depend on a JavaScript Runtime. Node.js provides this functionality.

Step 1 — Installing rbenv

In this step, you will install rbenv and make sure that it starts automatically at boot. To do this on macOS, this tutorial will use the package manager Homebrew.

To download the rbenv package with Homebrew, run the following command:

This will install rbenv and the ruby-build plugin. This plugin adds therbenv install command, which streamlines the installation process for new versions of Ruby.

Next, you’ll add the command eval '$(rbenv init -)' to your ~/.bash_profile file to make rbenv load automatically when you open up the Terminal. To do this, open your .bash_profile in your favorite text editor:

Add the following line to the file:

Save and quit the file.

Next, apply the changes you made to your ~/.bash_profile file to your current shell session:

To verify that rbenv is set up properly, use the type command, which will display more information about the rbenv command:

Your terminal window will display the following:

At this point, you have both rbenv and ruby-build installed on your machine. This will allow you to install Ruby from the command line in the next step.

Step 2 — Installing Ruby

With the ruby-build plugin now installed, you can install any version of Ruby you may need through a single command. In this step, you will choose a version of Ruby, install it on your machine, and then verify the installation.

First, use the -l flag to list all the available versions of Ruby:

The output of that command will be a long list of versions that you can choose to install.

For this tutorial, install Ruby 2.6.3:

Installing Ruby can be a lengthy process, so be prepared for the installation to take some time to complete.

Once it’s done installing, set it as your default version of Ruby with the global sub-command:

Verify that Ruby was properly installed by checking its version number:

Your output will look something like this:

To install and use a different version of Ruby, run the rbenv commands with a different version number, such as rbenv install 2.3.0 and rbenv global 2.3.0.

You now have one version of Ruby installed and have set your default Ruby version. Next, you will set yourself up to work with Ruby packages and libraries, or gems, which will then allow you to install Rails.

Step 3 — Working with Gems

Rails

Gems are packages of Ruby libraries and programs that can be distributed throughout the Ruby ecosystem. You use the gem command to manage these gems. In this step, you will configure the gem command to prepare for the Rails installation.

When you install a gem, the installation process generates local documentation. This can add a significant amount of time to each gem’s installation process, so turn off local documentation generation by creating a file called ~/.gemrc which contains a configuration setting to turn off this feature:

With that done, use the gem command to install Bundler, a tool that manages gem dependencies for projects. This is needed for Rails to work correctly:

You’ll see output like this:

You can use the gem env command to learn more about the environment and configuration of gems. To see the location of installed gems, use the home argument, like this:

You’ll see output similar to this:

Now that you have set up and explored your gem workflow, you are free to install Rails.

Install ruby on rails mac rvm

Step 4 — Installing Rails

To install Rails, use the gem install command along with the -v flag to specify the version. For this tutorial, we will use version 5.2.3:

The gem command installs the gem you specify, as well as every dependency. Rails is a complex web development framework and has many dependencies, so the process will take some time to complete. Eventually you’ll see a message stating that Rails is installed, along with its dependencies:

Note: If you would like to install a different version of Rails, you can list the valid versions of Rails by doing a search, which will output a long list of possible versions. We can then install a specific version, such as 4.2.7:

If you would like to install the latest version of Rails, run the command without a version specified:

rbenv works by creating a directory of shims, or libraries that intercept calls and change or redirect them. In this case, shims point Ruby commands to the files used by the Ruby version that’s currently enabled. Through the rehash sub-command, rbenv maintains shims in that directory to match every Ruby command across every installed version of Ruby on your server. Whenever you install a new version of Ruby or a gem that provides commands, such as Rails, you should use rehash.

To rehash the directory of shims, run the following command:

Verify your installation of Rails by printing its version with this command:

You will see the version of Rails that was installed:

With Rails successfully installed, you can begin testing your Ruby on Rails installation and start to develop web applications. In the next step, you will learn how to update and uninstall rbenv and Ruby.

Step 5 — Updating and Uninstalling rbenv and Ruby

When maintaining projects, it is useful to know how to update and uninstall when the need arises. In this step, you will upgrade rbenv, then uninstall Ruby and rbenv from your machine.

You can upgrade rbenv and ruby-build using Homebrew by running the following command:

If rbenv or ruby-build need to be updated, Homebrew will do it for you automatically. If your set up is already up to date, you will get output similar to the following:

This will ensure that we are using the most up-to-date version of rbenv available.

As you download additional versions of Ruby, you may accumulate more versions than you would like in your ~/.rbenv/versions directory. Using the ruby-build plugin’s uninstall subcommand, you can remove these previous versions.

For example, run the following to uninstall Ruby version 2.1.3:

With the rbenv uninstall command you can clean up old versions of Ruby so that you do not have more installed than you are currently using.

If you’ve decided you no longer want to use rbenv, you can remove it from your system.

To do this, first open your ~/.bash_profile file in your editor:

Find and remove the following line from the file to stop rbenv from starting when you open the Terminal:

~/.bash_profile

Once you have deleted this line, save the file and exit the editor.

Run the following command to apply the changes to your shell:

Ruby

Next, remove rbenv and all installed Ruby versions with this command:

Finally, remove the rbenv package itself with Homebrew:

Check the rbenv version to make sure that it has been uninstalled:

You will get the following output:

This means that you have successfully removed rbenv from your machine.

Conclusion

In this tutorial you installed Ruby on Rails with rbenv on macOS. From here, you can learn more about coding in Ruby with our How To Code in Ruby series. You can also explore how to use Ruby on Rails with PostgreSQL rather than its default sqlite3 database, which provides more scalability, centralization, and stability for your applications.

(Related article: How to Install Ruby on Rails for Mac OSX Mavericks)

For this post we are based on a clean “Ubuntu 14.04 LTS” installation, nevertheless I have installed these tools in previous versions with the same steps. And similar steps should work in other Linux ditros.

For day to day Ruby on Rails development work we need several tools to be porperly installed and running:

  • Git: Right now this is the standard Version Control System in the industry. Even more!! I work with git in all my client projects since a few years ago.
  • Rbenv (and ruby): lightweight Ruby Version Manager to isolate Ruby versions and project dependencies.
  • Rails: Our favourite framework to build Web apps.
  • Sublime Text Editor: lightweight modern editor.
  • PostgreSQL: Probably the best Database in the open source market (in fact my favourite)

For Ubuntu my first recommendation, before to make a big installation, is updating system packages:

Step 1. Installing Git

Git is the standard VCS for the Ruby community, and probably for the IT industry at this moment. Also it is pretty simple to install at Ubuntu:

Git will need your email and name at least to identify your commits, but I have added some other configurations interesting for my day-to-day work. Execute each line in prompt:

Probably you will use Github to deliver code to your clients, and it will need to identify you through your ssh-keys. You can follow this guide in order to create ssh keys (if you do not have them yet) and configure your account.

This process is similar with other Git providers like Bitbucket or your Company owned Git servers.

Step 2. Dynamic prompt with Git and ANSI colors

Tobias Sjösten wrote a pretty useful article explaining this.

This hack will:

  • Put our current branch in prompt
  • Change color if there are files to commit

This gives you a quick status of your changes in project in a quick eye-shot. You just have to add the following code to ~/.bash_profile file:

You should restart terminal for these changes to take effect.

Step 3. Install Ruby with RBENV

RVM and RBENV are the most used Ruby Version Managers among developers. Rbenv is lighter and solves the dependency isolation problem working with Bundle (the default dependency management gem for Rails).

RBENV give us several advantages:

  • The obvious ‘apt-get install ruby’ is outdated, and you probably want to work with latest ruby versions.
  • Allow different versions of ruby in the same machine, which will be useful when we write code in different projects.
  • Dependency isolation working with bundle.

You need Git that was installed previously to install RBENV.

With commands above you have installed Rbenv and the ruby versions builder, and give access to them directly from command prompt. Please, restart your terminal to load Rbenv.

The only disadvantage of BINSTUBS is that you have to remember to run the following command

Now, you can install every ruby version with a command:

Step 4. Your first Rails App

First you need to install Rails gem:

Create your new app:

Go to Gemfile and uncomment this line:

The gem “Therubyracer” is Google V8 engine wrapped in a gem, in order to have the capability of evaluating JavaScript code from Ruby. Pretty interesting in Rails templates and assets.

After that re-install gems:

And start it in your browser at http://localhost:3000

Step 5. Set Up Sublime Text as Code Editor

There are several code editors in the market to edit ruby code: SublimeText, RubyMine, Aptanta,…. Or Opensource like vim, gedit or emacs. Sublime Text is an excellent choice if you want a lightweight editor with great features and a modern user interface.

Download and unzip last version from the web page. In order to give access from command prompt:

Now you can open your Rails app by typing:

The convention for Ruby programs is to use two spaces as indentation. You can follow Sublime Text 2 => Preferences => Settings - User and add these lines.

Language

Step 6. PostgreSQL installation

This is a pretty good Database and maybe this will be your Production relational database. So worth it to take a look at how to integrate it with rails:

You can add the gem to your Gemfile:

And run bundle again: bundle install, to make PG gem installation.

After that your Database config file should be like:

Extra Ball. Set up RubyMine

Here at Tealeaf Academy we recommend Sublime for our students, because is lighter and easy to use. But if you need a more complete editor: with debugger, Git or Ticketing System integration, plugins … . RubyMine is a great choice.

You need a Java Runtime installation before:

Download and install:

They allow you a 30 day trial version, after that you should purchase a license.

Then you can open every Rails application within the project folder with the command:

Install Ruby Mac

Conclusion

Install Ruby On Rails Mac

Well, this is just the beginning. Here we just installed our Dev environment with a bunch of really useful tools, which you will use in your day-to-day work.

Install Ruby On Rails Mac Mojave

If you want a structured, instructor led program to level up to a professional level Ruby on Rails developer, check out our curriculum!