Docker
Debian
Ubuntu
Locale
Containerization

How to set the locale in a Debian/Ubuntu Docker container

Codemia Knowledge Hub ·

In the Dockerfile, install the locales package, enable the locale you want in /etc/locale.gen, generate it with locale-gen, and set LANG, LANGUAGE and LC_ALL with ENV. Without this, applications fall back to a default locale such as C, which lacks the localized features below.

What the locale controls

A locale defines regional settings for software:

  1. Character encoding: text files and strings are encoded and decoded correctly, which matters for non-ASCII characters.
  2. Date and time formats: output matches the conventions of the region.
  3. Numeric and currency formats: numbers and currencies are presented the local way.
  4. Sorting and comparison: strings are sorted and compared by local linguistic rules.

Step 1: install the locales package

dockerfile
1# Use the latest Debian/Ubuntu image
2FROM ubuntu:latest
3
4# Install required packages
5RUN apt-get update && apt-get install -y locales

Step 2: generate the locale and set the environment

dockerfile
1# Enable en_US.UTF-8 in /etc/locale.gen, then generate it
2RUN sed -i '/en_US.UTF-8/s/^# //' /etc/locale.gen && locale-gen
3
4# Set the desired locale
5ENV LANG=en_US.UTF-8
6ENV LANGUAGE=en_US:en
7ENV LC_ALL=en_US.UTF-8
  • sed on /etc/locale.gen uncomments the en_US.UTF-8 UTF-8 line. This step is essential on Debian: Debian's locale-gen ignores its arguments and only builds the locales enabled in /etc/locale.gen, so locale-gen en_US.UTF-8 on its own generates nothing and every later RUN prints locale: Cannot set LC_CTYPE to default locale: No such file or directory. On Ubuntu locale-gen en_US.UTF-8 also works, but the sed version works on both.
  • locale-gen compiles the enabled locales.
  • ENV LANG=en_US.UTF-8 sets the default locale, both language and encoding.
  • ENV LANGUAGE=en_US:en controls the language of messages shown to users.
  • ENV LC_ALL=en_US.UTF-8 overrides LANG and every LC_* variable, which keeps behavior consistent.

Setting the ENV lines after generation avoids locale warnings from the generating RUN itself.

Step 3: verify the locale

dockerfile
# Verify the locale is set correctly
RUN locale

Running locale in the running container should show en_US.UTF-8 as the active setting, and locale -a should list en_US.utf8. Built this way, both debian:bookworm and ubuntu:24.04 list C, C.utf8, en_US.utf8 and POSIX.

Multiple locales and image size

  • To get several locales, uncomment each one in /etc/locale.gen (or append lines such as de_DE.UTF-8 UTF-8) before running locale-gen.
  • Configure locales in the Dockerfile, not by hand in a running container, so the settings persist across container restarts.
  • After configuring locales, remove packages and files you no longer need to keep the image small.

Locale commands

CommandDescription
locale -aLists all available locales on the system.
locale-genGenerates the locales enabled in /etc/locale.gen. On Ubuntu, locale-gen <locale> also works.
dpkg-reconfigure localesReconfigures and potentially regenerates all locales.
update-localeUpdates default system locale. Changes take effect after a system-wide re-login.

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design