Skip to content

How to Fix Linux Locale Errors (locale: Cannot set LC_ALL)

DodaTech 2 min read

In this tutorial, you'll learn about How to Fix Linux Locale Errors (locale: Cannot set LC_ALL). We cover key concepts, practical examples, and best practices.

The Problem

You run a command and get locale: Cannot set LC_ALL to default locale: No such file or directory or perl: warning: Setting locale failed.. Applications like perl, python, ssh, and postfix show locale warnings because the requested locale is not generated on the system. This is common on minimal Docker containers, fresh Ubuntu/Debian installations, or systems configured with a locale that wasn't generated.

Quick Fix

1. Check current locale settings

locale

Expected output with errors:

locale: Cannot set LC_ALL to default locale: No such file or directory
LANG=en_US.UTF-8
LC_CTYPE=en_US.UTF-8
LC_NUMERIC="en_US.UTF-8"
...
LC_ALL=en_US.UTF-8

2. Generate missing locales

# Generate a specific locale
sudo locale-gen en_US.UTF-8

# Regenerate all locales from /etc/locale.gen
sudo locale-gen

3. On Debian/Ubuntu, reconfigure locales

sudo dpkg-reconfigure locales

Select en_US.UTF-8 from the list.

4. Set locale environment variables

System-wide:

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

Per-user (add to ~/.bashrc):

echo 'export LANG=en_US.UTF-8' >> ~/.bashrc
echo 'export LC_ALL=en_US.UTF-8' >> ~/.bashrc
source ~/.bashrc

5. Fix locale in SSH sessions

Edit /etc/ssh/sshd_config:

AcceptEnv LANG LC_*

Then restart SSH:

sudo systemctl restart sshd

6. Install the locales package (if missing entirely)

# Ubuntu / Debian
sudo apt-get install locales

# RHEL / CentOS
sudo yum install glibc-common

# Docker Alpine
RUN apk add --no-cache musl-locales

7. Set locale in a Dockerfile

FROM ubuntu:22.04
ENV LANG=en_US.UTF-8
ENV LC_ALL=en_US.UTF-8
RUN apt-get update && apt-get install -y locales && \
    locale-gen en_US.UTF-8 && \
    update-locale LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8

Common Causes

Cause Error Message Fix
Missing locale generation Cannot set LC_ALL to default locale sudo locale-gen en_US.UTF-8
Locale package not installed locale: command not found sudo apt-get install locales
SSH client sends unsupported locale perl: warning: Setting locale failed Set SendEnv LANG in ~/.ssh/config to supported value
Docker image has no locales Error in container builds Set ENV LANG=en_US.UTF-8 in Dockerfile
Corrupted locale archive Cannot read locale archive sudo locale-gen --purge

Prevention

  • Set LANG=en_US.UTF-8 in Dockerfiles: ENV LANG=en_US.UTF-8
  • Add locale-gen en_US.UTF-8 && update-locale to server provisioning scripts
  • Configure SSH to not send locale environment variables if the server doesn't support them
  • In Dockerfiles, use ENV LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 immediately after the RUN locale-gen command
  • Use localectl status on systemd-based systems to inspect and set the system-wide locale settings

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro