Suffix

Ruby 1.9 server encoding

Configuring UTF-8 to fix incompatible character encodings.

Last week I tried to port an old Rails application form Ruby 1.8 to the latest 1.9. My development environment—on Mac OS X Snow Leopard—was running fine but the server just didn’t want to show the French version of the site. Each time I tried to switch the English locale to the French one it died with the following error:

Encoding::CompatibilityError: incompatible character encodings: UTF-8 and ISO-8859-1

It took me a while to find out what went wrong: the server wasn’t configured for UTF-8.

Finding the current locale

You can get the current locale on an Ubuntu box with the locale command:

LANG=
LC_CTYPE="en_US"
…
LC_IDENTIFICATION="en_US"
LC_ALL=

Installing missing locales

Now, assume the Ruby 1.9 application needs the UTF-8 locale. First, check if the needed locale is installed on the system with the locale -a command (shows a list with all available locales). If no UTF-8 locale is installed you can generate one with:

$ locale-gen en_US.UTF-8

To change the locale used by your system, modify the file /etc/default/locale to contain, for example:

LANG="en_US.UTF-8"

Reboot the system and there you go… Ruby 1.9 should now be smart enough to use UTF-8 strings by default.

Thanks @jbpros for pointing me in the right direction!