Blog / Linux/ Guide to Configuring Chinese Locale on Debian/Ubuntu Systems

Guide to Configuring Chinese Locale on Debian/Ubuntu Systems

Debian/Ubuntu 系统中文语言环境配置指南:解决终端乱码与显示问题

Configuring Chinese Locale on Debian/Ubuntu Systems

If your terminal or applications display garbled Chinese characters on Debian or Ubuntu, it's usually because the Chinese locale is not installed or configured correctly. This guide details how to install language packs, configure locale settings, and permanently set Chinese as the default system language.

Step 1: Install the locales Package

First, ensure the locales package, which manages system language environments, is installed.

sudo apt update
sudo apt install locales

Step 2: Configure Locales

Use the dpkg-reconfigure command to interactively generate and select the required locales.

sudo dpkg-reconfigure locales

After running the command, you'll enter a text configuration interface:

  1. Select Locales: Use the arrow keys to navigate the list. Press Spacebar to select/deselect. To support Chinese, ensure at least these options are selected:
    • en_US.UTF-8 UTF-8 (Recommended to keep as a fallback)
    • zh_CN.GB2312 GB2312
    • zh_CN.GBK GBK
    • zh_CN.UTF-8 UTF-8 (This is the most important option)
  2. Confirm Selection: Use the Tab key to move focus to <Ok>, then press Enter.
  3. Set Default Locale: In the next screen, all selected locales are listed. Use the arrow keys to select zh_CN.UTF-8 and press Enter to set it as the system's default locale.

Step 3: Verify and Temporary Setting

After configuration, you can temporarily set the locale in your current terminal session to test:

export LANG=zh_CN.UTF-8

After this command, the command prompt and some Chinese-supporting programs in the current terminal window should display Chinese correctly.

Step 4: Permanently Set System Locale

Temporary settings only apply to the current session. To make the Chinese environment permanent, modify the system or user's environment variable configuration files.

Method 1: Modify User Profile (Recommended)

Edit the current user's ~/.bashrc or ~/.profile file:

echo 'export LANG=zh_CN.UTF-8' >> ~/.bashrc
echo 'export LC_ALL=zh_CN.UTF-8' >> ~/.bashrc

Then apply the changes immediately:

source ~/.bashrc

Method 2: Modify Global System Configuration

Edit the system configuration file /etc/environment (requires root privileges):

sudo nano /etc/environment

Add or modify the following lines in the file:

LANG="zh_CN.UTF-8"
LC_ALL="zh_CN.UTF-8"

Save and exit. This change will take effect for all users after a system reboot.

Step 5: Configure Terminal Emulator

In addition to system settings, the terminal software itself must be configured to use UTF-8 encoding.

  • GNOME Terminal / Ubuntu Default Terminal: Usually requires no extra configuration; it inherits system settings automatically.
  • Other Terminals (e.g., xterm, rxvt): Look for the Character Encoding option in their settings or startup parameters and set it to UTF-8.
  • Remote Connection Tools (e.g., PuTTY, Xshell): In the connection settings, set the "Character set" or "Encoding" to UTF-8.

Troubleshooting and Additional Notes

  • Missing Fonts: If Chinese displays as boxes, Chinese fonts may be missing. Install a font package:
    sudo apt install fonts-noto-cjk
  • Only Some Programs Show Garbled Text: Some legacy programs may not respect the LANG setting. Try setting their environment variables individually or specifying them in startup scripts.
  • Check Current Settings: Use this command to view the currently active locale settings:
    locale
  • Scope of Effect: LANG is the default setting. LC_ALL has the highest priority and overrides all LC_* settings. Setting both ensures consistency.

After completing all steps, reboot the system or log out and back in. Your Debian/Ubuntu system should now correctly display and handle Chinese.

Post a Comment

Your email will not be published. Required fields are marked with *.