EDIT: This post has been updated and retitled for El Capitan.

 

If you have attempted to install the Nokogiri gem and encountered errors similar to the output below… I feel your pain and have a solution.

The Pain

➜ z gem install nokogiri Building native extensions. This could take a while... ERROR: Error installing nokogiri: ERROR: Failed to build gem native extension.
/Users/mariozig/.rvm/rubies/ruby-2.2.1/bin/ruby -r ./siteconf20150318-11691-1hlore5.rb extconf.rb
checking if the C compiler accepts ... yes
checking if the C compiler accepts -Wno-error=unused-command-line-argument-hard-error-in-future... no
Building nokogiri using packaged libraries.
checking for gzdopen() in -lz... yes
checking for iconv using --with-opt-* flags... yes
************************************************************************
IMPORTANT NOTICE:
Building Nokogiri with a packaged version of libxml2-2.9.2
with the following patches applied:
 - 0001-Revert-Missing-initialization-for-the-catalog-module.patch
 - 0002-Fix-missing-entities-after-CVE-2014-3660-fix.patch
Team Nokogiri will keep on doing their best to provide security
updates in a timely manner, but if this is a concern for you and want
to use the system library instead; abort this installation process and
reinstall nokogiri as follows:
gem install nokogiri -- --use-system-libraries
 [--with-xml2-config=/path/to/xml2-config]
 [--with-xslt-config=/path/to/xslt-config]
If you are using Bundler, tell it to use the option:
bundle config build.nokogiri --use-system-libraries
 bundle install
Note, however, that nokogiri is not fully compatible with arbitrary
versions of libxml2 provided by OS/package vendors.
************************************************************************
Extracting libxml2-2.9.2.tar.gz into tmp/x86_64-apple-darwin14.1.0/ports/libxml2/2.9.2... OK
Running patch with /Users/mariozig/.rvm/gems/ruby-2.2.1/gems/nokogiri-1.6.6.2/ports/patches/libxml2/0001-Revert-Missing-initialization-for-the-catalog-module.patch...
Running 'patch' for libxml2 2.9.2... OK
Running patch with /Users/mariozig/.rvm/gems/ruby-2.2.1/gems/nokogiri-1.6.6.2/ports/patches/libxml2/0002-Fix-missing-entities-after-CVE-2014-3660-fix.patch...
Running 'patch' for libxml2 2.9.2... OK
Running 'configure' for libxml2 2.9.2... OK
Running 'compile' for libxml2 2.9.2... OK
Running 'install' for libxml2 2.9.2... OK
Activating libxml2 2.9.2 (from /Users/mariozig/.rvm/gems/ruby-2.2.1/gems/nokogiri-1.6.6.2/ports/x86_64-apple-darwin14.1.0/libxml2/2.9.2)...
************************************************************************
IMPORTANT NOTICE:
Building Nokogiri with a packaged version of libxslt-1.1.28
with the following patches applied:
 - 0001-Adding-doc-update-related-to-1.1.28.patch
 - 0002-Fix-a-couple-of-places-where-f-printf-parameters-wer.patch
 - 0003-Initialize-pseudo-random-number-generator-with-curre.patch
 - 0004-EXSLT-function-str-replace-is-broken-as-is.patch
 - 0006-Fix-str-padding-to-work-with-UTF-8-strings.patch
 - 0007-Separate-function-for-predicate-matching-in-patterns.patch
 - 0008-Fix-direct-pattern-matching.patch
 - 0009-Fix-certain-patterns-with-predicates.patch
 - 0010-Fix-handling-of-UTF-8-strings-in-EXSLT-crypto-module.patch
 - 0013-Memory-leak-in-xsltCompileIdKeyPattern-error-path.patch
 - 0014-Fix-for-bug-436589.patch
 - 0015-Fix-mkdir-for-mingw.patch
Team Nokogiri will keep on doing their best to provide security
updates in a timely manner, but if this is a concern for you and want
to use the system library instead; abort this installation process and
reinstall nokogiri as follows:
gem install nokogiri -- --use-system-libraries
 [--with-xml2-config=/path/to/xml2-config]
 [--with-xslt-config=/path/to/xslt-config]
If you are using Bundler, tell it to use the option:
bundle config build.nokogiri --use-system-libraries
 bundle install
************************************************************************
Extracting libxslt-1.1.28.tar.gz into tmp/x86_64-apple-darwin14.1.0/ports/libxslt/1.1.28... OK
Running patch with /Users/mariozig/.rvm/gems/ruby-2.2.1/gems/nokogiri-1.6.6.2/ports/patches/libxslt/0001-Adding-doc-update-related-to-1.1.28.patch...
Running 'patch' for libxslt 1.1.28... OK
Running patch with /Users/mariozig/.rvm/gems/ruby-2.2.1/gems/nokogiri-1.6.6.2/ports/patches/libxslt/0002-Fix-a-couple-of-places-where-f-printf-parameters-wer.patch...
Running 'patch' for libxslt 1.1.28... OK
Running patch with /Users/mariozig/.rvm/gems/ruby-2.2.1/gems/nokogiri-1.6.6.2/ports/patches/libxslt/0003-Initialize-pseudo-random-number-generator-with-curre.patch...
Running 'patch' for libxslt 1.1.28... OK
Running patch with /Users/mariozig/.rvm/gems/ruby-2.2.1/gems/nokogiri-1.6.6.2/ports/patches/libxslt/0004-EXSLT-function-str-replace-is-broken-as-is.patch...
Running 'patch' for libxslt 1.1.28... OK
Running patch with /Users/mariozig/.rvm/gems/ruby-2.2.1/gems/nokogiri-1.6.6.2/ports/patches/libxslt/0006-Fix-str-padding-to-work-with-UTF-8-strings.patch...
Running 'patch' for libxslt 1.1.28... OK
Running patch with /Users/mariozig/.rvm/gems/ruby-2.2.1/gems/nokogiri-1.6.6.2/ports/patches/libxslt/0007-Separate-function-for-predicate-matching-in-patterns.patch...
Running 'patch' for libxslt 1.1.28... OK
Running patch with /Users/mariozig/.rvm/gems/ruby-2.2.1/gems/nokogiri-1.6.6.2/ports/patches/libxslt/0008-Fix-direct-pattern-matching.patch...
Running 'patch' for libxslt 1.1.28... OK
Running patch with /Users/mariozig/.rvm/gems/ruby-2.2.1/gems/nokogiri-1.6.6.2/ports/patches/libxslt/0009-Fix-certain-patterns-with-predicates.patch...
Running 'patch' for libxslt 1.1.28... OK
Running patch with /Users/mariozig/.rvm/gems/ruby-2.2.1/gems/nokogiri-1.6.6.2/ports/patches/libxslt/0010-Fix-handling-of-UTF-8-strings-in-EXSLT-crypto-module.patch...
Running 'patch' for libxslt 1.1.28... OK
Running patch with /Users/mariozig/.rvm/gems/ruby-2.2.1/gems/nokogiri-1.6.6.2/ports/patches/libxslt/0013-Memory-leak-in-xsltCompileIdKeyPattern-error-path.patch...
Running 'patch' for libxslt 1.1.28... OK
Running patch with /Users/mariozig/.rvm/gems/ruby-2.2.1/gems/nokogiri-1.6.6.2/ports/patches/libxslt/0014-Fix-for-bug-436589.patch...
Running 'patch' for libxslt 1.1.28... OK
Running patch with /Users/mariozig/.rvm/gems/ruby-2.2.1/gems/nokogiri-1.6.6.2/ports/patches/libxslt/0015-Fix-mkdir-for-mingw.patch...
Running 'patch' for libxslt 1.1.28... OK
Running 'configure' for libxslt 1.1.28... ERROR, review '/Users/mariozig/.rvm/gems/ruby-2.2.1/gems/nokogiri-1.6.6.2/ext/nokogiri/tmp/x86_64-apple-darwin14.1.0/ports/libxslt/1.1.28/configure.log' to see what happened.
*** 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
 --with-opt-include
 --without-opt-include=${opt-dir}/include
 --with-opt-lib
 --without-opt-lib=${opt-dir}/lib
 --with-make-prog
 --without-make-prog
 --srcdir=.
 --curdir
 --ruby=/Users/mariozig/.rvm/rubies/ruby-2.2.1/bin/$(RUBY_BASE_NAME)
 --help
 --clean
 --use-system-libraries
 --enable-static
 --disable-static
 --with-zlib-dir
 --without-zlib-dir
 --with-zlib-include
 --without-zlib-include=${zlib-dir}/include
 --with-zlib-lib
 --without-zlib-lib=${zlib-dir}/lib
 --enable-cross-build
 --disable-cross-build
