When executing administrative commands like sudo apt update, installing packages via dpkg, running Perl scripts, or logging into a remote server via SSH on Ubuntu, seeing the terminal warnings perl: warning: Setting locale failed., locale: Cannot set LC_CTYPE to default locale: No such file or directory, and Failed to set locale, defaulting to C.UTF-8 indicates that the system’s C library (glibc) cannot find the compiled locale definition files specified by your environment variables. While this warning does not typically halt package installations, it disrupts terminal character encoding, breaks multi-byte Unicode rendering, and produces repetitive diagnostic noise across command outputs. Resolving this issue involves compiling missing locale packages, configuring /etc/default/locale, and adjusting SSH client forwarding rules.
Understanding Linux Locale Architecture and glibc
In Linux, locales define language, character encoding, regional number formats, and date conventions for software. The GNU C Library (glibc) parses environment variables including LANG, LC_ALL, LC_CTYPE, and LC_MESSAGES, querying pre-compiled binary definitions in /usr/lib/locale/locale-archive. If a user environment requests a locale like en_US.UTF-8 or en_GB.UTF-8, but that specific definition was never compiled by locale-gen, glibc falls back to the minimalist emergency C.UTF-8 locale and prints this warning.
Common technical causes of locale fallback errors include:
- Uncompiled Language Definitions: Minimal cloud images (such as Ubuntu on AWS, DigitalOcean, or Docker containers) omitting pre-generated locales to minimise base image size. For cron issues, see our guide on fixing cron jobs not running in Ubuntu.
- SSH Client Locale Forwarding (SendEnv): Connecting from a macOS or Windows client via SSH where the client forwards a locale (such as
en_GB.UTF-8orUTF-8) absent on the remote Ubuntu server. For GPG key errors, check our guide on fixing GPG error NO_PUBKEY in Ubuntu. - Missing /etc/default/locale Configuration: The system lacking a persistently declared default system locale file. For trackpad glitches, see our guide on fixing touchpad not working in Ubuntu.
- Missing locales Package: The base Debian/Ubuntu
localesmanagement package being uninstalled or purged.
| Environment Variable | Operational Role | Typical Healthy Value |
|---|---|---|
LANG |
System default locale for unassigned variables | en_US.UTF-8 or en_GB.UTF-8 |
LC_ALL |
Master override that supersedes all other LC_* settings | Empty by default (set only for troubleshooting) |
LC_CTYPE |
Character classification and case conversion (Unicode) | Inherits from LANG |
C.UTF-8 |
Minimal built-in fallback POSIX locale | Active fallback when requested locale is missing |
Method 1: Install the locales Package and Generate Required Locales
Ensuring the official locales package is installed and compiling the specific required language definitions directly resolves the missing archive files.
- Open your terminal (press Ctrl + Alt + T or connect via SSH).
- Update your package repository list and install the locales package:
sudo apt update && sudo apt install -y locales - Generate the standard international and regional UTF-8 locales:
sudo locale-gen en_US.UTF-8 en_GB.UTF-8 - Compile the updated locale archive:
sudo dpkg-reconfigure locales - In the curses menu that appears, ensure en_US.UTF-8 (or your local preference) is selected with an asterisk (*), select OK, and set it as the default system locale.
Method 2: Set System-Wide Defaults via update-locale
Configuring the master locale definition file ensures all newly spawned subshells and daemons inherit valid environment variables.
- Run the official locale update command:
sudo update-locale LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 - Inspect the contents of the configuration file to confirm:
cat /etc/default/locale - The output should clearly display:
LANG=en_US.UTF-8LC_ALL=en_US.UTF-8 - Log out of your current terminal session and log back in to reload the environment.
Method 3: Prevent SSH Clients from Forwarding Unsupported Locales
If the error occurs exclusively when connecting to an Ubuntu server over SSH from macOS or Linux workstations, your local SSH client is pushing its local locale settings to the remote server.
- On the Ubuntu server, edit the SSH daemon configuration:
sudo nano /etc/ssh/sshd_config - Locate the line reading:
AcceptEnv LANG LC_* - Comment out the line by adding a hash symbol (
#) at the beginning:#AcceptEnv LANG LC_* - Save and exit the file (Ctrl + O, then Ctrl + X).
- Restart the SSH service:
sudo systemctl restart ssh - Disconnect and reconnect your SSH session. The client will no longer inject unrecognised locale parameters.
Method 4: Fix Locales in Docker Containers and Minimal Chroots
When building minimal Docker images or working inside chroot environments, adding the locale generation commands to your Dockerfile or setup script prevents build failures.
- In your Dockerfile, append the following sequence before running package installations:
RUN apt-get update && apt-get install -y locales && rm -rf /var/lib/apt/lists/* \&& locale-gen en_US.UTF-8ENV LANG=en_US.UTF-8ENV LANGUAGE=en_US:enENV LC_ALL=en_US.UTF-8 - For immediate shell sessions inside an existing container, execute:
export LANG=C.UTF-8export LC_ALL=C.UTF-8
Method 5: Verify Active Locale Configurations
Confirm that your shell now operates in a healthy, fully-defined locale environment.
- Execute the locale verification command:
locale - Inspect every variable in the list (
LANG,LC_CTYPE,LC_NUMERIC, etc.). Ensure none are enclosed in quotation marks indicating unverified fallbacks. - Run a test command:
sudo apt update - The package manager will refresh without generating a single Perl or locale warning.