Set up System Locale on Ubuntu 22.04: Easy Guide Steps

Posted on

Set up System Locale on Ubuntu 22.04: Easy Guide Steps

This tutorial aims to guide you on how to Set up System Locale on Ubuntu 22.04 from the Command line. You will also learn to change the default locale on the Ubuntu 22.04 server. The system locale defines the language and country-specific settings for the programs running on your system and your shell sessions. You can use locales to format time, date, numbers, currency, and other values according to your language or country. Follow the guide steps on the Orcacore website to Set up System Locale on Ubuntu 22.04.

To Set up System Locale on Ubuntu 22.04, you must have access to your server as a root or non-root user with sudo privileges. For this purpose, you can visit this guide on Initial Server Setup with Ubuntu 22.04.

Step 1 — Check Default System Locale on Ubuntu 22.04

First, you can check your current system locale on your Ubuntu server by using the command below:

locale
**Output**
LANG=en_US.UTF-8
LANGUAGE=
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=

Also, you can use the following command instead:

localectl status
**Output**
   System Locale: LANG=en_US.UTF-8
       VC Keymap: n/a
      X11 Layout: us
       X11 Model: pc105

Step 2 — Check Which Locales are Enabled on Ubuntu 22.04

You may have several locales set for different parts of your system. To check which locales are enabled on your Ubuntu server, run the command below:

locale -a
**Output**
C
C.utf8
en_US.utf8
POSIX

Step 3 — How To Generate a Locale on Ubuntu 22.04?

If you want to enable a system locale for the region you want, you can simply use the locale-gen command. Here we use the following command to select a locale and generate it on Ubuntu 22.04:

dpkg-reconfigure locales

Here as an example, we select the en_US.ISO-8859-1 and click OK.

Select desired system locale on Ubuntu

Then, it will ask you to select the default system locale on your Ubuntu 22.04. We select the en_US.UTF-8 and click OK.

Set up System Locale on Ubuntu 22.04

When it is finished, you will get the following output:

**Output**
Generating locales (this might take a while)...
  en_US.ISO-8859-1... done
  en_US.UTF-8... done

Finally, verify your system locale is enabled with the command below:

locale -a
**Output**
C
C.utf8
en_US
en_US.iso88591
en_US.utf8
POSIX

Step 4 — How To Change the Default Locale in the Ubuntu Server?

At this point, you can easily change the default locale manually. For this purpose, you need to edit the /etc/default/locale file, we use vi editor to open the file:

vi /etc/default/locale

The file has a single line that describes the default locale:

LANG=en_US.UTF-8

You can change the LANG with your desired locale. When you are done, save and close the file.

Finally, log out from your current session and then log back in, or open a new terminal, to activate the new system locale.

Also, there is an alternative method that you can update the locale file without editing it. You just run the following update-locale command to set your system locale:

update-locale LANG=<your-desired-locale>

Remember to restart the session to activate your locale.

For more information, you can see the man pages for locale commands:

# man locale
# man update-locale
# man localectl

Conclusion

At this point, you have learned to Set up System Locale on Ubuntu 22.04, Check the Default System Locale, Check Which Locales are Enabled, Generate a Locale, and Change the Default Locale.

Hope you enjoy it. You may also interested in these articles:

Netplan Gateway is Deprecated Use Default Routes on Ubuntu

Install Zabbix 6.4 on Ubuntu 22.04

Alternative Solutions for Setting Up System Locale on Ubuntu 22.04

While the article provides a comprehensive guide to Set up System Locale on Ubuntu 22.04 using the command line and dpkg-reconfigure and update-locale tools, here are two alternative approaches you can use to achieve the same goal:

1. Using localectl Command (Directly)

The localectl command, as shown in the original article for checking the system locale status, can also be used directly to set the system locale. This method offers a more streamlined approach compared to manually editing the /etc/default/locale file. It also handles the necessary system updates for the changes to take effect.

Explanation:

