6

I accidentally add a locale, e.g. sudo locale-gen zh_TW

e.g. locale -a

zh_TW

But how to remove it?

Ryan
  • 5,341
  • 21
  • 71
  • 87

5 Answers5

9

You can also reconfigure the locales package using dpkg-reconfigure locales.

Oliver
  • 5,883
  • 23
  • 32
6

I think you can do this by editing /var/lib/locales/supported.d/local and removing that locale. Then run locale-gen to regenerate the compiled locales.

mgorven
  • 30,036
  • 7
  • 76
  • 121
  • 2
    In my instance of Ubuntu 20.04 there is not such folder... – kovinet Dec 16 '21 at 08:12
  • In the man page for `locale-gen`, it says about `/var/lib/locales/supported.d/`: "A directory containing locale.gen snippets provided by language-pack packages. Do not edit these manually, they will be overwritten on package upgrades.". Is the `local` file you refer to a special case? Or do you know that the advice from the man page is not actually an issue in practice? – SpinUp __ A Davis Jul 13 '22 at 17:21
6

Which locales are installed on my machine?

You can check which locales are generated and configured on your system using the locale command:

  • locale ... list the current locale configuration
  • locale -a ... lists all all locales that were generated on your system
  • locale -a -v ... list all locales and show useful additional information (such as directory names that contain the locale information data files)

The last command from above makes you see that all generated locales are located in /usr/lib/locale/, and you may remove any of them if unneeded. Each pack of locale information is a directory containing text files and other directories.

-

Supported locales

All locales that you want your system to support are listed in the text files in /var/lib/locales/supported.d/. These files have two columns, language tag and character map.

I want my system to know US-English only, so I have only one file there, called en, which contains just a single line:

en_US UTF-8

-

Error messages

If error messages are displayed when issuing the locale command, e.g.

locale: Cannot set LC_ALL to default locale: No such file or directory

make sure the file /etc/default/locale exists and has proper content, such as:

LANG="en_US"
LANGUAGE="en_US:en"

-

Get rid of unneeded locale data - Step by step

