When provisioning servers or writing automation scripts, manually running dpkg-reconfigure locales
becomes impractical. The interactive selection process breaks automation flows and complicates deployment pipelines. Here's a bulletproof method to handle this programmatically.
First, ensure locales package is installed:
sudo apt-get update && sudo apt-get install -y locales
Create a minimal locale configuration file (replace contents of /etc/locale.gen
):
echo "en_US.UTF-8 UTF-8" | sudo tee /etc/locale.gen
sudo locale-gen
Update the default locale configuration:
echo "LANG=en_US.UTF-8" | sudo tee /etc/default/locale
sudo update-locale LANG=en_US.UTF-8
To confirm the changes took effect:
# Check current locale
locale
# Verify /etc/default/locale contents
grep -E '^LANG=' /etc/default/locale
# Alternative verification
localectl status
For Docker containers or minimal installations where locale-gen
isn't available:
sudo apt-get install -y language-pack-en
sudo update-locale LANG=en_US.UTF-8
source /etc/default/locale
A complete implementation with error handling:
#!/bin/bash
set -e
# Install prerequisites
apt-get update && apt-get install -y locales
# Configure locale
echo "en_US.UTF-8 UTF-8" > /etc/locale.gen
locale-gen en_US.UTF-8
update-locale LANG=en_US.UTF-8
# Verification
if ! grep -q "LANG=en_US.UTF-8" /etc/default/locale; then
echo "Locale configuration failed!" >&2
exit 1
fi
echo "Locale configured successfully"
- If changes don't reflect, try logging out and back in or rebooting
- For SSH sessions, ensure
AcceptEnv LANG LC_*
is set in sshd_config - Check existing environment variables with
env | grep -E 'LC_|LANG'
When automating server deployments or container builds, interactive commands like dpkg-reconfigure locales
become problematic. We need a fully scriptable solution that:
- Installs required packages without prompts
- Configures
en_US.UTF-8
as the exclusive locale - Handles all configuration files properly
Here's the robust implementation I've tested across Debian/Ubuntu versions:
#!/bin/bash
# Install locales package non-interactively
DEBIAN_FRONTEND=noninteractive apt-get install -y locales
# Configure locales
echo "en_US.UTF-8 UTF-8" > /etc/locale.gen
locale-gen en_US.UTF-8
# Set system-wide default
update-locale LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8
# Clean up other locale settings
sed -i '/^[^#]/d' /etc/locale.gen
Add these checks to your script to validate the configuration:
# Verify locale generation
if ! locale -a | grep -q "en_US.utf8"; then
echo "Locale generation failed" >&2
exit 1
fi
# Check /etc/default/locale
if ! grep -q '^LANG=en_US.UTF-8$' /etc/default/locale || \
[ $(grep -v '^#' /etc/default/locale | wc -l) -ne 1 ]; then
echo "Locale configuration incomplete" >&2
exit 1
fi
For container environments, this optimized version works best:
FROM debian:bullseye
RUN apt-get update && \
DEBIAN_FRONTEND=noninteractive apt-get install -y locales && \
echo "en_US.UTF-8 UTF-8" > /etc/locale.gen && \
locale-gen en_US.UTF-8 && \
update-locale LANG=en_US.UTF-8 && \
rm -rf /var/lib/apt/lists/*
ENV LANG=en_US.UTF-8 \
LANGUAGE=en_US:en \
LC_ALL=en_US.UTF-8
Missing locale-gen: Ensure the locales
package is installed before running locale-gen
Partial configuration: The update-locale
command sometimes doesn't update all files - manually verify /etc/environment
if needed
Service restarts: Some services may need restarting after locale changes, particularly those running in init systems