Fix Rocky 9 dnf "Failed to set locale, defaulting to C.UTF-8"
Rocky 9 dnf warns "Failed to set locale, defaulting to C.UTF-8" when LANG or LC_ALL names a missing locale. Install a glibc-langpack or use C.UTF-8.
Failed to set locale, defaulting to C.UTF-8LANG (or LC_ALL) points to a locale such as en_US.UTF-8 or ko_KR.UTF-8, but that locale's data is not installed, so dnf fell back to C.UTF-8. The command still runs, so nothing is broken. Install the matching glibc-langpack-<code> or point LANG at a locale that exists, and the message goes away.
[1] List the installed locales. Containers and minimal images often have only C, C.utf8, and POSIX.
# locale -aCC.utf8POSIX# rpm -qa "glibc*langpack*" "glibc-minimal-langpack"glibc-minimal-langpack-2.34-83.el9.7.x86_64With only glibc-minimal-langpack, no other language's locale exists. Red Hat's docs explain that a regular install (Anaconda) includes glibc-all-langpacks, which carries every locale, while space-conscious systems such as containers and cloud images install only the needed glibc-langpack-<code> packages (RHEL langpacks docs).
[2] Confirm the values behind the warning. Look at LC_ALL and LC_* as well as LANG: the locale is taken from LC_ALL, then the individual LC_* variables, then LANG, with the first one winning (locale(7)). If LC_ALL names a missing locale, fixing LANG leaves the warning in place.
# LANG=C.UTF-8 LC_ALL=ko_KR.UTF-8 bash -c "env | grep -E \"^(LANG|LC_)\" | sort"bash: warning: setlocale: LC_ALL: cannot change locale (ko_KR.UTF-8)LANG=C.UTF-8LC_ALL=ko_KR.UTF-8In a real shell, just run env | grep -E '^(LANG|LC_)'. Running locale in the same shell shows exactly which categories fail.
# LANG=ko_KR.UTF-8 locale 2>&1 | head -4locale: Cannot set LC_CTYPE to default locale: No such file or directorylocale: Cannot set LC_MESSAGES to default locale: No such file or directorylocale: Cannot set LC_ALL to default locale: No such file or directoryLANG=ko_KR.UTF-8Find where LANG and LC_ALL come from: /etc/locale.conf, ~/.bashrc/~/.bash_profile, an ENV line in the Dockerfile, or the environment of a cron job or service unit. Over SSH, a client-sent LANG only arrives if the server accepts it, and Rocky 9's default openssh-server configuration had no AcceptEnv line (checked on openssh-server-9.9p1).
# grep -r AcceptEnv /etc/ssh/sshd_config /etc/ssh/sshd_config.d/[3] Install the langpack for the language you use. dnf provides tells you which package holds the locale.
# dnf -q provides "*/locale/ko_KR.utf8/LC_CTYPE" 2>&1 | head -5glibc-langpack-ko-2.34-266.el9_8.x86_64 : Locale data for KoreanRepo : baseosBefore installing, preview the change with --assumeno; it installs nothing. A langpack must match the glibc version, so if the system glibc is older than the repository's, glibc itself is upgraded too (see Upgrading: below, 2.34-83 → 2.34-283).
# dnf --assumeno install glibc-langpack-ko 2>&1 | grep -E "^(Upgrading|Installing|Upgrade|Install|Operation)|glibc" | sed "s/ */ /g"Installing: glibc-langpack-ko x86_64 2.34-283.el9_8 baseos 356 kUpgrading: glibc x86_64 2.34-283.el9_8 baseos 2.0 M glibc-common x86_64 2.34-283.el9_8 baseos 300 k glibc-minimal-langpack x86_64 2.34-283.el9_8 baseos 27 kInstall 1 PackageUpgrade 3 PackagesOperation aborted.If glibc appears under Upgrading:, treat it on production servers as a glibc update and install during a maintenance window. If the list is acceptable, install.
# dnf -y -q install glibc-langpack-ko glibc-langpack-enUpgraded: glibc-2.34-283.el9_8.x86_64 glibc-common-2.34-283.el9_8.x86_64 glibc-minimal-langpack-2.34-283.el9_8.x86_64Installed: glibc-langpack-en-2.34-283.el9_8.x86_64 glibc-langpack-ko-2.34-283.el9_8.x86_64If you need every language, glibc-all-langpacks covers them all. For container images, installing only the languages you need keeps them smaller.
[4] Check again. The locales exist and the same LANG no longer triggers the warning.
# locale -a | grep -Ei "^(en_us|ko_kr)"en_USen_US.iso885915en_US.utf8ko_KR.euckrko_KR.utf8# LANG=ko_KR.UTF-8 dnf -q repolistrepo id repo nameappstream Rocky Linux 9 - AppStreambaseos Rocky Linux 9 - BaseOSextras Rocky Linux 9 - Extras[5] If you cannot install packages, switch to a locale that exists. C.UTF-8 is always present and runs without the warning. Messages stay in English, but UTF-8 text is handled normally.
# LANG=C.UTF-8 dnf -q repolistrepo id repo nameappstream Rocky Linux 9 - AppStreambaseos Rocky Linux 9 - BaseOSextras Rocky Linux 9 - ExtrasIf [2] showed LC_ALL set, changing LANG alone leaves the warning.
# LANG=C.UTF-8 LC_ALL=ko_KR.UTF-8 dnf -q repolistFailed to set locale, defaulting to C.UTF-8repo id repo nameappstream Rocky Linux 9 - AppStreambaseos Rocky Linux 9 - BaseOSextras Rocky Linux 9 - ExtrasFor a one-off test, override with LC_ALL. It takes precedence over everything, so the warning goes away whatever the other LC_* values are.
# LC_ALL=C.UTF-8 dnf -q repolistrepo id repo nameappstream Rocky Linux 9 - AppStreambaseos Rocky Linux 9 - BaseOSextras Rocky Linux 9 - ExtrasFor a permanent fix, find where LC_ALL is set (export LC_ALL= or ENV LC_ALL= in the files from [2]), remove it or point it at an existing locale, then set LANG. LC_ALL overrides every other setting, so it is better kept out of everyday config files. In the test, setting only LC_CTYPE to a missing locale did not make this dnf warn, but other programs may differ, so clean up LC_* too. In a Dockerfile use ENV LANG=C.UTF-8; for an account, fix the export LANG= line in ~/.bashrc. The system-wide default is LANG= in /etc/locale.conf (the tested image had LANG="C.UTF-8"). On a real server localectl set-locale LANG=en_US.UTF-8 edits that file, but containers without systemd have no localectl.
If dnf stops on a repository error instead, see dnf Failed to download metadata; if it cannot find a package name, see dnf Unable to find a match.