Per-user

If you use Perl or other locale-dependent applications in CentOS, you may encounter confusing warnings like "perl: warning: Setting locale failed." Not having locales set properly leads to subtle issues down the line, so getting this resolved is important for a stable system.

What Are Locales and Why Do They Matter?

Locales are settings that define language, character encoding, date/time formats, and other regional preferences on a Linux system. They provide cultural context for software running on a machine so things like sorting, unicode mappings, and translations work properly across languages.

Many applications rely on correct locale settings to function smoothly, Perl being one example. Under the hood, the locale defines expected character encodings, so strings, sorting, and pattern matching work predictably. Other programs like Python, Ruby, awk, sed, and many more also depend on proper locales.

Analyzing the "perl: warning: Setting locale failed" Message

In the sample error message, we can see Perl detecting issues with the locale settings:

LANGUAGE = (unset)
LC_ALL = (unset)  
LC_CTYPE = "UTF-8"
LANG = "en_US.UTF-8"

The warning is triggered because the global LC_ALL variable is unset entirely, meaning no locale is defined. And while LC_CTYPE and LANG refer to valid locale definitions, the unset LC_ALL takes precedence. The result is Perl falls back to the POSIX locale, which lacks definitions for many character and language preferences Perl depends on.

Fixing Locale Issues in CentOS 8

1. Set Locale Variables

The simplest fix is to manually set the LC_CTYPE and LC_ALL variables in the shell before running Perl, which the sample demonstrates:

export LC_CTYPE=en_US.UTF-8
export LC_ALL=en_US.UTF-8 

This overrides the unset variables just for that session. en_US.UTF-8 explicitly defines the US English language and UTF-8 character encoding, which covers most common Unicode needs.

2. Set Locales Permanently

For a permanent fix, the locale variables can be set system-wide or on a per-user basis by adding them to shell profile scripts like /etc/profile or ~/.bash_profile:

# System-wide
echo ‘export LC_ALL=en_US.UTF-8‘ | sudo tee -a /etc/profile 

echo ‘export LC_ALL=en_US.UTF-8‘ >> ~/.bash_profile

This means the settings apply automatically in all new shells.

Best Practices for Locale Configuration

Beyond just fixing immediate locale issues, there are some best practices worth following:

  • Check that desired locales are actually installed on the system with localectl list-locales
  • Use UTF-8 locales as the default where possible for best Unicode support
  • Define a default locale rather than relying on the POSIX one to avoid surprises
  • Set locale at the global LC_ALL level instead of each sub-category like LC_CTYPE
  • Ensure applications use consistent locales instead of relying on different defaults

Properly configuring locales may take some initial effort, but saves you from subtle issues down the road. Perl and many other applications will thank you!