The localectl utility is part of the systemd suite and is designed to manage system locale settings. It directly interacts with the systemd locale configuration, ensuring consistency and proper integration with other system services.

Steps:

  1. Identify the desired locale: Use locale -a to list available locales.
  2. Set the system locale: Use the localectl set-locale command with the desired locale.
sudo localectl set-locale LANG=fr_CA.UTF-8

Replace fr_CA.UTF-8 with the locale you want to set.

  1. Verify the change: Use localectl status to confirm the new locale setting.
localectl status
  1. Restart the system or log out and back in: While localectl attempts to apply changes immediately, a reboot or re-login ensures that all processes pick up the new locale settings.

Advantages:

  • More direct and concise command.
  • Avoids manual file editing, reducing the risk of errors.
  • Integrates well with systemd.

Disadvantages:

  • Requires familiarity with the localectl command.

2. Using a Configuration Management Tool (Ansible)

For managing multiple Ubuntu 22.04 servers, using a configuration management tool like Ansible is a scalable and repeatable solution to Set up System Locale on Ubuntu 22.04. Ansible allows you to automate the process of setting the locale across multiple machines from a central location.

Explanation:

Ansible uses playbooks, written in YAML, to define the desired state of the system. These playbooks can then be applied to multiple servers simultaneously, ensuring consistent configurations.

Steps:

  1. Install Ansible on your control machine (if not already installed).
  2. Create an Ansible playbook (e.g., locale.yml):
---
- hosts: all
  become: true
  tasks:
    - name: Set system locale
      command: "localectl set-locale LANG={{ desired_locale }}"
      args:
        creates: /etc/locale.conf # Ensure idempotency
      notify:
        - restart_systemd_daemon

  handlers:
    - name: restart_systemd_daemon
      systemd:
        name: systemd-localed.service
        state: restarted

Explanation of the Playbook:

  • hosts: all: Specifies that the playbook will run on all hosts defined in your Ansible inventory.
  • become: true: Indicates that the tasks will be executed with root privileges (sudo).
  • tasks: A list of tasks to be executed.
    • name: Set system locale: A descriptive name for the task.
    • command: "localectl set-locale LANG={{ desired_locale }}": Executes the localectl command to set the locale. {{ desired_locale }} is a variable that will be defined later.
    • args: creates: /etc/locale.conf: Makes the task idempotent. It will only run if the file /etc/locale.conf does not exist (meaning the locale hasn’t been set yet).
    • notify: restart_systemd_daemon: Triggers the restart_systemd_daemon handler if the locale is changed.
  • handlers: A list of handlers that are executed only when notified by a task.
    • name: restart_systemd_daemon: A descriptive name for the handler.
    • systemd: name: systemd-localed.service state: restarted: Restarts the systemd-localed service, ensuring that the new locale settings are applied.
  1. Define the desired_locale variable: You can define this variable in your Ansible inventory file, in a group_vars file, or on the command line. For example, in your inventory file (hosts):
[ubuntu_servers]
server1 ansible_host=192.168.1.10
server2 ansible_host=192.168.1.11

[ubuntu_servers:vars]
desired_locale=es_ES.UTF-8
  1. Run the playbook:
ansible-playbook -i hosts locale.yml

Advantages:

  • Scalable: Easily manage locales across multiple servers.
  • Idempotent: Playbooks ensure that changes are only made if necessary, preventing unintended side effects.
  • Centralized management: Define and apply configurations from a single location.
  • Repeatable: Ensures consistent configurations across all servers.

Disadvantages:

  • Requires familiarity with Ansible.
  • Initial setup can be more complex than using command-line tools.

These alternative solutions offer different approaches to achieving the same goal of setting the system locale on Ubuntu 22.04. The best method will depend on your specific needs and environment. Whether it’s setting up a single server or managing a large fleet, understanding these options gives you flexibility and control over your system configurations. With this guide you can now easily Set up System Locale on Ubuntu 22.04.