/Users/mariozig/.rvm/gems/ruby-2.2.1/gems/mini_portile-0.6.2/lib/mini_portile.rb:279:in `block in execute': Failed to complete configure task (RuntimeError)
 from /Users/mariozig/.rvm/gems/ruby-2.2.1/gems/mini_portile-0.6.2/lib/mini_portile.rb:271:in `chdir'
 from /Users/mariozig/.rvm/gems/ruby-2.2.1/gems/mini_portile-0.6.2/lib/mini_portile.rb:271:in `execute'
 from /Users/mariozig/.rvm/gems/ruby-2.2.1/gems/mini_portile-0.6.2/lib/mini_portile.rb:66:in `configure'
 from /Users/mariozig/.rvm/gems/ruby-2.2.1/gems/mini_portile-0.6.2/lib/mini_portile.rb:109:in `cook'
 from extconf.rb:278:in `block in process_recipe'
 from extconf.rb:177:in `tap'
 from extconf.rb:177:in `process_recipe'
 from extconf.rb:487:in `<main>'
extconf failed, exit code 1
Gem files will remain installed in /Users/mariozig/.rvm/gems/ruby-2.2.1/gems/nokogiri-1.6.6.2 for inspection.
Results logged to /Users/mariozig/.rvm/gems/ruby-2.2.1/extensions/x86_64-darwin-14/2.2.0/nokogiri-1.6.6.2/gem_make.out

The Problem

For some reason Apple’s Yosemite version of OSX does not have a system accessible installation of libxml2.  Nokogiri requires this in order to compile and luckily Xcode has a version of libxml2 bundled with it — we just need to specify it when installing the gem.  It’s important to get Nokogiri installed correctly because as of right now Rails 4.2.1.rc4 automatically attempts to install it and you will feel pain.

How I Solved It

gem install nokogiri -- --use-system-libraries=true --with-xml2-include=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/libxml2/

A Little Back Story…

Migrating from MySQL to PostgreSQL is not as straight forward as it sounds.  As you may or may not know, MySQL’s ‘mysqldump’ utility comes with a “compatible” option where you can specific “postgresql” but the catch is this:  the resulting SQL is not properly escaped and will need it’s data types augmented.

In other words… or in my words, the SQL generated from mysqldump that’s meant to be compatible with postgresql is basically worthless in it’s exported state.

 

How I Migrated

Thanks to people smarter than me, (specifically Max Lapshin) the process of dealing with the above is pretty straight forward with the help of mysql2postgres!

Here is what I did to migrate my MySQL data —  a few million rows clocking in just under 1 gig.

 

Step 1:
Install the mysql2postgres gem.

Before you just go and clone the gem to your local system, take a look @ the forks as they might have something interesting you want.  At the time of this writing, maxlapshin’s repo had like 5 or 6 outstanding pull requests.  My example code will just use his repo as the clone source.

 

Step 2:
Configuration

After installation, the ‘mysql2psql’ command should be available.  If it’s not, just close your terminal, open a new one and navigate back to where you cloned the gem.

By default you won’t have a configuration file.  Run the ‘mysql2psql’ command and it will generate a default one for you.  Once you do that, just open it up and configure it to your heart’s desires.

 

Once you open the configuration file, just read it top to bottom.  Everything is commented and it’s very straight forward stuff.  Fill in your MySQL details, then fill in your postgres details (or use the file: option).

Last part is to run the migration.

Step 3:
Run the Migration

This part will *probably* be as simple as doing this:

 

If that worked for you then enjoy the sweet sweet glory of being migrated.  Go check your data, utf8 encodings and all that fun stuff.

If that did not work out and you got an error like the following… then you might be using a mac, along with homebrew and can easily fix it!

Le Error

The Fix (aka How to Install mysql-connector-c on a mac)

  1. FIRST get rid of homebrew’s mysql-connector-c with: ‘brew remove mysql-connector-c’
  2. Go to MySQL’s download page and grab the Connector/C for your flavor of OSX
  3. Extract it and do the following:
    1. Copy the lib/ files to /usr/local/lib
    2. Copy the include/ files to /usr/local/include
    3. Copy the bin/ files to /usr/local/bin
  4. Profit.

Note: I’m still not fully sure why this fixed it for me.  The version i install from MySQL is the same version Homebrew uses. :/

 

To start off, the friendly_id gem allows your models to have “slugs” which ultimately help when you want your urls to appear more descriptive. For example instead of site.com/cars/43  you will get something like: site.com/cars/slug-name .

It’s better for SEO and IMO provides a better experience for situations where someone copies a link and sends it to another individual.  When you see a descriptive URL you kind of know what to expect and it feels less threatening than site.com/?blah=92349&whatever=5959 ….. OR i’m just a wussy.

My current situation is this:
I have some STI models that use inheritance and want to use friendly_id with it.

The problem i ran into and erroneously thought was a bug can be found here:
https://github.com/norman/friendly_id/issues/167 .  Basically when creating records for inheriting models i was getting duplicate slug errors.  Something like: “Error: Duplicate entry blah blah”.

A quick answer the gem’s author, Norman, rained light and joy on where i had gone wrong.

The Solution:

So how do you get STI models and friendly_id to work together?  This gist says it all and uses the idea of cars/brands as the STI model.  Note: this is really only for example, if you have a lot of cars and brands you will probably want to model that differently.

Read the docs:

The gem’s docs are really good.  While the docs for the FriendlyId::Scoped module explain everything in concise terms here they do not explicitly cover STI so hopefully this helps you out if you’re in the same situation.

I wanted to play around with creating my own gems so today I decided to try the jeweler gem.

Install went fine:

io@love:~$ sudo gem install jeweler
Successfully installed jeweler-1.4.0
1 gem installed
Installing ri documentation for jeweler-1.4.0...
Installing RDoc documentation for jeweler-1.4.0...

But when i attempted to create “the perfect gem” I ran into a bit of a problem:

io@love:~/ruby/gem_test$ jeweler the-perfect-gem
/usr/lib/ruby/gems/1.8/gems/git-1.2.5/lib/git/lib.rb:700:in `command': git config "--global" "--list"  2>&1:fatal: unable to read config file /home/io/.gitconfig: No such file or directory (Git::GitExecuteError)
	from /usr/lib/ruby/gems/1.8/gems/git-1.2.5/lib/git/lib.rb:672:in `command_lines'
	from /usr/lib/ruby/gems/1.8/gems/git-1.2.5/lib/git/lib.rb:352:in `global_config_list'
	from /usr/lib/ruby/gems/1.8/gems/git-1.2.5/lib/git.rb:148:in `global_config'
	from /usr/lib/ruby/gems/1.8/gems/jeweler-1.4.0/bin/../lib/jeweler/generator/options.rb:15:in `initialize'
	from /usr/lib/ruby/gems/1.8/gems/jeweler-1.4.0/bin/../lib/jeweler/generator/application.rb:9:in `new'
	from /usr/lib/ruby/gems/1.8/gems/jeweler-1.4.0/bin/../lib/jeweler/generator/application.rb:9:in `run!'
	from /usr/lib/ruby/gems/1.8/gems/jeweler-1.4.0/bin/jeweler:5
	from /usr/bin/jeweler:19:in `load'
	from /usr/bin/jeweler:19

And after some research… looks like i had not done some mandatory git setup steps… as i SHOULD have.

The solution…

io@love:~/ruby/gem_test$ git config --global user.name "iouser"
io@love:~/ruby/gem_test$ git config --global user.email "[email protected]"
io@love:~/ruby/gem_test$ git config --global github.user iouser

And finally the glory:

io@love:~/ruby/gem_test$ jeweler the-perfect-gem
	create	.gitignore
	create	Rakefile
	create	LICENSE
	create	README.rdoc
	create	.document
	create	lib
	create	lib/the-perfect-gem.rb
	create	test
	create	test/helper.rb
	create	test/test_the-perfect-gem.rb
Jeweler has prepared your gem in the-perfect-gem

Note: If you see the this error

io@love:~/ruby/gem_test$ jeweler the-perfect-gem
No github.user found in ~/.gitconfig. Please tell git about your GitHub account (see http://github.com/blog/180-local-github-config for details). For example: git config --global github.user defunkt

it means you forgot to do this part:

io@love:~/ruby/gem_test$ git config --global github.user iouser

I’ve seen a few people confused by this, and I was stumped back in the day SO:

FYI, if you want to install gems from GitHub you need to add it as a source!

How? Easy!

sudo gem sources -a http://gems.github.com

Now you can install any gem hosted on GitHub with something like:

sudo gem install yo-mama

One other thing to note. Gem names when installing from GitHub are formatted as: username-projectname

So if you were looking @ user “billybob” and he forked a gem/project called “fishface”… the way you install would be:

sudo gem install billybob-fishface

Ran into an interesting problem today that took me a bit to figure out. I need to automate some web tasks so of course I turned to Mechanize. When trying to install the gem on Ubuntu I ran into the following problem:

io@crazo:~$ sudo gem install mechanize
Building native extensions.  This could take a while...
ERROR:  Error installing mechanize:
	ERROR: Failed to build gem native extension.

/usr/bin/ruby1.8 extconf.rb install mechanize
checking for xmlParseDoc() in -lxml2... yes
checking for xsltParseStylesheetDoc() in -lxslt... no
checking for exsltFuncRegister() in -lexslt... no
checking for libxml/xmlversion.h in /opt/local/include,/opt/local/include/libxml2,/usr/include/libxml2,/usr/include,/usr/include/libxml2,/usr/local/include/libxml2... yes
checking for libxslt/xslt.h in /opt/local/include,/opt/local/include/libxml2,/usr/include/libxml2,/usr/include,/usr/include/libxml2,/usr/local/include/libxml2... no
need libxslt
*** 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
	--without-opt-dir
	--with-opt-include
	--without-opt-include=${opt-dir}/include
	--with-opt-lib
	--without-opt-lib=${opt-dir}/lib
	--with-make-prog
	--without-make-prog
	--srcdir=.
	--curdir
	--ruby=/usr/bin/ruby1.8
	--with-xml2lib
	--without-xml2lib
	--with-xsltlib
	--without-xsltlib
	--with-exsltlib
	--without-exsltlib


Gem files will remain installed in /usr/lib/ruby/gems/1.8/gems/nokogiri-1.1.0 for inspection.
Results logged to /usr/lib/ruby/gems/1.8/gems/nokogiri-1.1.0/ext/nokogiri/gem_make.out

This line threw a hint:
checking for libxslt/xslt.h in /opt/local/include,/opt/local/include/libxml2,/usr/include/libxml2,/usr/include,/usr/include/libxml2,/usr/local/include/libxml2… no

Anyways, after some Googling i found this post on Industry Zero and resolved the issue with a simple package installation:

io@crazo:~$ sudo apt-get install libxslt1-dev

After that, noooo problem installing mechanize and even resolved an issue i was having with nokogiri getting installed!

io@crazo:~$ sudo gem install mechanize
Building native extensions.  This could take a while...
Successfully installed nokogiri-1.1.0
Successfully installed mechanize-0.9.0
2 gems installed
Installing ri documentation for nokogiri-1.1.0...
Installing ri documentation for mechanize-0.9.0...
Installing RDoc documentation for nokogiri-1.1.0...
Installing RDoc documentation for mechanize-0.9.0...