a better way of building ruby, rails, lighttpd, mysql, and postgres on OS X tiger

motivation

basics

First, install the Apple Xcode tools from your OS X installation disc.

Second, install DarwinPorts from here. It’s just a regular Mac package.

Now go to the terminal (Applications/Utilities/Terminal) and type the following commands:

sudo port -d selfupdate
sudo port install lighttpd +ssl
sudo port install rb-rubygems
sudo port install rb-fcgi
sudo port install mysql4 +server

If rubygems doesn’t install properly, try Jamie’s tip.

If the mysql install fails, try setting your GCC version back to 3.3 for the duration of the command:

sudo gcc_select 3.3
sudo port install mysql4 +server
sudo gcc_select 4.0

The above trick may help you if other packages fail, too. You can try mysql5; I’m just not sure if the ruby mysql bindings are current enough.

rmagick or postgres or others

Also maybe you want rmagick (more information): sudo port install rb-rmagick

You could also use the gem (sudo gem install rmagick) if you need a more up-to-date version. The gem version may have more complicated dependencies, though.

For postgres, do: sudo port install postgresql81 +server

If you need other things, you can use port list whatever to browse the available DarwinPorts packages. whatever can contain wildcards (example: port list rb* ).

fix the system path

Next, add /opt/local/bin to the system PATH in the file /etc/profile, so that it looks as below. You might as well add /opt/local/sbin and the /usr/local/ folders while you’re at it. So in the end, the line should look like this:

PATH="/opt/local/bin:/opt/local/sbin:/usr/local/bin:\
/usr/local/sbin:/bin:/sbin:/usr/bin:/usr/sbin"

The PATH environment variable tells the system where to look (and in what order) for executable files that are not run with an explicit path. It is better to put it in /etc/profile, which is system-wide, as opposed to ~/.bashrc, which is specific to the current user.

gems

Please note; if installing gems fails with some kind of require error, try reinstalling the rb-rubygems port. It seems to not complete correctly the first time around for many people.

Rails, rake, mysql:

sudo gem install rails
sudo gem install rake

# do this all on one line
sudo gem install mysql -- --with-mysql-config=/opt/local/bin/mysql_config

You may have to use mysql_config4 or mysql_config5, depending.

Make sure to choose a ruby version of the mysql gem, and not win32. If you are having compile errors with the 2.7 version, and are using the mysql.com distribution of MySQL instead of the MacPorts distribution, this article might help you.

Capistrano:

sudo gem install capistrano

If you use postgres because of its awesomeness:

# do this all on one line
sudo gem install postgres -- --with-pgsql-include-dir=/opt/local/include/postgresql81
--with-pgsql-lib-dir=/opt/local/lib/postgresql81

make lighttpd start automatically

Next, you can configure a webserver daemons. This step is optional, and not very useful if you don’t need to develop under a more deployment-like environment. Normally script/server from the Rails directory will be your development web server.

The lighttpd configuration file lives at /opt/local/etc/lighttpd.conf. If you put this file in /Library/LaunchDaemons/, and load it into the launchd list via sudo launchctl load -w /Library/LaunchDaemons/net.lighttpd.plist, it will boot automatically at start-up. You can then stop and start the lighttpd launchd daemon with sudo launchctl start net.lighttpd, etc.

Note, loading the launchd item will fail if there isn’t a working configuration file at /opt/local/etc/lighttpd.conf.

make the db start automatically

Lastly, DarwinPorts will also put a file similar to org.darwinports.mysql5.plist or org.darwinports.postgresql81-server.plist in your /Library/LaunchDaemons/ folder, depending on which RDBMS you have decided to install. You can use this to start the database server automatically, just like starting lighttpd as mentioned above. You need to do all the same steps:

sudo launchctl load -w /LibraryLaunchDaemons/org.darwinports.postgresql81-server.plist
sudo launchctl start org.darwinports.postgresql81-server

Chances are, starting the server will silently fail. If you look at the .plist file with a text editor, you will notice that it is calling some wrapper scripts that are probably in /opt/local/etc/LaunchDaemons/ somewhere. If you execute these by hand, from the command line, you can see what the problem is. Usually it is expecting your actual database to be in some other location than where you put it, assuming you initialized the database at all. Resolve this either by modifying the wrapper script directly, or by adding the appropriate env variable to /etc/profile.

done

I am assuming that you will be able to load your schema and configure your database users/roles yourself.

Note, don’t try to run two instances of port in parallel, even if you have dual cores. They will fight.

