Installing postgres gem on OSX (Leopard with MacPorts PostgreSQL82)

Posted by Roy Hooper Sat, 08 Mar 2008 20:25:00 GMT

Unfortunately, out of the box, the postgres gem fails to install with the MacPorts postgres82-server install. There are two problems. The first is it can’t find pg_config. The second is incorrect architecture detection (thanks to Andreas Flierl at the RubyForge postgres module forums for pointing out the fix).

ERROR:  Error installing postgres:
    ERROR: Failed to build gem native extension.

/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby extconf.rb install postgres
extconf.rb:73: command not found: pg_config --bindir
The fix for this one is to fix the path temporarily:
 export PATH=/opt/local/lib/postgresql82/bin/:$PATH

Then when we retry the gem installation, we’ll get the following error(s):

postgres.c:41: error: static declaration of ‘PQserverVersion’ follows non-static declaration
/opt/local/include/postgresql82/libpq-fe.h:262: error: previous declaration of ‘PQserverVersion’ was here
postgres.c:41: error: static declaration of ‘PQserverVersion’ follows non-static declaration
/opt/local/include/postgresql82/libpq-fe.h:262: error: previous declaration of ‘PQserverVersion’ was here
postgres.c: In function ‘Init_postgres’:
postgres.c:2676: error: ‘pgconn_protocol_version’ undeclared (first use in this function)
postgres.c:2676: error: (Each undeclared identifier is reported only once
postgres.c:2676: error: for each function it appears in.)
postgres.c:2677: error: ‘pgconn_server_version’ undeclared (first use in this function)
postgres.c: In function ‘Init_postgres’:
postgres.c:2676: error: ‘pgconn_protocol_version’ undeclared (first use in this function)
postgres.c:2676: error: (Each undeclared identifier is reported only once
postgres.c:2676: error: for each function it appears in.)
postgres.c:2677: error: ‘pgconn_server_version’ undeclared (first use in this function)
lipo: can't open input file: /var/tmp//ccBatpen.out (No such file or directory)
make: *** [postgres.o] Error 1

These are fixed by specifying a specific architecture to use.

The following commands worked well for me:
export PATH=/opt/local/lib/postgresql82/bin/:$PATH
sudo env ARCHFLAGS="-arch i386" gem install --remote postgres

RMagick on OSX

Posted by Roy Hooper Mon, 19 Feb 2007 00:22:00 GMT

You’d think that installing rmagick on OSX would just work if you’re a fink user and use the gem.

You’d be dead wrong, unfortunately. Like I was.

Instead, you should use Darwin Ports and manually build ImageMagick/RMagick.

Fortunately there’s some really easy to follow instructions here: http://rmagick.rubyforge.org/install-osx.html

The error I was encountering occurred when I tried:

require_gem 'rmagick'
require 'RMagick'
Magick
in irb. The Magick bit failed telling me: NameError: uninitialized constant Magick. After the above, I can skip the require_gem part, and go straight to it:
require 'RMagick'

Today's Ruby lessons

Posted by Roy Hooper Fri, 03 Mar 2006 06:02:00 GMT

Today, I learned a few handy things about Ruby.

  • Ruby calls class properties "instance variables" and prefixes them with @
  • Ruby calls class static variables "class properties" and prefixes them with @
  • You can call static methods (class methods) with either .method() or ::method(), but should use ::method() for readability.
  • Class methods are defined by prefixing the method name with the class (eg Test.static_method).
  • Ruby lets you add or redefine methods to classes at any time. Declaring a new class block doesn't destroy the existing class definitions.
  • Object::constants, ::methods, ::class_variables, and other similar methods let you discover what is and is not yet defined with ease. See my previous blog about IRB and tab completion to get the most out of this tidbit.
  • The .respond_to? method is the way to tell if a method is defined for a particular object.
  • The collect method of an array is like map, but not quite. It is like the each method, but it collects the output of the block into a new array.
  • the p() method prints stuff out such as arrays/hashes in a more useful manner than print. For example:

    irb(main):070:0> p Object::methods
    ["to_a", "respond_to?", "ancestors", "module_eval", "const_missing", "type", "protected_methods", "instance_methods", "public_method_defined?", "superclass", "eql?", "instance_variable_set", "const_get", "is_a?", "autoload", "hash", "send", "to_s", "class_eval", "class_variables", "class", "public_instance_methods", "instance_method", "tainted?", "private_methods", "private_method_defined?", "__send__", "included_modules", "untaint", "const_set", "id", "inspect", "instance_eval", "clone", "public_methods", "protected_instance_methods", "protected_method_defined?", "extend", "autoload?", "freeze", "public_class_method", "display", "allocate", "__id__", "<=>", "<", "method", "methods", "==", "===", ">", "nil?", "dup", "instance_variables", "include?", "private_instance_methods", ">=", "instance_of?", "const_defined?", "<=", "name", "private_class_method", "new", "object_id", "=~", "singleton_methods", "method_defined?", "equal?", "taint", "constants", "frozen?", "instance_variable_get", "kind_of?"]
    => nil

    irb(main):071:0> print Object::methods
    to_arespond_to?ancestorsmodule_evalconst_missingtypeprotected_ methodsinstance_methodspublic_method_defined?superclasseql?instance _variable_setconst_getis_a?autoloadhashsendto_sclass_evalclass_ variablesclasspublic_instance_methodsinstance_methodtainted? private_methodsprivate_method_defined?__send__included_ modulesuntaintconst_setidinspectinstance_evalclonepublic_ methodsprotected_instance_methodsprotected_method_defined? extendautoload?freezepublic_class_methoddisplayallocate__id__<=>< methodmethods=====>nil?dupinstance_variablesinclude?private_ instance_methods>=instance_of?const_defined?<=nameprivate_ class_methodnewobject_id=~singleton_methodsmethod_defined? equal?taintconstantsfrozen?instance_variable_getkind_of?=>
    nil

