I strongly suggest that you use Pow server to run your Rails (or any Rack) applications locally.
It installs very fast, works with rbenv, you don’t need to configure anything and you can run multiple apps at once (each with its own version of Ruby and its own set of gems). This last advantage is particularly great because, in many occasions, you are dealing with multiple apps that are dependent on each other.
Installing pow is a breeze. At a terminal prompt do:
curl get.pow.cx | sh
enter your password when prompted, and you are good to go.
Pow runs any rack application (rails included) with a config.ru file. Let’s say we have a rails app under ~/work/bestapp. To run your app with Pow, cd into your app’s directory and install dependencies using bundle install. Then:
and symlink your project:
ln -s ~/work/bestapp
And now if you browse to http://bestapp.dev, you will be able to see your application running with Pow. Notice that the name of the symlink specifies the hostname you will use to access the application. ~Happy days!
For more info (like restarting apps, serving static assets, viewing logs, etc) visit Pow’s awesome user manual here.
Unarchive the downloaded zip file (usually by just double clicking on it). This will give you back the Postgres app.
Finally, drag and drop the Postgres app to your Applications folder.
Now, simply double click to launch the app (i.e. to run the world’s most advanced open source database on your Mac). It just couldn’t get any easier.
You should be able to see a tiny elephant icon in your menubar. That means your database is up and running.
Tip: I suggest setting up your database to run/start automatically at Login. To do that, click on the elephant icon in your menubar and select ‘Automatically Start at Login’.
Next, you are going to want to add the /bin directory that ships with Postgres app to your PATH. Open your .bash_profile (in your home directory) and add the following line to the bottom of it (if .bash_profile doesn’t exist, simply create it by running touch ~/.bash_profile):
Now if you type psql at a terminal prompt, you will be connected to the database server and will be able to interact with it through various commands. A list of all psql’s commands can be found in this tutorial.
Tip: running rails db command, in the context of your rails app, will do the same thing as psql.
Personally, my preferred way to explore the db structure, tables and data is through a GUI client. At the time of this writing, my favourite client is PG Commander, but other alternatives are pgAdmin and Induction app.
Using Postgres with your Rails app:
Alright, with Postgres app successfully setup, we can now create a new Rails app, and instruct it to use PostgreSQL as database:
rails new myapp -d postgresql
When the application is created, Bundler will install the pg gem. Looking at the database.yml file, you can see the application is already configured to use Postgres. However, you need to make some minor changes to make it work with postgres.app. Your configuration should look something like this (for development, as well as, test and production databases):
at the root of your rails app to create the database.
To verify that your Rails app is able to connect to your newly created database, and is able to interact with it, we are going to create a post scaffold, run the migration, and check if we can create, retrieve, update and delete posts through the browser:
rails g scaffold post title:string body:text
Browse to http://0.0.0.0:3000/posts and try creating some posts. If you can retrieve, update and delete them successfully, you are all set!
Then run brew doctor in terminal to make sure everything is ok with your homebrew installation, in which case, you will see the “Your system is ready to brew” message. Don’t worry if there are any warnings, just read the description carefully, most of the time you will find useful suggestions. Follow them, and you will be fine. During my installation, I got a warning about an outdated version of Git, and as a remedy, homebrew suggested that I run brew install git which solved the problem. Running brew doctor again verified it, and I was good to go.
Finally, run brew update
Run the following command at a terminal prompt:
brew install rbenv
You will need to configure your bash profile. You’ll only ever have to do this once. (in case you cannot find your bash profile in the home directory, simply create it by running touch ~/.bash_profile.
echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
Install ruby-build and rbenv rehash gem using homebrew:
Run rbenv rehash. In fact you should run this every time you install a new version of ruby or when you install a gem that provides a command.
Set the 2.0 version as the global default (because it is the latest and greatest ruby, you should all use/prefer it over other versions):
rbenv global 2.0.0-p195
At this point you should spend some time reading rbenv command reference
and get familiarised with different commands that rbenv provides for simple ruby version management. Highly recommended that you read the above link very carefully before moving on to the rest of this tutorial. however, here’s a summary of the most important commands:
rbenv local <VERSION> : will set the local project level version of ruby to the VERSION specified. rbenv local : will report the local version of ruby currently being used. rbenv versions : will report all ruby versions known to rbenv. rbenv version : will report the currently active ruby version. rbenv which irb : will show the full path to the executable that rbenv will invoke when you run the given command.
Next, we will update to the latest Rubygems version: