Setup
Exercises
Objective
Welcome to your first exercise! We're delighted to have you along for the course.
Before we can start creating Rails apps, we need to get Ruby and Rails installed on your computer. Depending on your operating system, you may already have a version of Rails installed. But for this course you'll need to be running Rails 7.0.8.
So just follow the steps below for your favorite operating system—Mac, Windows, or Linux— and we'll be on our way!
Mac OS X
The best way we've found to install Ruby and Rails on a Mac is using rbenv. It's a command-line tool that makes it easy to install and manage Ruby versions. We'll be installing Ruby 3.1.4 and Rails 7.0.8.
If you already have Ruby 3 installed, you can jump straight to installing Rails.
Note that Mac OS X ships with a version of Ruby. However, it's best not to mess around with the system-installed Ruby as it's intended to be used by the operating system and apps installed by Apple. So we'll use rbenv to install a separate user-level Ruby environment, rather than changing the system-installed Ruby.
-
Start by finding the Terminal application (it's under the Applications -> Utilities directory) and dragging it onto your dock. You'll end up using Terminal a lot as a Rails developer, so it's good to have it handy. Then open a new Terminal session. You should see a new window with a cursor and a prompt that looks something like this:
enoch:~ mike$
If this is the first time you've seen this side of a Mac, it may seem rather intimidating. Don't let it throw you. It's simply a way to interact with your computer by entering commands. The default prompt includes the computer name (enoch in my case), the current working directory (tilde represents your home directory), the current user name (mike), and a trailing $ which is the prompt for input.
We'll use the command prompt throughout the course to run commands and perform tasks on our Rails app. In fact, here comes our first command...
-
To install rbenv, first we need to install Homebrew which makes it easy to install and compile software packages from source.
To install Homebrew, copy the following command and paste it after your Terminal prompt:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
If you're asked about installing Apple's Command Line Tools for Xcode, go ahead and answer "yes".
-
Now we can use Homebrew to install rbenv. To do that, run the following command in your Terminal session:
brew install rbenv
-
Next, set up rbenv in your Terminal's shell by running the following command and following the printed instructions:
rbenv init
-
Then close your Terminal session and open a fresh new Terminal session so that the changes take effect.
-
Now that we have rbenv installed and the Terminal's shell set up properly, we're ready to install Ruby 3.1.4. To do that, type
rbenv install 3.1.4
This will download, compile, and install Ruby into a directory managed by rbenv. (If you're curious, it's under the
~/.rbenv
directory.)Installing Ruby may take a while, so feel free to grab a refreshing beverage or a tasty snack. Mmm...
-
When installation is complete, set Ruby 3.1.4 as the global Ruby version by typing:
rbenv global 3.1.4
This sets Ruby 3.1.4 as the default version to be used whenever you open any new Terminal sessions.
-
Then verify that Ruby 3.1.4 is the current Ruby version by typing:
ruby -v
You should see something like the following, though your exact patch number, date, and revision may be slightly different:
ruby 3.1.4p223 (2023-03-30 revision 957bb7cb81) [arm64-darwin22]
-
With Ruby installed, now we're ready to install Rails! Rails is distributed via RubyGems: the standard Ruby package manager. When you installed Ruby, the RubyGems system came along for the ride. With RubyGems already installed, it's easy to install Rails and its dependencies.
Install Rails 7.0.8 by typing
gem install rails -v 7.0.8 --no-document
Then sit back and relax as RubyGems downloads all the Rails-related gems. After a minute or so, you should end up with a couple dozen gems installed.
-
Installing Rails gives us a
rails
command, but to use it we first need to inform rbenv about that command. To do that, typerbenv rehash
-
Finally, verify that Rails 7.0.8 was successfully installed by typing
rails -v
You should see:
Rails 7.0.8
-
Finally, you'll need to install Git (a distributed version control system) if you don't already have a version installed. You can check if you have Git installed by
git --version
We won't use Git until later in the course, but Rails will auto-generate some handy Git-related files if we have Git installed from the get-go.
Excellent! Now everything you need is installed.
Choose A Code Editor
Throughout the course we'll be creating a Rails application by writing Ruby code in one or more files. You'll need a code editor to create and edit these files. The editor doesn't need to have a lot of features. In fact, a basic code editor that has Ruby syntax highlighting and a file/directory browser works best.
We'll make the decision easy for you. Unless you already have a code editor that you're very comfortable using, we recommend using Visual Studio Code as we do in the videos. VS Code runs equally well on Mac, Windows, or Linux and it's completely free! We also recommend installing the Simple Ruby ERB extension which provides both Ruby and ERB syntax highlighting.
You're now ready to start creating Rails apps!
Windows 11
The best way we've found to install Ruby and Rails on Windows is using RubyInstaller. It's a self-contained Windows-based installer that includes a Ruby language execution environment and the Windows DevKit.
-
Navigate to the RubyInstaller downloads page and click the Ruby+Devkit 3.1.4-1 (x64) executable installer. Save the file to your Desktop, for example, and then run the file once it has finished downloading. You may need to use Windows Explorer to navigate to where you saved the executable file and double-click it to start the installation process.
-
After accepting the license agreement, the installer will ask you to select components to install. Make sure the checkmarks labeled "Ruby RI and HTML documentation" and "MSYS2 development toolchain" are checked. MSYS2 is required to install Ruby gems with C extensions, and some of the gems used by Rails do indeed have C extensions. Then click "Next" and Ruby will be installed in the
C:\Ruby31-x64
directory as promised.Wait! Do not click "Finish" on the last installer screen until you've made sure the checkmark labeled "Run 'ridk install' to set up MSYS2 and development toolchain" is checked. With that checkbox checked, when you click "Finish", a new command window will open asking you which components of MSYS2 you want to install. Choose "1" which is the base MSYS2 installation. It will proceed to install a bunch of stuff, and then return back to the prompt asking which components you want to install. You should see the message "MSYS2 seems to be properly installed". Go ahead and close that command window.
-
Next, open a new command prompt by selecting the Start menu, typing
cmd
into the search box, and pressingEnter
. You should see a new window with a blinking cursor and a command prompt that looks something like this:C:\Users\mike>
If this is the first time you've seen this command prompt it may seem rather intimidating, but don't let it throw you. It's simply a way to interact with your computer by entering commands. We'll use the command prompt throughout the course to run Ruby and use some related command-line tools. In fact, here comes our first command...
-
Verify that Ruby 3.1.4 was successfully installed by typing the following at the command prompt:
ruby -v
You should see something like this, though your exact patch number, date, and revision may be slightly different:
ruby 3.1.4p223 (2023-03-30 revision 957bb7cb81) [x64-mingw-ucrt]
If instead you see "Command not found", then you either need to open a new command prompt and try again, or check that your
PATH
environment variable includes theC:\Ruby31-x64\bin
directory. -
Next, install Rails 7.0.8 by typing
gem install rails -v 7.0.8 --no-document
Then sit back and relax as all the Rails-related gems are downloaded. After a few minutes, you should end up with 40 or so gems installed.
-
And verify that Rails 7.0.8 was successfully installed by typing
rails -v
You should see:
Rails 7.0.8
-
Finally, you'll need to install Git (a distributed version control system) if you don't already have a version installed. You can check if you have Git installed by
git --version
We won't use Git until later in the course, but Rails will auto-generate some handy Git-related files if we have Git installed from the get-go.
Excellent! Now everything you need is installed.
Choose A Code Editor
Throughout the course we'll be creating a Rails application by writing Ruby code in one or more files. You'll need a code editor to create and edit these files. The editor doesn't need to have a lot of features. In fact, a basic code editor that has Ruby syntax highlighting and a file/directory browser works best.
We'll make the decision easy for you. Unless you already have a code editor that you're very comfortable using, we recommend using Visual Studio Code as we do in the videos. VS Code runs equally well on Mac, Windows, or Linux and it's completely free! We also recommend installing the Simple Ruby ERB extension which provides both Ruby and ERB syntax highlighting.
You're now ready to start creating Rails apps!
Linux
Folks who run Linux tend to already be comfortable with the command line, installing and building software, and configuring things just the way they like. We're not so bold as to tell you exactly how to tweak your Linux install. The important thing is that you get Ruby 3.1.4, Rails 7.0.8, and SQLite 3 installed to match the versions we'll use throughout this course.
Unless you have a strong preference, we recommend installing Ruby on Linux using rbenv. It's a command-line tool that makes it easy to install and manage Ruby versions.
-
Start by updating
apt-get
and installing the dependencies required for rbenv and Ruby:sudo apt-get update sudo apt-get install git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev software-properties-common libffi-dev nodejs npm
-
Then install rbenv:
sudo apt install rbenv
If you prefer a manual approach, follow the steps in the Basic GitHub Checkout section of the official rbenv installation documentation.
-
Once you have rbenv installed, it's then easy to get Ruby and and Rails installed by picking up with step 6 in the Mac OS X instructions above.
If you need additional help with other shells, more examples can be found in the Basic GitHub Checkout section of the official rbenv installation documentation.
Choose A Code Editor
Throughout the course we'll be creating a Rails application by writing Ruby code in one or more files. You'll need a code editor to create and edit these files. The editor doesn't need to have a lot of features. In fact, a basic code editor that has Ruby syntax highlighting and a file/directory browser works best.
We'll make the decision easy for you. Unless you already have a code editor that you're very comfortable using, we recommend using Visual Studio Code as we do in the videos. VS Code runs equally well on Mac, Windows, or Linux and it's completely free! We also recommend installing the Simple Ruby ERB extension which provides both Ruby and ERB syntax highlighting.
You're now ready to start creating Rails apps!