How do I reduce the size of locale-archive?

You can first remove all unneeded locales by doing:

$localedef --list-archive | grep -v -i ^en | xargs localedef --delete-from-archive

Where ^en can be replaced by the locale you wish to keep

Then

$build-locale-archive

If this gives you an error similar to

$build-locale-archive
/usr/sbin/build-locale-archive: cannot read archive header

Then try this

$mv /usr/lib/locale/locale-archive /usr/lib/locale/locale-archive.tmpl
$build-locale-archive

If that still fails, check your version. According to this page newer versions don't have the necessary files to rebuild the archive in order to save space. You'll need to run

yum reinstall glibc-common

In later releases of Red Hat Enterprise Linux, you may use dnf, a similar application.


On Amazon Linux

  1. Delete the unused archives from the locale-archive. This command deletes all except the ones beginning with en.

    localedef --delete-from-archive $(localedef --list-archive | grep -v -i ^en | xargs)
    
  2. Copy the locale-archive (with removed locales) file to be the new locale-archive.tmpl file

    cp /usr/lib/locale/locale-archive /usr/lib/locale/locale-archive.tmpl
    
  3. Run build-locale-archive

    build-locale-archive
    

Note: If you run build-locale-archive it zeros out the .tmpl file after each run and when you run it again you will see the error:

build-locale-archive: cannot read archive header

You can get a copy of the locale-archive.tmpl file either by copying the current locale-archive to locale-archive.tmpl or by downloading and unpacking glibc-common. If you use the default locale-archive.tmpl file from the glibc-common RPM and run build-locale-archive against it, it will create a full (~102MB) locale-archive. You need to repeat the above steps to get the smaller size archive.

In the above case deleting all locales except the en_* ones my locale-archive went from 102MB down to 3.4MB