Also, you will want an IDE/editor such as RadRails (yay!) or TextMate (hmm…). And you will need Firefox and probably want the following extensions: Web Developer, Tamper Data, Html Validator, FireBug, Tab Mix Plus, User Agent Switcher, ColorZilla, and MeasureIt.

Also, I recommend using a visual database modeler, even if you have to run it under Parallels. Your choices are mainly DBDesigner (mysql only, free), Toad Data Modeler (formerly CASE Studio, now free), Sybase PowerDesigner (expensive), and Rational Rose (expensive). You may expect your data model to stay simple, but chances are it won’t. The penalty for not understanding the complete implications of your data model is great; this is one place where the enterprise got it right. That said, do what you think is best.

really done

And now you are done, and all set up to use Prototype to make the best lightbox ever, even better than the five hundred other ones which are also the best ever.

This is all based on me piecing together the things that worked for me on OS X 10.4.6 (Intel). Let me know if you have trouble.

26 responses

  1. Hi, Thanks for putting the time into this tutorial.

    I ran into errors when installing rb-gems. Is this vital, because it says that it was installed successfully, so I’m confused.

    [snip]

    Error: /opt/local/bin/port: Status 1 encountered during processing.

  2. Hmm. I didn’t have any trouble with ruby-gems 0.8.11. But having gem support is important. Can you successfully require gems in source code? If so I wouldn’t worry about it.

    If not, the line

    /opt/local/var/db/dports/build/... failed: You don’t have write permissions into the /opt/local/var/db/dports/build/... directory

    seems telling. You could try deleting the ‘build’ folder entirely and installing ruby-gems again. If that still doesn’t work, there is some flag to request port to install from pre-compiled binaries, if available. You could try that, too.

    It is also slightly possible that you ran out of disk space.

  3. I tried it again, and it worked fine, so all must be in order. Thank you again for this guide.

    This is the error I get from launchctl.

    Numenor:/users/blissdev/development/rails blissdev$ sudo launchctl stop net.lighttpd Password: launchctl stop error: No such process

    Is there a specific place I should be installing my rails apps?

  4. Jordan,

    I updated the lighttpd/launchctl section above; please try that.

    There is no specific place to install your Rails apps, the Desktop or your ~ directory is fine. I think the default Rails framework dumped out by the rails command includes a sample lighttpd.conf.

  5. Ok, one more problem. Everything seems to be working great except for the mysql. I’m getting an error when I try to run mysql:

    ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ’/opt/local/var/run/mysqld/mysqld.sock’ (2)

    Any thoughts?

  6. You need to start a mysql server daemon that runs in the background and actually manages the database, and have it listen on that particular socket. mysql is just the command-line interface. It looks like either your server daemon is not running, or the socket location is wrong… does that help at all?

    You can also take a look at the “start mysql 5” instructions from this guy.

  7. I did uninstall port on mysql4 to try 5, but i still get this. any ideas?

    Numenor:~ blissdev$ sudo port install mysql5 +server -f -> Activating mysql5 5.0.22_0+darwin_8+server Error: Target com.apple.activate returned: Image error: /opt/local/libexec/mysqld is being used by the active mysql4 port. Please deactivate this port first, or use the -f flag to force the activation. Error: /opt/local/bin/port: Status 1 encountered during processing.

  8. Try looking at this. Also, I think the -f flag has to come directly after the word port in your command string. DarwinPorts documentation is here; I admit that it can be a little wonky unless you’re already familiar with apt.

  9. I was able to successfully uninstall 4 and install 5. But now I’m getting this. And the sock file doeesn’t exist. I’m really sorry for all these questions, but I’m a bit of a unix/osx command line noob.

    Numenor:~ blissdev$ /opt/local/lib/mysql5/bin/mysqladmin -u root password password /opt/local/lib/mysql5/bin/mysqladmin: connect to server at ‘localhost’ failed error: ‘Can’t connect to local MySQL server through socket ’/opt/local/var/run/mysql5/mysqld.sock’ (2)’ Check that mysqld is running and that the socket: ’/opt/local/var/run/mysql5/mysqld.sock’ exists!

    Is there a way to generate the sock file?

    I finally decided to install it via the mysql binary from their site. It seems to be working fine, but do I need to do something special to connect it with rails? Maybe something with the mysql gem?

  10. As far as I know, the only important thing for a unix socket is that the file handle exists. So you should be able to just do touch /opt/local/var/run/mysql5/mysqld.sock. Also you might have to change the permissions to 666 or 777.

    If you are not using the port mysql, you will have to change the paths in the sudo gem install mysql command above.

  11. You wrote “Also, I recommend using a visual database modeler, even if you have to run it under Parallels.” Have you used and if so, do you like CocoaMySQL? What about the tools sold my MySQL AB? I’m trying to get a sense for what people use and find helpful.

  12. I haven’t used CocoaMySQL. I used Case Studio, now available free as Toad Data Modeler. I have extremely limited experience with Rational products.

    The data modeler was invaluable to me when my SQL skills were weak. But as I’ve become more experienced, the complexity threshold at which I would want to use a modeler has increased. So these days I wouldn’t use one except for a super-complicated schema (more than 20 or so models).

  13. Thanks, Evan, for this quick and easy guide to Rails on MacOSX. I used hivelogic last time I went through this, and using ports is both easier, and superior in terms of maintenance.

    A few pointers on how it runs on OS X 10.4.8 with MacPorts 1.320 and Ruby 1.8.5.1:

    Rake is now a dependency of Rails, so run instead:

    sudo gem install -y rails

    likewise,

    sudo gem install -y capistrano

    Also, when running ‘sudo gem install mysql…’, the user is prompted for the MySQL version. I chose:

    2. mysql 2.7 (ruby)

    Lastly, after installing the msyql4 port, you’ll need to run

    sudo -u mysql mysql_install_db

    then these two will work:

    sudo /opt/local/bin/mysqld_safe --user=mysql
    /opt/local/bin/mysqladmin -u root password newpassword

    Then I can run the following:

    sudo launchctl load -w  \
      /Library/LaunchDaemons/org.macports.mysql4.plist
    sudo launchctl stop org.macports.mysql4
  14. I think, in Peters Post it should look like:

    sudo launchctl load -w \
    /Library/LaunchDaemons/org.macports.mysql4.plist

    not

    sudo launchclt load -w \
    /Library/LaunchDaemons/org.macports.mysql4.plist

  15. Hi and thank you for the nice guide you posted here. The comments were also welcomed so thank you all!

    I have a small side question though. You mention here the Toad modeler, but this is a Windows app. An ERD modeler is a must-to-have tool but I was unable to find one for OSX. Is anybody here able to help me with a path to a tool like this? I am now forced to do modeling on Windows and to return to my MAc for the rest of the development but … that’s … not nice.

    Any help is highly appreciated and thenk you in advance!

    -florin

  16. I would run Toad/CASE Studio in Windows under Parallels on my Intel Mac. It was a little bit of a pain but not too bad, since you can share your working directory as a Parallels folder. I don’t know of any native OS X modelers.

  17. I’m running that big long string after the rails and rake and I’m coming up with this:

    Select which gem to install for your platform
    (powerpc-darwin8.8.0)
      1. mysql 2.7.3 (mswin32)
      2. mysql 2.7.1 (mswin32)
      3. mysql 2.7 (ruby)
      4. mysql 2.6 (ruby)
      5. mysql 2.5.1 (ruby)
      6. Cancel installation
    >

    I installed MySQL 5 already so I don’t know what to do here…you have any clue?

  18. Since you have a non-Windows system, you need the most recent pure Ruby driver, which in this case is #3.

  19. I’m unable to install lighttpd via MacPorts. Somehow, port can’t get a file called patch-doc-lighttpd.

    After some googling I downloaded the file manually from MacOSForge (http://trac.macosforge.org/project…) and placed it into /opt/local/var/db/dports/distfiles/lighttpd/ only to have then port throw a “checksum failed” error into my face :(

    Any ideas how to solve this?

    carrot:~ winsmith$ sudo port install lighttpd +ssl
    --->  Fetching lighttpd
    --->  Attempting to fetch patch-doc-lighttpd.conf
    ...
    --->  Attempting to fetch patch-doc-lighttpd.conf
    Error: Target com.apple.fetch returned: fetch failed
    Error: Status 1 encountered during processing.
    
  20. Um… nevermind, it’s working now. It seems there was something wrong with MacPort’s mirrors. Thanks for your educating post nonetheless. bye

  21. Sure thing. I was kind of waiting for the transition to MacPorts to be completed, but it seems like it will be a while.

  22. Thanks for this post. I keep referring to it again and again when I’m setting up a new Mac for Rails.

    About visual database modelers, I recommend SQLEditor. It’s not free, but it’s a Mac-native program, with a small footprint, snappy performance, and just the right feature set for me. It can connect to a number of RDBMSes through JDBC, and build schemas from them. It even exports your schema to Rails migrations.

    I don’t understand why SQLEditor isn’t more popular.

Follow

Get every new post delivered to your Inbox.

Join 517 other followers