Now we know all the necessary details to get started with cleaning up our system's locale information:

  1. Edit the locale file(s) in /var/lib/locales/supported.d/, and remove all unneeded locales (one locale per line)
  2. Create and edit /etc/default/locale (see above for an example)
  3. Delete all generated locale data: rm -rfv /usr/lib/locale/*
  4. Re-generate new locales: locale-gen

That's all! Reboot your machine to make your changes take effect. Then run one or more of the locale command examples from above to ensure yourself that the result is as expected.

-

Reference:

https://www.linuxquestions.org/questions/blog/bittner-195120/remove-unwanted-locales-on-ubuntu-debian-3281/

Paulo Coghi
  • 588
  • 1
  • 11
  • 22
  • 1
    Thanks for the most comprehensive answer. I think this one deserves as _the_ answer, @Ryan. – xpt Jun 29 '19 at 16:42
  • Thanks, @xpt! The merit goes to "bittner" on LinuxQuestions :) – Paulo Coghi Jul 01 '19 at 13:17
  • In the man page for `locale-gen`, it says about `/var/lib/locales/supported.d/`: "A directory containing locale.gen snippets provided by language-pack packages. Do not edit these manually, they will be overwritten on package upgrades.". Do you know that the advice from the man page is not actually an issue in practice? – SpinUp __ A Davis Jul 13 '22 at 17:26
4

Note: Some of the commands below require root privileges, consider the use of sudo.

Basic info

According to man locale-gen, locales are set in several files.

/etc/locale.gen

The main configuration file, which has a simple format: every line that is not empty and does not begin with a # is treated as a locale definition that is to be built.

/var/lib/locales/supported.d/

A directory containing locale.gen snippets provided by language-pack packages. Do not edit these manually, they will be overwritten on package upgrades.

Locales are compiled (generated) into a single file.

/usr/lib/locale/locale-archive

Usual default locale archive location.

Comprehensive details on locales at the Arch Wiki.

Checking locales and the locale

To list available (known) locales, run any of the following commands (with minor output differences).

locale -a
localectl list-locales

To check the (already) generated locales, run the following command.

localedef --list-archive

To check the currently used locale, run any of the following commands (with minor output differences).

locale
localectl

Setting and generating (new) locales

Locales are typically set by uncommenting lines in /etc/locale.gen, after which running locale-gen is required.

nano /etc/locale.gen # uncomment desired lines (locales)
locale-gen

This will compile (generate) locales into /usr/lib/locale/locale-archive for each uncommented line in /etc/locale.gen and under /var/lib/locales/supported.d/, whether they were previously compiled or not.

Alternatively, the command

locale-gen <locale>

will uncomment the corresponding line in locale-gen while generating the desired locale and only this one.

Note: The implementation of locale-gen is distro dependent. For instance, the command above is valid in Ubuntu/Debian but not in ArchLinux.

Removing locales

When issuing locale-gen, the compiled archive is erased and all locales in /etc/locale.gen and under /usr/lib/locale/locale-archive are regenerated anew. The command locale-gen --purge <locale> doesn't do what the modifier suggests but the opposite: It removes all compiled locales except those indicated. To make sure only specific locales are generated when locale-gen is issued or and update is performed both /etc/locale.gen and /usr/lib/locale/locale-archive must be considered.

To remove locales in /etc/locale.gen, simply comment the desired lines and regenerate the locales using locale-gen.

To remove locales under /var/lib/locales/supported.d/ is trickier. Since any file /var/lib/locales/supported.d/<code> depends on the package language-pack-<code>-base, any change on the former will be restored when the latter is updated. To solve this, simply hold the packages that update files under /var/lib/locales/supported.d/. The command that achieves this in Ubuntu/Debian is the following.

apt-mark language-pack-<code>-base

To update a held package, you must unmark it or simply --ignore-hold.

Workaround. A more intrusive but general solution that prevents changes under /var/lib/locales/supported.d/ is to set files in it with the "immutable (i)" attribute. So instead of removing files, empty them. For instance:

cd /var/lib/locales/supported.d/
rm <code> && touch <code> # <code> has been emptied
lsattr <code>    # regular attributes
chattr +i <code> # adding (+) immutable
lsattr <code>    # checking attributes

Setting the locale

Setting and generating locales does not set the system locale. Any of the following commands achieves this.

echo LANG=<code> | sudo tee /etc/locale.conf # reboot (might be ignored in Ubuntu)
localectl set-locale LANG=<code>
vitaminace33
  • 161
  • 4
  • Answer also given [here](https://askubuntu.com/a/1281631/762841). – vitaminace33 Oct 10 '20 at 11:50
  • 1
    Would you consider posting this as an answer to [this question](https://askubuntu.com/questions/477974)? It has higher precedence in google searches for me, but all the answers there recommend installing a new package. IMO your answer is superior as it's much more informative about the system. – SpinUp __ A Davis Jul 13 '22 at 16:42
  • I'll update the answer and do what you suggest. Thank you for the pointing it out and for your appreciation. – vitaminace33 Jul 14 '22 at 18:38
  • Following the suggestion of @SpinUp, the answer has also been proposed [here](https://askubuntu.com/questions/477974). – vitaminace33 Jul 16 '22 at 12:57
0

I am unsure why the most Distributions and Users are unaware from localepurge

For Debian based Systems, should it be available by

apt-get install localepurge

From the Manpage:

localepurge is a small script to recover disk space wasted for unneeded locale files and localized man pages. It will be automagically invoked by dpkg upon completion of any apt installation run.

You have to define the locale diretory names you want to keep from removal after each apt installation run in the /etc/locale.nopurge configuration file. Unless localepurge has been adequately configured, the system's localization files won't be touched at all.

The contents of following directories will be affected from removals:

 - /usr/share/doc/kde/HTML
 - /usr/share/gnome/help
 - /usr/share/locale
 - /usr/share/man
 - /usr/share/omf
 - /usr/share/tcltk
 - /usr/share/cups/{templates,locale,doc-root}
 - /usr/share/calendar
 - /usr/share/aptitude
 - /usr/share/help/<domain>/HELP_CONTENT(files&dirs)
 - /usr/share/vim/vim*/lang

The localization files you actually need and which you want to be preserved on your system can be easily configured by running the following command:

dpkg-reconfigure localepurge

Conclusion: This Script will take care of any unwanted locales at the moment and in the future.

djdomi
  • 1,377
  • 3
  • 10
  • 19