How to install and use RVM

RVM is a critical part of your Ruby on Rails development stack. Why? Because it allows you to work in multiple Ruby environments. As soon as you build out more than one project you could find yourself having to deal with one project at a certain version of Ruby and another project at a different version. Without a tool like RVM. This can be a huge headache. Fortunately, installing RVM is pretty easy.

So let’s start on a bare OS X system. You’ll notice you have Ruby installed already by running the following command in the OS X Terminal App (all the commands in this post should be run in Terminal):

ruby -v

Outputs:

ruby 2.0.0p481 (2014-05-08 revision 45883) [universal.x86_64-darwin14]

At the time of this post, the Ruby on my OS X 10.10 Yosemite was 2.0.0 patch level 481.

What you should be comfortable determining is whether or not you are seeing the OS X version of Ruby or the RVM version (the one you want).

So lets see the path to the ruby executable:

which ruby

Outputs:

/usr/bin/ruby

The output /usr/bin/ruby tells you that you are referencing the built in OS X ruby. Let’s fix that.

Here’s how you install RVM:

\curl -L https://get.rvm.io | bash -s stable --ruby

This could take some time to finish. When it does, run the previous command again to check your ruby executable path. It should read:

/Users/your-username/.rvm/rubies/ruby-2.1.3/bin/ruby

See that .rvm in the path? That means RVM is installed and being properly referenced!

The –ruby flag in the previous command installed the latest version of ruby. You can see that it has changed by checking for the version again:

ruby 2.1.3p242 (2014-09-19 revision 47630) [x86_64-darwin14.0]

Again, based on when you read this post and run these commands, your versions may be different.

Here are some default commands to get you started with RVM:

rvm list

This will show you the ruby executables (also called rubies) installed on your machine.

rvm list known

This will show you the rubies you can install

rvm install [insert ruby version number]

This will install a specific version of ruby to your machine

rvm install 2.0.0

rvm use [insert ruby version number] –default

This will make sure that a specific version of ruby is used every time you open a terminal window

rvm use 2.0.0 --default

rvm gemset list

This will list all the gemsets for your current ruby version

rvm gemset create [insert gemset name]

This will create a gemset in your current ruby version

rvm gemset create my-gemset

rvm use [insert ruby version number @ gemset name] –default

This will make sure a specific version of ruby is loaded every time AND at a specific gemset

rvm use 2.0.0@my-gemset

Now. What do you need to do in order to get started with Rails? Well, to be honest, there is some discussion as to whether or not gemsets are even required with Bundler. Obviously they are more important if you are developing a Ruby project without Bundler. So, for me, I find setting a ruby version is all I need.

Using RVM you have the ability to manage different ruby versions, what gems are included with each one and you’re not cluttering up the built in ruby with all your various project gems.

How to install and use PostgreSQL

If you’re a Rails developer and you’re not using Postgres for your database, its likely because you have already invested time and energy installing and configuring MySQL on your Mac. You also probably got use to great tools like Sequel Pro for managing those databases also.

However, it seems the community has really moved towards Postgres. Why? Well I can’t say for everyone, but for me there were two big reasons:

1. Heroku

Learning Rails can be daunting enough, but also learning how to do a production deployment of a Rails application can take it over the edge. To be honest, there really isn’t much of a reason to learn how to do a good production deployment of Rails unless you are personally responsible for taking client or company projects end to end – and even then, why wouldn’t you use Heroku, save yourself the liability of being responsible for managing a production Rails deployment. In my opinion, get really good at Rails. I’m not saying those server management skills aren’t important – I’m saying there are very important and just as specialized as learning Rails itself. So let someone who’s neck deep in operations take care of your app. End rant.

2. PostGIS

This doesn’t get talked about enough, but mobile introduced a huge new skill gap in developers. Those who know how to work with locations and those who don’t. What do I mean? Almost every mobile application has some location based feature. Well if you know mobile development then you are thinking: “That’s easy! iOS gives me the location of a user. done”. True, so does a web browser, that’s not the hard part. The hard part is asking whether that location is near another one… oh ya… you forgot about that part. Now there are a ton of ways to approach this, but PostGIS makes this a much easier task. Also, you think doing a production Rails deployment is hard… deploy a GIS product or server. No thanks.

So how do we install Postgres on OS X? Well there are a couple ways. Here’s the easy way. The way I do it.

Install the Postgres app

Go to http://postgresapp.com and click Download. Then drag that app to your Applications folder and fire it up. Done!

Its almost too simple.

So how do we use this with Rails? Well you will need the pg gem. When you try to install the gem you will likely get an error that it can’t find pg_config. That’s because its packaged in that cool app we just downloaded, so all you have to do is tell the gem where it is. Here’s how you do that:

gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.3/bin/pg_config

Note: You may have to change the version number, but at the time of this post it was 9.3

Now you just fire up a rails project using:

rails new myapp --database=postgresql

Using a PostgreSQL Client on the Mac

That’s it! Now you could click that Open psql button and start managing your databases from the command line, and there is nothing wrong with that, but you might want a nice tool with an easy to use graphical interface. Unfortunately Sequel Pro… is MySQL only. So the best option I found is PGCommander. Its actually great, but its also $40. If you find something as good and cheaper let me know. I use PGCommander.

Enabling PostGIS for PostgreSQL

Remember the other thing I talked about? How cool PostGIS is. Well another great thing about that Postgres app is that its easily enabled if you are build some cool location based Rails web services!

To enable PostGIS, connect to your database and enter the command:

CREATE EXTENSION postgis;

