others-how to solve Cannot set LC_CTYPE to default locale: No such file or directory when using locale on Linux Systems ?

1. Purpose

In this post, I would demo how to solve ‘Cannot set LC_CTYPE to default locale: No such file or directory’ or ‘Cannot set LC_ALL to default locale: ?????????’ error when we change locale in Linux Systems.

2. Environment

  • Linux or MacOS

3. The problem

When we execute the ‘locale’ command to show all system languages , we got this:

[root@genuine-post ~]# locale
locale: Cannot set LC_CTYPE to default locale: No such file or directory
locale: Cannot set LC_ALL to default locale: ?????????
LANG=zh_CN.utf8
LC_CTYPE=UTF-8
LC_NUMERIC="zh_CN.utf8"
LC_TIME="zh_CN.utf8"
LC_COLLATE="zh_CN.utf8"
LC_MONETARY="zh_CN.utf8"
LC_MESSAGES="zh_CN.utf8"
LC_PAPER="zh_CN.utf8"
LC_NAME="zh_CN.utf8"
LC_ADDRESS="zh_CN.utf8"
LC_TELEPHONE="zh_CN.utf8"
LC_MEASUREMENT="zh_CN.utf8"
LC_IDENTIFICATION="zh_CN.utf8"
LC_ALL=

4. The solution

Just add the following line to the file: /etc/sysconfig/i18n :

LC_CTYPE="en_US.UTF-8"

Then we verify the change:

[root@genuine-post ~]# locale
LANG=zh_CN.utf8
LC_CTYPE=zh_CN.UTF-8
LC_NUMERIC="zh_CN.utf8"
LC_TIME="zh_CN.utf8"
LC_COLLATE="zh_CN.utf8"
LC_MONETARY="zh_CN.utf8"
LC_MESSAGES="zh_CN.utf8"
LC_PAPER="zh_CN.utf8"
LC_NAME="zh_CN.utf8"
LC_ADDRESS="zh_CN.utf8"
LC_TELEPHONE="zh_CN.utf8"
LC_MEASUREMENT="zh_CN.utf8"
LC_IDENTIFICATION="zh_CN.utf8"
LC_ALL=

5. How it works?

5.1 What is LC_CTYPE

LC_CTYPE This variable determines the locale category for character handling functions, such as tolower(), toupper() and isalpha(). This environment variable determines the interpretation of sequences of bytes of text data as characters (for example, single- as opposed to multi-byte characters), the classification of characters (for example, alpha, digit, graph) and the behaviour of character classes. Additional semantics of this variable, if any, are implementation-dependent.

LC_CTYPE is an override to LANG, and overrides just the character set used. All other features (categories) of LANG are still used as set by LANG.

5.2 What is the file /etc/sysconfig/i18n?

The /etc/sysconfig/i18n file sets the default language, any supported languages, and the default system font.

Here is the list of all the options of the /etc/sysconfig/i18n:

image-20210308135754240

6. Summary

So the key point is to tell the system how to convert from normal characters to localized characters, which can be controlled by the LC_CTYPE environment variable in the /etc/sysconfig/i18n.