02 Dec 2008

Ubuntu 8.04 Hardy "Can't set the locale" Error

Yesterday, I finally got around to upgrading my home server from Ubuntu 6.06 LTS (aka ‘Dapper Drake’) to the latest “long-term support” release, 8.04 LTS (‘Hardy Heron’).

Pretty much everything went according to plan. Since my server is headless, I was a bit nervous about the whole thing – having to attach a monitor and keyboard to it would have been a major problem. But this turned out to be unwarranted; the whole procedure was quite smooth.

The only issue I did run into was the dreaded “can’t set the locale; make sure $LC_* and $LANG are correct” problem, after I rebooted. This is a very common issue, and if you’re a Linux or BSD user and you haven’t run across it yet, chances are at some point you will. A quick search using Google will turn up hundreds of people looking for solutions.

Unfortunately it’s a nasty issue because there are many reasons why it can happen. In my case, none of the solutions suggested in most forum posts (run dpkg-reconfigure locale, check locale -a, etc.) worked. However, I did notice that when I looked at the current values of $LANG and $LC_ALL, they were incorrect.

In particular:

$ echo $LANG
en_US

This is wrong. The correct locale specifies a text encoding, so a proper value is en_US.UTF-8, not just en_US.

Unfortunately, it took me a long time to figure out where to set this value. Throwing it into my .bashrc would have solved the problem when I was logged in and running things as my user, but it wouldn’t have prevented it from cropping up when the root user’s cron tasks ran automatically (which results in me getting sent error emails every few minutes; pretty annoying).

What I wanted was to set LANG=en_US.UTF-8 as a global variable for the entire system, for all users, all the time, whether running interactively or not. In order to do this, the file /etc/environment must be edited. This file holds global variables that apply to the entire system: typically just the locale and a bare-minimum PATH.

To /etc/environment I added (the first line was present but specified “en_US”):

LANG="en_US.UTF-8"
LANGUAGE="en_US.UTF-8"
LC_ALL="en_US.UTF-8"

In order to get this to take effect, I had to restart all my open shells, including a few instances of screen. However, it made the problems go away.

This entry was converted from an older version of the site; if desired, it can be viewed in its original format.