In order to get started with Rails, you must first install it on your computer. This tutorial will be focused on Mac OS, specifically in version 10.11 El Capitan and 10.12 Sierra. I will be sharing a blog post for Windows, as well as Linux.
Mac OS comes with Ruby pre-installed out-of-the-box (OOTB), but you’ll likely want to update it to a more recent version. We will be using Homebrew for this.
You can validate the install was successful by running:
git config --list
output: git config –global color.ui true
git config –global user.name “Jon Doe”
git config –global user.email “firstname.lastname@example.org”
ssh-keygen -t rsa -C “email@example.com“
A package manager for Mac OS which makes it easier to install files on your computer.
To install, copy the following command and paste it into your terminal:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
That will install Homebrew and its corresponding docs / dependencies.
You can confirm that it installed correctly by running:
If you already had Homebrew installed, listen up:
I had to run the following commands to get it to work.
brew link --overwrite autoconf brew link --overwrite pkg-config brew link --overwrite rbenv brew link --overwrite ruby-build
The next thing we need to set up is rbenv.
A Ruby version management tool built by Sam Stephenson.
brew install rbenv ruby-build
Add rbenv to our .bash_profile so that it is available to us whenever we open a Terminal.
echo 'if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi' >> ~/.bash_profile source ~/.bash_profile
The next step will be to enable the xcode command line developer tools.
Having done so, we proceed to finding our new Ruby build.
To list available Ruby versions:
rbenv install --list
Select Ruby version (opt for the latest stable build, for me it is 2.3.0, yours may vary)
We will now run two commands:
rbenv install 2.3.0 rbnev global 2.3.0
- Sets a local application-specific Ruby version by writing the version name to a .ruby-version file in the current directory. This version overrides the global version.
Sets the global version of Ruby to be used in all shells by writing the version name to the ~/.rbenv/version file.
Now, validate the install:
If everything went correctly, it should output the new Ruby version.
You can also test this by running:
To add Rails (or any other valid gem, as a matter of fact), to your RubyGems use the command gem install.
gem install rails -v 5.0.2
In order to be able to execute it from the command line interface (CLI) or terminal, you will need to tell rbenv to see it:
This command installs shims for all Ruby executables known to rbenv (i.e., ~/.rbenv/versions/bin/).
Another useful command is rbenv which; it displays the full path to the executable that rbenv will invoke when you run the given command.
rbenv which rails
You can validate the installation using:
output: Rails 5.0.2
Rails comes with sqlite3 support built in, which is great for development and testing, but not my first choice for a production database (DB).
My relational database of choice is MySQL.
You can install it using Homebrew:
brew install mysql
This will setup a MySQL database without a root password. To secure it run:
The following step is optional
To launch MySQL on startup:
ln -sfv /usr/local/opt/mysql/*plist ~/Library/LaunchAgents
To launch now:
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
By default the database user is <strong>root</strong> with no password. You can test the connection by running:
mysql --user=root mysql
If you have assigned a password to the root account, you must also supply a –password or -p option, e.g.
mysql --user=root mysql --password=foobar
After you have tested your connection, the next step would be to scaffold a sample project:
cd ~/Sites ruby rails new myrailsapp -d mysql cd myrailsapp rake db:create rails server
- Navigate into your work directory
- Choose a database engine:
- To use sqlite3 (default): `ruby rails new myrailsapp`
- To use MySQL (recommended): `ruby rails new myrailsapp -d mysql`
- Navigate into the project directory
- Use rake to spin up a database
- Serve locally
You can now access your app via http://localhost:3000
And there you have it, you have successfully install Rails, and created your first project.
To learn more about rails, make sure to check out their GitHub repository.
If you enjoyed this blog post or found it helpful in any way, make sure to follow me on Twitter to find out when a new one is available.