Learning Ruby

Posted by Roy Hooper Thu, 02 Mar 2006 05:22:00 GMT

I’ve recently been exploring Ruby. I thought that I’d try my hand at blogging with a purpose. That said, I’m going to blog my progress as I learn this intriguing language.  <br />
Ruby describes itself as a mix of Smalltalk, Python, and Perl.  Some of its syntax seems weird and awkward at first, but I feel part of that is due to the fact that its even more lax than Perl about its syntax.  One of the challenges I faced was finding a decent set of examples showing the syntax and quirks of the language in action.  I originally tried to use the online version of Programming Ruby, but I found that it was not a great choice for me.  I need examples, and plenty of them.

I managed to overcome some of the limitations of this gude through general surfing (I wish I’d kept track of those web pages), and through playing with irb.  Irb is neat.  I think it stands for Interactive Ruby.  Its a shell-like interface to Ruby.  If you enable tab completion, then irb becomes even more valuable to the learner, as you can explore the classes, variables, and methods more easily.
<br />More recently, I found a rather oddly written book, Why’s (Poignant) Guide to Ruby.  I find the banter to be horribly annoying, but after the first chapter I learned to skip over it.  Buried in between inane and unnecessary blather, there’s tons of examples, with decent explanations to boot.  I think Why could reduce the size of the book by over 75% and call it Why’s Concice Guide to Ruby instead.

Something else I played with is Ruby on Rails. I walked through the first two tutorials (first, second).  I’m not sure I like how automagically it does some of the database stuff, but I intend to learn how it works so I can override it all.  I’ll post about that when I learn about it.  For now, though, the most interesting thing I got was from scanning through Amy’s blog entry about the MVC: Most Vexing ConundrumMVC or Model View Controller is a design model that I intend to learn more about.

For now though, I’m still making my way through Why’s guide.

Setting up a blog with Typo

Posted by Roy Hooper Thu, 02 Mar 2006 05:19:00 GMT

Sometimes you just have to try something for yourself. Today, that was setting up my own blog on my own server.

Yesterday, I posted my first article about my experiences with Ruby at a seldom used LiveJournal of mine. I eventually decided that it made more sense to have this on my own server, running my own choice of software on my own choice of OS (FreeBSD).

A bit of googling led me to Typo. Typo is a blogging package written using the Ruby on Rails framework.

The package looked slick, has themes, and other nice features, so I figured I’d give it a try. That was the easy part. I’ve spent the better part of my evening upgrading bits and pieces of software as a result.

I started off with the easy bits: upgrading Ruby and rails. This really was the easy part. In FreeBSD, it was a matter of installing the Ruby port, followed by the rubygem-rails port. This port automatically installed all the necessary dependencies, including RubyGems. RubyGems is a package manager type of thing for Ruby. It works well. It reminds me of CPAN for Perl.

Afterwards, I learned I would need FastCGI for good performance with any Rails app. Naturally, I had to upgrade Apache —but couldn’t do it the easy way without cutting off web service for my users for an unacceptable period of time. So I had to build a temporary copy of Apache 2.2 to test the new configuration. After I was satisfied that the temporary copy worked, I switched it on. I then was able to purge the old packages and dependencies, build the new Apache 2.2 package, install, configure, and then test it. I was finally ready to switch off the temporary Apache install and enable the new Apache packages.

Now my system was ready for FastCGI. FastCGI is a nifty way of speeding up applications that are slow to start, but fast once started—like Ruby and Perl applications.

Because I wanted to put this blog at http://www.royhooper.ca/blog/, I had to follow alternate install instructions. Of course this didn’t go smoothly either. I had to futz around with my configuration, looking for the culprit. Eventually, with the help of trusty Google, I ran across two [1, 2] helpful web pages.

I’m fairly happy with the results. I’ll definitely have to play with this theme to make it look the way I want, but I like how it works.