Sign up ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Usually, I run aptitude -y install locales then dpkg-reconfigure locales to set up locale.

Now I want to put it into a shell script, how can I reliably do the following, automatically / non-interactively?

  • Choose en_US.UTF-8 and set it as system default
  • Disable all other locales
  • (Optional) Verify if /etc/default/locale contains one-and-only entry of LANG=en_US.UTF-8 as expected
share|improve this question

3 Answers 3

up vote 2 down vote accepted

See locale-gen:

 locale-gen --purge en_US.UTF-8


 echo -e 'LANG="en_US.UTF-8"\nLANGUAGE="en_US:en"\n' > /etc/default/locale
share|improve this answer
Thanks, but I'm getting File "/usr/share/apt-listchanges/", line 32, in <module> sys.stderr.write(_("Can't set locale; make sure $LC_* and $LANG are correct!\n"))... – kenn Feb 23 '12 at 8:02
Try LANG=en_US.UTF-8 locale-gen --purge en_US.UTF-8 – Stone Feb 23 '12 at 9:36
This morning, with a clean state of VM, it worked! Thanks! – kenn Feb 23 '12 at 19:30
Hmm… when you run locale-gen --purge en_GB.UTF-8 on a clean installation, you get this unexpected output: Generating locales (this might take a while)... en_US.UTF-8... done Generation complete. Any ideas how to set the list of locales as you do in dpkg-reconfigure locales non-interactively? – TwoWholeWorms Aug 1 '14 at 16:14

To reconfigure the timezone and locales non-interactively, from within a script, here is what works for me (under Debian):

  • For configuring the timezone, I first create '/etc/localtime' as a soft link to the appropriate zoneinfo file under the '/usr/share/zoneinfo' directory. Then, I run the dpkg-reconfigure command, and everything will be put in place. So, for instance, to set up timezone 'Europe/Brussels':

    ln --force --symbolic "${ZONEINFO_FILE}" '/etc/localtime'
    dpkg-reconfigure --frontend=noninteractive tzdata

    (Note that the AREA is a subdirectory under '/usr/share/info', and the ZONE is a file under the AREA subdirectory).

  • For configuring the locales, I first run a sed script that will create a new copy of the '/etc/locale.gen' file, based on the contents of the '/usr/share/i18n/SUPPORTED' file. Every line from the input file will be copied, but it will be turned into a comment unless it is an entry of a UTF-8 locale for a language that I wish to make available on my system (e.g., English, Dutch, French, and German):

    sed --regexp-extended --expression='
       1  {
    # This file lists locales that you wish to have built. You can find a list\
    # of valid supported locales at /usr/share/i18n/SUPPORTED, and you can add\
    # user defined locales to /usr/local/share/i18n/SUPPORTED. If you change\
    # this file, you need to rerun locale-gen.\
       /^(en|nl|fr|de)(_[[:upper:]]+)?(\.UTF-8)?(@[^[:space:]]+)?[[:space:]]+UTF-8$/!   s/^/# /
    ' /usr/share/i18n/SUPPORTED >  /etc/locale.gen

    Next, I set the default environment locale in the debconf database, e.g., to British English:

    debconf-set-selections <<< 'locales locales/default_environment_locale select en_GB.UTF-8'

    I subsequently remove the existing '/etc/default/locale' file (just to make sure that its old contents will not interfere with my new settings), and run the dpkg-reconfigure command to generate all of the locales that the sed script selected, and to create a new '/etc/default/locale' file with just an entry to set the 'LANG' variable to my selected default environment locale:

    rm --force --verbose /etc/default/locale
    dpkg-reconfigure --frontend=noninteractive locales

    Then, depending on my requirements, I may want to run a few update-locale commands, to override, e.g., the variables that affect the formatting of values, and set them to a different locale (such as Irish English):

    update-locale LC_NUMERIC='en_IE.UTF-8'
    update-locale LC_TIME='en_IE.UTF-8'
    update-locale LC_MONETARY='en_IE.UTF-8'
    update-locale LC_PAPER='en_IE.UTF-8'
    update-locale LC_NAME='en_IE.UTF-8'
    update-locale LC_ADDRESS='en_IE.UTF-8'
    update-locale LC_TELEPHONE='en_IE.UTF-8'
    update-locale LC_MEASUREMENT='en_IE.UTF-8'
    update-locale LC_IDENTIFICATION='en_IE.UTF-8'

    (I could have specified all of these parameters on a single invocation of the update-locale command, but apparently, the order in which the entries get written to the '/etc/default/locale' file is unpredictable if I do so. I prefer them to always be in the same order, which is why I generate them one by one.)

    And finally, I may want to run the update-locale command one last time, to set up the LANGUAGE variable (i.e., the list of languages in which I want translatable text messages to get displayed):

    update-locale LANGUAGE='en_GB:en_US:en'

Throw all this together into a bash script, and you can easily reconfigure your locale settings with a simple run of the script, which will not require any further user interaction.

In my experience, however, the new settings will not become entirely activated until after you reboot your system twice (at least on Debian Jessie Xfce). After the first reboot, the login manager will take the new settings alright, but after you login, your user session will continue to use the old settings; then, after the second reboot, your user session will take the new settings as well.

share|improve this answer

Could not get the answer above to work. Instead, I use this method (for Dockerfiles):

# Configure timezone and locale
echo "Europe/Oslo" > /etc/timezone && \
    dpkg-reconfigure -f noninteractive tzdata && \
    sed -i -e 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen && \
    sed -i -e 's/# nb_NO.UTF-8 UTF-8/nb_NO.UTF-8 UTF-8/' /etc/locale.gen && \
    echo 'LANG="nb_NO.UTF-8"'>/etc/default/locale && \
    dpkg-reconfigure --frontend=noninteractive locales && \
    update-locale LANG=nb_NO.UTF-8
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.