Search code examples
linuxperllocale

Perl fails to set locale even though it is installed


I am having trouble running Perl on a Linux system (Ubuntu):

user@Box:~$ perl -e exit
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
    LANGUAGE = "en_US:en",
    LC_ALL = (unset),
    LC_CTYPE = "UTF-8",
    LANG = "en_DK.UTF-8"
are supported and installed on your system.
perl: warning: Falling back to a fallback locale ("en_DK.UTF-8").

Googling showed, that this is usually related to environment variables referring to a missing locale, however all locales seem to be there:

user@Box:~$ locale -a
locale: Cannot set LC_CTYPE to default locale: No such file or directory
C
en_DK.utf8
en_GB.utf8
en_US
en_US.iso88591
en_US.utf8
POSIX

I have tried installing all of en, but that does not seem to affect anything.


Solution

  • Found the answer while writing the question:

    The culprit is LC_CTYPE=UTF-8, which is apparently perfectly valid in macOS (and Perl will accept it there), but not on Linux. To avoid it, one can override LC_CTYPE as follows:

    root@Box:~# update-locale LC_CTYPE=en_US.UTF-8
    

    After logging out and back in again, Perl will no longer complain.

    user@Box:~# perl -e 'print "Hack the Planet!\n"'
    Hack the Planet!