Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

bash warns that it cannot change the locale after you log in to a server

Errorbash: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8)

Short answer

The shell was told to use a locale that is not built on the server. Run locale -a to see what exists, generate the missing one with locale-gen, and set it with update-locale. If it only happens over SSH, your client is sending a locale the server does not have, so stop forwarding it.

  • Applies to Ubuntu 24.04, Debian 12, OpenSSH 9
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print which locale variables are set and which locales the machine can actually use. The one named in the warning must appear in the second list.

    locale
    locale -a
  2. 2Match what you see, then jump to its fix

    1. 1the name in the warning is missing from the output of locale -aThe locale was never generated on this serverFix
    2. 2the warning appears only after ssh login, and a local console login is cleanYour SSH client forwards a locale the server does not haveFix
    3. 3locale prints LC_ALL=something, and setting LANG does not change the resultLC_ALL is set by hand and overrides every other locale settingFix

What does the error look like?

  • bash: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8)

Cause 1 of 3

The locale was never generated on this server

1Check

Minimal server images ship only locale templates. A locale is compiled on the machine only after it is listed in /etc/locale.gen and locale-gen is run.

locale -a
grep -n "en_US.UTF-8" /etc/locale.gen

2Fix

Build the locale, then make it the system default.

sudo locale-gen en_US.UTF-8
sudo update-locale LANG=en_US.UTF-8

3Verify

Open a new session. The warning is gone and locale shows the new value.

locale -a | grep -i en_US
locale

Confirmed by manpages.ubuntu.com

Did this fix it?

Cause 2 of 3

Your SSH client forwards a locale the server does not have

1Check

Debian-based clients set SendEnv LANG LC_* in ssh_config and Debian-based servers set AcceptEnv LANG LC_* in sshd_config. The server copies the client's LANG, which names a locale it lacks.

echo "$LANG $LC_ALL"
grep -E "SendEnv|AcceptEnv" /etc/ssh/ssh_config /etc/ssh/sshd_config

2Fix

Either generate that locale on the server as in the first cause, or stop the client sending it for this host. A pattern with a leading minus clears variables an earlier SendEnv line set, so the Debian default does not apply to this host.

# ~/.ssh/config on your machine
Host my-server
    SendEnv -LANG -LC_*

3Verify

Log in again and the warning no longer prints.

ssh my-server locale

Confirmed by manpages.ubuntu.com

Did this fix it?

Cause 3 of 3

LC_ALL is set by hand and overrides every other locale setting

1Check

When LC_ALL is non-empty its value is used for every category, before any LC_* or LANG variable. A stale export in .bashrc or .profile keeps asking for a missing locale.

grep -n "LC_ALL" ~/.bashrc ~/.profile ~/.bash_profile /etc/environment /etc/default/locale 2>/dev/null

2Fix

Delete the line, or point it at a locale that exists, then start a new shell.

unset LC_ALL
# and remove the export LC_ALL=... line from the file grep found

3Verify

locale shows an empty LC_ALL and no warning is printed.

locale

Confirmed by manpages.ubuntu.com

Did this fix it?

How do I stop it coming back?

  • Generate the locales you use in your server provisioning script instead of fixing each machine by hand.
  • Use one explicit LANG, such as en_US.UTF-8 or C.UTF-8, across all servers.
  • Avoid exporting LC_ALL in shell startup files; set LANG instead.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.