Safely Change the System Locale with update-locale
You will change a system-wide locale setting in /etc/locale.conf, check the resulting file, and know how to reverse the change. The examples use update-locale from the Ubuntu locales package, version 2.39-0ubuntu8.9 installed on this machine.
The route
Jump straight to the step you need, or tick off Done means at the end.
Allow about ten minutes. You need a shell, the locales package, and sudo access for the final write to /etc/locale.conf. The examples first use a disposable copy, so the investigation and rehearsal do not need elevated privileges.
Warning
This is persistent configuration. A locale change can affect later shell sessions, services and programs that read the file at startup. Keep the old value before changing it, and make the final edit during a suitable maintenance window if other users or services depend on it.
1. Check the installed command
Confirm which executable and package version you are using. This is read-only and does not need sudo:
$ command -v update-locale
/usr/sbin/update-locale
$ dpkg-query -W -f='${Package} ${Version}\n' locales
locales 2.39-0ubuntu8.9
$ update-locale --help
Usage: update-locale [OPTIONS] [LANG=locale] [LC_NUMERIC=locale] ...
The installed help is the useful contract here. It supports variable assignments such as LANG=C.UTF-8, a variable name with no assignment to remove it, --reset, --locale-file=FILE and --no-checks.
2. Inspect the current configuration
Read the target before editing it. On this machine, /etc/default/locale is a symbolic link to the same file, but /etc/locale.conf is the file named by the command's manual:
$ ls -l /etc/locale.conf /etc/default/locale
-rw-r--r-- 1 root root 17 ... /etc/locale.conf
/etc/default/locale -> ../locale.conf
$ sudo cat /etc/locale.conf
LANG=en_US.UTF-8
Use the actual output from your host as the baseline. Do not assume that a locale is installed merely because its name looks familiar. locale -a lists the locale names available to the local runtime.
Checkpoint
Copy the current file somewhere safe before the real edit:
$ sudo cp --preserve=all /etc/locale.conf /etc/locale.conf.update-locale-backup
$ sudo cat /etc/locale.conf.update-locale-backup
3. Rehearse against a disposable file
Make a temporary copy and ask update-locale to edit that path. This exercises the same file format without touching the live configuration:
$ testdir=$(mktemp -d)
$ cp /etc/locale.conf "$testdir/locale.conf"
$ update-locale --locale-file="$testdir/locale.conf" LANG=C.UTF-8 LANGUAGE
$ cat "$testdir/locale.conf"
LANG=C.UTF-8
The first argument assigns LANG. The bare LANGUAGE argument removes that variable from the file. Existing unrelated settings remain when you do not use --reset. The disposable directory is under /tmp; remove it after checking the result if your normal temporary-file policy requires that.
4. Choose the update you actually want
For a normal change, pass each assignment explicitly. Multiple assignments can be made in one invocation:
$ sudo update-locale LANG=en_GB.UTF-8 LC_TIME=en_GB.UTF-8
$ sudo cat /etc/locale.conf
LANG=en_GB.UTF-8
LC_TIME=en_GB.UTF-8
Only name variables that you intend to remove. For example, this keeps LANG and removes LANGUAGE:
$ sudo update-locale LANG=en_GB.UTF-8 LANGUAGE
Do not add --reset casually. It clears variables not supplied on the command line. On the installed command, an existing omitted entry is written as a commented line, such as #LC_TIME=C, rather than remaining active. That is a broader change than updating one variable and may surprise programs or people inspecting the file.
5. Verify the file and the active shell
Read the resulting file first. This confirms what was persisted, but it does not change the environment of a shell that is already running:
$ sudo cat /etc/locale.conf
LANG=en_GB.UTF-8
LC_TIME=en_GB.UTF-8
$ locale
Start a new login session, or launch a child process with the intended environment, before judging application behaviour. The locale command reports the current process environment, not a promise that every running service has re-read /etc/locale.conf. Restart only the affected service, using its normal service procedure, when its documentation requires a restart.
If locale -a does not list the requested locale, stop and resolve locale generation through your distribution's normal package configuration. Do not use --no-checks as a fix for a missing locale.
6. Handle validation failures
By default, sanity checks reject an invalid locale value and leave the target unchanged. Rehearse that failure safely:
$ update-locale --locale-file="$testdir/locale.conf" LANG=not_a_locale
*** update-locale: Error: invalid locale settings: LANG=not_a_locale
$ echo $?
255
$ cat "$testdir/locale.conf"
LANG=C.UTF-8
The exact diagnostic formatting can vary, but a non-zero status is the signal to inspect the error and file. --no-checks deliberately bypasses these sanity checks:
$ update-locale --locale-file="$testdir/locale.conf" --no-checks LANG=not_a_locale
$ cat "$testdir/locale.conf"
LANG=not_a_locale
This option is not a validation workaround. Use it only when you have independently verified that the value is valid for the consumer that will read it. An unchecked value can make later commands or services fail in less obvious places.
7. Undo an unwanted change
If the new file is wrong, restore the backup you made before the edit. This overwrites the live file, so check the source path carefully:
$ sudo diff -u /etc/locale.conf.update-locale-backup /etc/locale.conf
$ sudo cp --preserve=all /etc/locale.conf.update-locale-backup /etc/locale.conf
$ sudo diff -u /etc/locale.conf.update-locale-backup /etc/locale.conf
The final diff should produce no output. Remove the backup only after you have confirmed the restored configuration and no longer need it. If the change was already consumed by a service, restore the file and follow that service's normal reload or restart procedure.
Done means
- You checked the installed
localespackage and command syntax. - You inspected and backed up
/etc/locale.conf. - You rehearsed the intended assignment with
--locale-fileon a disposable copy. - You changed only the variables you meant to change, without using
--resetaccidentally. - You verified the saved file and understand that existing processes may retain their old environment.
- You know that
--no-checksbypasses validation and have a tested backup for recovery.