So that’s it! Now you have a PostgreSQL database on OS X ready for development. You can easily start using PostGIS services if you need and you can manage the databases using PGCommander!

I’m building an iOS app. Right now.

That’s right. I’m building an app right now.

I’ve found myself here with the precious commodity of time after doing the obligatory family visits over the holidays. I’ve always had a couple ideas, but never had the time. So here I am.

Getting started is always the hardest part… luckily I’ve been preparing for this moment for far too long… sort of an analysis paralysis kind of thing. I’ve been through a bunch of iOS online programs so I’m not completely green, stuff like iOS App Development with Swift Essential Training by lynda.com. I’ve also read a few books on the subject, Beginning iPhone Development by Apress is a good place to start if you’re just getting started – in fact I’m going to keep this at my side as I get going here – to reference the basics in case I get stuck. I’ve also purchased The Complete iOS 8 Course with Swift by Bitfountain. I’m going to reference this as well if I need to.

For the backend? I’m a rails (is it Rails?) developer. So I’ll use that. I’m gonna stub it out with hard-coded values to start… not really worrying about most of the backend until the end. I’ll push the app to Heroku so I can test it easily during development.

So what am I building? Well I have a couple ideas. I’m going to continue blogging my process and the problems I am hoping to solve, so maybe you’ll be able to figure it out… but for now… its a surprise.

Let’s get started!

How to fix: brew update files overwritten by merge

Sometimes when you run:

brew update

You get an error:

brew update the following untracked working tree files would be overwritten by merge

According to the repo wiki for Homebrew this is caused by an old bug in update that has long since been fixed.

Basically you have changed (uncommitted) files in your Homebrew working directory that you need to deal with. You simply need to reset the working directory so your update command can pull the latest files. If you’re familiar with git – then this will all make sense, if not, just run these 3 commands and your Homebrew will be fixed!

cd /usr/local
git fetch origin
git reset --hard origin/master

How to install and use Homebrew

Updated for OS X 10.10 Yosemite! Should work on OS X 10.9 Mavericks too.

If you’re doing any Ruby on Rails or other open source development on your Mac, you’re going to need Homebrew.

What is Homebrew?

Homebrew is a package management system for your Mac. Homebrew makes it easy to install the software you generally need for Ruby on Rails development. Sometimes when you install RubyGems for your project, they will be dependent on software being installed on your Mac. One of the more obvious examples would be a MySQL gem and the actual MySQL Database Server. You install the MySQL Database Server using Homebrew! One of the less obvious examples would be the Nokogiri gem. This gem uses a software package named libxml2. You would install the libxml2 software package using Homebrew in order to install the Nokogiri RubyGem.

So how do you install Homebrew?

Installing Homebrew is dead simple. However, it does have a rather large dependency. You will need the command line tools addon of Xcode installed. Xcode is made by Apple and its used to write Mac and iOS applications. Xcode is free and all you need to do is search for it on the Mac App Store and click install. Its a rather large application, so it may take a while to download and install.

Once you’ve finished installing Xcode, you will need to install the command line tools.

Installing Command Line Tools on older versions of Xcode

You will need to open Xcode and choose Xcode -> Preferences. Choose the Downloads tab and install the Command Line Tools. If you don’t see any of this in Preferences, then read the next section…

Xcode Command Line Tools

This will take some time, but when its done your Mac will be ready to install Homebrew!

Install Command Line Tools on the latest version of Xcode

Open up a Terminal window. If you aren’t doing that by using Spotlight (which is how you should open ALL your Mac apps) then you do that by pressing CMD + SPACE, then searching ‘Terminal’. Hit Enter.

Inside the terminal window type:

xcode-select --install

If you see the message:

xcode-select: error: command line tools are already installed, use "Software Update" to install updates

Then you already have the command line tools installed, otherwise, a dialog box will popup prompting you to install the command line tools. Go ahead and install them.

When the installation is complete you should be able to run:

xcode-select -p

That should output:

/Applications/Xcode.app/Contents/Developer

Installing Homebrew

Now that you have the Command Line Tools for Xcode installed, its time to install Homebrew. Remember I said it was easy… well it is.

If you visit the Homebrew website, you will see the following command:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

This command will need to be run in the OS X Terminal App. This means copy and pasting it into the Terminal window and pressing ENTER.

When the command is complete you should be able to run the following command in the Terminal window:

brew --version

If this works, then Homebrew is installed!

Now let’s go over a few common tasks you will have with Homebrew.

brew doctor

You want to use this command after system changes (such as OS X or Xcode updates) or if you think you need to troubleshoot your Homebrew installation. The output will tell you what’s wrong and generally how to fix it.

brew update

This will update your Homebrew formula list. Note, this doesn’t update all your installed packages! What this does is update the versions and availability of packages you could install. You want to run this often to keep your Homebrew installation up to date with the latest available software.

brew upgrade

This will upgrade your installed packages.

brew search [insert search term here]

This command will search the possible packages so you can find the one you want

brew install [insert package name here]

This will install a software package onto your system.

brew list

This will list all the packages that are currently installed on your system.

brew info [insert package name here]

Sometimes after you install a package, you get a screenful of information or configuration steps. If you’re like me, you will probably blow right past that and keep working – then it will dawn on you that you probably should have read some of that stuff. This will let you go back and do that.

brew uninstall [insert package name here]

And finally, you may want to remove a package you previously installed. That’s what this is for.

So that’s a quick intro to Homebrew and how you can manage software dependencies for RubyGems on your Mac!