A guide to setting up a Ruby on Rails development environment
Overview
This will take about 30 minutes.
We will be setting up a Ruby on Rails development environment on Mac OS X 10.10 Yosemite.
Older versions of OS X are mostly compatible so follow along as far as you can and then Google search for any problems you run into. There are plenty of people who have documented solutions for them.
Installing Homebrew
First, we need to install Homebrew. Homebrew allows us to install and compile software packages easily from source.
Homebrew comes with a very simple install script. When it asks you to install XCode CommandLine Tools, say yes.
Open Terminal and run the following command:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Installing Ruby
Choose the version of Ruby you want to install:
Now that we have Homebrew installed, we can use it to install Ruby.
We're going to use rbenv to install and manage our Ruby versions.
To do this, run the following commands in your Terminal:
brew install rbenv ruby-build
# Add rbenv to bash so that it loads every time you open a terminal
echo 'if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi' >> ~/.bash_profile
source ~/.bash_profile
# Install Ruby
rbenv install 2.2.2
rbenv global 2.2.2
ruby -v
Configuring Git
We'll be using Git for our version control system so we're going to set it up to match our Githubaccount. If you don't already have a Github account, make sure to register. It will come in handy for the future.
Replace the example name and email address in the following steps with the ones you used for your Github account.
git config --global color.ui true
git config --global user.name "YOUR NAME"
git config --global user.email "YOUR@EMAIL.com"
ssh-keygen -t rsa -C "YOUR@EMAIL.com"
The next step is to take the newly generated SSH key and add it to your Github account. You want to copy and paste the output of the following command and paste it here.
cat ~/.ssh/id_rsa.pub
Once you've done this, you can check and see if it worked:
ssh -T git@github.com
You should get a message like this:
Hi excid3! You've successfully authenticated, but GitHub does not provide shell access.
Installing Rails
Choose the version of Rails you want to install:
Installing Rails is as simple as running the following command in your Terminal:
gem install rails -v 4.2.1
Rails is now installed, but in order for us to use the rails
executable, we need to tell rbenv
to see it:
rbenv rehash
And now we can verify Rails is installed:
rails -v
# Rails 4.2.1
Setting Up A Database
Rails ships with sqlite3 as the default database. Chances are you won't want to use it because it's stored as a simple file on disk. You'll probably want something more robust like MySQL or PostgreSQL.
There is a lot of documentation on both, so you can just pick one that seems like you'll be more comfortable with. If you're coming from PHP, you may already be familiar with MySQL. If you're new to databases, I'd suggest skipping to setting up PostgreSQL.
MySQL
You can install MySQL server and client from Homebrew:
brew install mysql
Once this command is finished, it gives you a couple commands to run. Follow the instructions and run them:
# To have launchd start mysql at login:
ln -sfv /usr/local/opt/mysql/*plist ~/Library/LaunchAgents
# Then to load mysql now:
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
By default the mysql user is root
with no password.
When you're finished, you can skip to the Final Steps.
PostgreSQL
You can install PostgreSQL server and client from Homebrew:
brew install postgresql
Once this command is finished, it gives you a couple commands to run. Follow the instructions and run them:
# To have launchd start postgresql at login:
ln -sfv /usr/local/opt/postgresql/*plist ~/Library/LaunchAgents
# Then to load postgresql now:
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
By default the postgresql user is your current OS X username with no password. For example, my OS X user is named chris
so I can login to postgresql with that username.
Final Steps
And now for the moment of truth. Let's create your first Rails application:
rails new myapp
#### If you want to use MySQL
rails new myapp -d mysql
#### If you want to use Postgres
# Note you will need to change config/database.yml's username to be
# the same as your OSX user account. (for example, mine is 'chris')
rails new myapp -d postgresql
# Move into the application directory
cd myapp
# If you setup MySQL or Postgres with a username/password, modify the
# config/database.yml file to contain the username/password that you specified
# Create the database
rake db:create
rails server
You can now visit http://localhost:3000 to view your new website!
Now that you've got your machine setup, it's time to start building some Rails applications.
If you received an error that said Access denied for user 'root'@'localhost' (using password: NO)
then you need to update your config/database.yml file to match the database username and password.