Ahmad Raza
Posted on February 5, 2023
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:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Installing Ruby
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' >> ~/.zshrc
source ~/.zshrc
To install Ruby and set the default version, we'll run the following commands:
rbenv install 3.2.0
rbenv global 3.2.0
Confirm the default Ruby version matches the version you just installed.
ruby -v
Installing Rails
Installing Rails is as simple as running the following command in your Terminal:
gem install rails -v 7.0.4
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 7.0.4
Setting Up A Database
We're going to install sqlite3 from homebrew because we can't use the built-in version with macOS Sierra without running into some troubles.
brew install sqlite3
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.
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:
brew services start postgresql
By default the postgresql user is your current macOS username with no password. For example, my macOS user is named ahmad
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 macOS 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.
Posted on February 5, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.