Posts Tagged Mac

Setting up Rails 3 on Mac OSX Snow Leopard 10.6.4

I finally made the complete switch to Mac this weekend.  I have been using my iMac at work for about a year and I love it, but I have been holding on to my Dell laptop and more and more every day I feel like throwing it through a window, so I decided to bite the bullet and buy a Macbook Pro so I could be more productive at home.  Here is a step by step guide of how I set it up to work with Rails 3.

1. Install git and RVM

RVM is a great way to manage multiple ruby versions on your Mac, and it also makes installing any version dead simple.  You will need to install “git” before you can install RVM.  I downloaded the git DMG from here:

http://git-osx-installer.googlecode.com/files/git-1.7.3.2-intel-leopard.dmg

Install git from the DMG and then you can install RVM afterwards.  Note that you will have to restart your terminal session in order to use the git command.

Install RVM by issuing the following commands in your shell:

Now you should be good to go with RVM. Here is the install guide if you run into any problems: http://rvm.beginrescueend.com/rvm/install/

Install a C Compiler (by installing XCode)

You will need a c compiler to install ruby using RVM.  You can download XCode, which has a c compiler, from the Apple website.  I downloaded the iOs/XCode package since i will probably be developing iPhone apps as well.  This is a mamoth download (3+ GB), so be prepared to wait a while.

If you dont feel like waiting that long for the download, pop in the OSX DVD that came with your Mac and when the startup screen appears, choose “Optional Installs”, and you can install XCode from there (without iOS stuff).

Install Ruby 1.9.2

cowboycoded$  rvm install 1.9.2

Simple as that!  This may take a few minutes to compile and install.  RVM will also install RubyGems, so its not necessary to do that manually.  Snow Leopard ships with Ruby 1.8.7, so you will need to switch to version 1.9.2 after you install it with RVM.  Use RVM to switch versions.

cowboycoded$  rvm 1.9.2

Verify that you are using 1.9.2

cowboycoded$  ruby -v

Set 1.9.2 as the default Ruby in RVM, so you don’t have to switch every time you open a new shell

cowboycoded$  rvm --default use 1.9.2

Install Ruby on Rails 3

cowboycoded$  gem install rails

This will install Rails 3 and all of its dependencies.  At the time of this writing the current version was 3.0.3.  I use MySQL for all of my apps, so I need to install that also.

Download and install MySQL server

http://dev.mysql.com/downloads/mysql/

Its probably easiest to download the DMG and install it that way.  Get the 64 bit DMG if you are using Snow Leopard.  Probably a good idea to install the MySQL Startup Item that is in the DMG as well.  This will start MySQL when your Mac boots.

After you install you can use the Startup Item to start MySQL manually:

cowboycoded$  sudo /Library/StartupItems/MySQLCOM/MySQLCOM start

If you are using the mysql gem (v2.8.1), then you will likely run into a problem when you bundle install. Install the gem manually using this command instead:

cowboycoded$  sudo env ARCHFLAGS="-arch x86_64" gem install mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config

Test it out

You should have everything you need at this point.  Fire up a new rails app and make sure everything worked:

cowboycoded$  rails new test_app
cowboycoded$  cd test_app
cowboycoded$  bundle install
cowboycoded$  rails s

Tags: , , , , ,

gem install mysql on Mac OSX Leopard with RVM

I installed RVM a few months back and messed around with it for a minute, but I did not try out Rails 3 beta with it. I have been researching Rails 3 a good bit lately, and I decide that it would be worth it to upgrade most of my apps to Rails 3, since it has so many appealing new features… especially the plugin API.  I have Ruby 1.8.6 installed on my box and you need a version greater than 1.8.7 (I think) in order to run Rails 3.  RVM is an easy way to run multiple versions of Ruby on the same box without conflicts.   So I fired up my shell and checked to see if RVM was working:

>rvm list

This returned my system ruby and nothing else. So I decided to install 1.9.1

>rvm install 1.9.1

It went thru the process and took a few minutes to download, configure and compile it. After it was complete I fired off this command:

>rvm 1.9.1
>ruby -v

From there I could see that I was in fact using Ruby 1.9.1 instead of 1.8.6 which was originally installed on my Mac. VERY COOL! I proceeded to install rails –pre and everything seemed to be working fine until I created a rails app and did some tests. It was complaining that I needed to install mysql 2.8.1 in order for activerecord to work. Note that in RVM, your gems are installed in a separate directory for each Ruby version that you install. Then I proceeded to install the mysql gem

>gem install mysql
Building native extensions.  This could take a while...
ERROR:  Error installing mysql:
        ERROR: Failed to build gem native extension.

/Users/johnmcaliley/.rvm/rubies/ruby-1.9.2-preview3/bin/ruby extconf.rb
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lm... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lz... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lsocket... no
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lnsl... no
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lmygcc... no
checking for mysql_query() in -lmysqlclient... no
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more
details.  You may need configuration options.

Provided configuration options:
        --with-opt-dir.............yada,yada,yada..........

Damn! I have seen that before.. way back when I first set up my Mac to run Rails 2.3.2. ohh.. so I need to tell it where mysql is. I searched G and found what was working for other people (Note: change -arch to your type system):

>sudo env ARCHFLAGS="-arch i386" gem install mysql -- \
>   --with-mysql-dir=/usr/local/mysql --with-mysql-lib=/usr/local/mysql/lib \
>   --with-mysql-include=/usr/local/mysql/include

Doh! Still not working.. I get the same error. So I proceeded to search and search to find the right way to do this. Here are some decent resources I ran across and they may help your particular situation, but nothing seemed to work for me.
http://wonko.com/post/how-to-install-the-mysqlruby-gem-on-mac-os-x-leopard
http://adamyoung.net/gem-install-mysql-OSX
http://movesonrails.com/journal/2010/4/21/rvm-installing-the-mysql-gem-ruby-191-under-osx.html(I think there are some typos in this blog and it appears rake-compiler is what you really need at the end)

So after a few hours of wanting to punch my screen, I decided to remove RVM, do a fresh install and try again.

>sudo rvm implode

This should remove RVM and all the files under $HOME/.rvm/
Actually… I did it without sudo first and it removed rvm, but not the .rvm directory under my home… so I had to sudo rm -Rf ~/.rvm

I followed the instructions on the RVM install page and it installed it just fine. This time I installed the lastest version of Ruby which is 1.9.2 (yes I tried that with RVM before I uninstalled it too and it did not work).

>rvm install 1.9.2
>rvm 1.9.2
>sudo env ARCHFLAGS="-arch i386" gem install mysql -- \
>   --with-mysql-dir=/usr/local/mysql --with-mysql-lib=/usr/local/mysql/lib \
>   --with-mysql-include=/usr/local/mysql/include

Worked like a charm… So I guess it was something with my original install of RVM. I must have configured it wrong or something.. When all else fails, start from scratch.

Tags: , ,