How to Fix Garbled Text on Ubuntu: Complete Guide to Locale, Fonts, and Encoding Settings

目次

1. Introduction

When using Ubuntu, you may occasionally encounter garbled characters. These issues can appear in various situations, such as terminal output, displaying Japanese filenames, or viewing Japanese web pages in a browser. In many cases, Japanese text may not display correctly with the default configuration, making proper setup essential.

This article explains the causes of garbled text in Ubuntu and provides concrete solutions to resolve them. This guide is intended for the following users:

  • Beginners who are using Ubuntu without configuring Japanese language support
  • Users who want to understand the root cause of garbled characters and seek fundamental solutions
  • Users experiencing garbled text in terminal or GUI environments and want to know how to fix it

Let’s begin by reviewing the primary causes of garbled text in Ubuntu.

2. Main Causes of Garbled Text

Incorrect Locale Settings

The locale in Ubuntu defines system settings related to language, date format, and other regional behaviors. When these settings are incorrect, Japanese text may not display properly, resulting in garbled characters.

For example, if running the locale command shows values like “C” or “POSIX,” the system locale is not configured properly:

$ locale
LANG=C
LC_ALL=

Ideally, a Japanese environment should show settings such as LANG=ja_JP.UTF-8.

Missing or Unconfigured Fonts

In some default Ubuntu installations, Japanese fonts may not be available, causing Japanese text to display as empty squares (□) or unreadable symbols.

This issue typically occurs in the following scenarios:

  • Menu items and buttons in GUI applications appear corrupted
  • Opening Japanese text in a text editor shows garbled characters

Mismatched Character Encoding

Ubuntu primarily uses UTF-8 encoding. When opening files encoded in Shift_JIS or EUC-JP—common in legacy Windows or UNIX systems—text corruption may occur.

Typical issues include:

  • Text editors display strange symbols when opening Japanese files
  • Output from the cat command appears broken in the terminal

Terminal or Editor Misconfiguration

Even if files are correctly encoded in UTF-8, misconfigured terminal or editor settings can still cause display issues.

  • The terminal encoding is set to something other than UTF-8
  • Editors like Vim or VSCode fail to auto-detect character encoding
  • Japanese characters appear as “?” or “◇” when viewed via less or cat

3. Checking and Fixing Locale Settings

How to Check Locale Settings

To check your current locale configuration, run the command below:

locale

Example output:

LANG=C
LC_CTYPE="C"
LC_NUMERIC="C"
LC_TIME="C"
LC_COLLATE="C"
LC_MONETARY="C"
LC_MESSAGES="C"
LC_PAPER="C"
LC_NAME="C"
LC_ADDRESS="C"
LC_TELEPHONE="C"
LC_MEASUREMENT="C"
LC_IDENTIFICATION="C"
LC_ALL=

In this case, LANG=C indicates that Japanese support is not enabled. For a proper Japanese configuration, you should see values such as:

LANG=ja_JP.UTF-8
LC_ALL=ja_JP.UTF-8

Installing and Setting the Japanese Locale

1. Check and Add the Japanese Locale

To confirm whether the Japanese locale is available in your system, run the following command:

locale -a | grep ja_JP

Example output:

ja_JP.eucJP
ja_JP.utf8

If ja_JP.utf8 is not listed, you must install the Japanese locale package.

Install it using the following commands:

sudo apt update
sudo apt install -y language-pack-ja

Then enable the locale:

sudo locale-gen ja_JP.UTF-8
sudo update-locale LANG=ja_JP.UTF-8

2. Apply the Locale System-Wide

To apply the locale change across the entire system, run the following commands:

export LANG=ja_JP.UTF-8
export LC_ALL=ja_JP.UTF-8

To make these changes persistent, add them to ~/.bashrc or ~/.profile:

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

If you want to apply the settings to all users, edit the following file instead:

sudo nano /etc/default/locale

Add or update the following entries:

LANG=ja_JP.UTF-8
LC_ALL=ja_JP.UTF-8

To apply the settings, log out and log back in, or reboot the system.

4. Installing and Configuring Japanese Fonts

Why Japanese Fonts Are Necessary

In a default Ubuntu environment, Japanese fonts may not be installed. Without them, Japanese text appears as empty squares or unintelligible symbols.

You can confirm missing fonts in the following situations:

  • Menu labels and GUI buttons display garbled characters
  • Japanese text appears corrupted when opened in a text editor

Recommended Japanese Fonts

The following Japanese fonts are available for use in Ubuntu:

Font NameDescription
Noto Sans CJK JPA high-quality Japanese font provided by Google (recommended as default)
Takao FontsThe former default fonts in Ubuntu, available in regular and bold styles
IPA FontsHigh-quality fonts provided by the Information-technology Promotion Agency (IPA)
VL Gothic (VLゴシック)Highly readable, ideal for terminal environments

How to Install Japanese Fonts

1. Noto Sans CJK JP (Recommended Default)

sudo apt update
sudo apt install -y fonts-noto-cjk

2. Takao Fonts

sudo apt install -y fonts-takao

3. IPA Fonts

sudo apt install -y fonts-ipafont

4. VL Gothic (for Terminal Use)

sudo apt install -y fonts-vlgothic

Once installation is complete, restart the system or refresh the font cache:

fc-cache -fv

How to Configure Fonts

Configuring Fonts in GUI Applications

  1. Open the “Settings” application
  2. Navigate to the “Fonts” section
  3. Change “Standard Font,” “Document Font,” and “Monospace Font” to your preferred fonts
  4. Log out and log back in to apply the changes

Configuring Fonts in the Terminal

  1. Open the terminal
  2. Select “Preferences” from the menu
  3. Open the “Profile” settings and enable “Use custom font”
  4. Select your preferred font (e.g., Noto Sans Mono CJK JP)
  5. Save the settings and restart the terminal

Verify Font Configuration

To confirm that fonts have been applied correctly, try the following steps:

  1. Check installed fonts using fc-list
fc-list | grep "Noto"
  1. Verify Japanese display in the terminal
echo "こんにちは、Ubuntuの文字化け対策"
  1. Confirm Japanese text rendering in GUI applications such as Firefox or LibreOffice

5. Checking and Converting Character Encoding

What Is Character Encoding?

Character encoding defines how characters are represented digitally. Common encodings include:

EncodingCharacteristicsMain Usage
UTF-8Multi-language support; standard in LinuxUbuntu and web development
Shift_JISJapanese-focused; standard in Windows environmentsWindows apps and legacy systems
EUC-JPPreviously used in UNIX-based systemsOlder Linux distributions
ISO-2022-JPUsed in some mail systemsEmail communication

Since Ubuntu uses UTF-8 as the default encoding, opening files encoded in other formats may lead to garbled text.

How to Check a File’s Character Encoding

1. Using the file Command

file -i sample.txt

Example output:

sample.txt: text/plain; charset=iso-8859-1

2. Using the nkf Command

sudo apt install -y nkf
nkf --guess sample.txt

Example output:

Shift_JIS (CRLF)

How to Convert Character Encoding

1. Using the iconv Command

Example: Convert Shift_JIS to UTF-8

iconv -f SHIFT_JIS -t UTF-8 sample.txt -o sample_utf8.txt

Example: Convert EUC-JP to UTF-8

iconv -f EUC-JP -t UTF-8 sample.txt -o sample_utf8.txt

2. Using the nkf Command

Example: Convert Shift_JIS to UTF-8

nkf -w sample.txt > sample_utf8.txt

Example: Convert EUC-JP to UTF-8

nkf -w --overwrite sample.txt

Preventing Garbled Text in Terminals and Editors

1. Display Correct Encoding with less

export LESSCHARSET=utf-8
less sample.txt

2. Open Files with Encoding Specified in vim

vim -c "set encoding=utf-8" sample.txt

3. Modify Character Encoding in gedit or VSCode

  • gedit (GNOME default editor)
  1. Open the file using gedit sample.txt
  2. When saving, select UTF-8 in the “Encoding” dropdown
  • VSCode (Visual Studio Code)
  1. Click the “Encoding” indicator at the bottom of the window
  2. Select UTF-8 to convert the file

6. Checking Terminal and Editor Settings

Verify and Adjust Terminal Settings

1. Check Terminal Encoding

To confirm your locale settings, run the commands below:

echo $LANG
echo $LC_ALL

Example output (correct configuration)

ja_JP.UTF-8
ja_JP.UTF-8

If the output shows C or POSIX, change the locale to ja_JP.UTF-8.

2. Configure Terminal Fonts

GNOME Terminal (Ubuntu default terminal)

  1. Open the terminal
  2. Select “Preferences”
  3. Open the “Profile” and navigate to the “Text” tab
  4. Enable “Use custom font” and select one of the following:
  • Noto Sans Mono CJK JP
  • VL Gothic
  • Takao Gothic
  1. Save your settings and restart the terminal

Configuring Editor Encoding

1. Vim Encoding Settings

Open Vim and run the following commands to check current encoding:

:set encoding?
:set fileencoding?

Example output:

encoding=utf-8
fileencoding=utf-8

If the settings differ from utf-8, update ~/.vimrc with:

set encoding=utf-8
set fileencodings=utf-8,sjis,euc-jp
set fileformats=unix,dos,mac

2. Nano Encoding Settings

To change the default encoding, add the following to ~/.nanorc:

set encoding "utf-8"

3. VSCode Encoding Settings

  1. Click the “Encoding” indicator at the bottom right
  2. Select “Reopen with Encoding” and choose UTF-8
  3. If needed, select “Save with Encoding”

To set UTF-8 as the default, add the following to settings.json:

"files.encoding": "utf8"

7. Solutions for Specific Cases

Preventing Garbled Text in GUI Applications

1. Japanese Characters Display Incorrectly in Firefox or Chrome

Solution:

  1. Install the required Japanese fonts
sudo apt install -y fonts-noto-cjk fonts-ipafont
  1. Check and update browser font settings
  • Firefox:
    1. Visit about:preferences
    2. Open “Fonts & Colors” → “Advanced”
    3. Set both “Proportional” and “Monospace” fonts to Noto Sans CJK JP
  • Google Chrome:
    1. Access chrome://settings/fonts
    2. Change “Standard font” and “Fixed-width font” to Noto Sans CJK JP

2. Garbled Text in LibreOffice

Solution:

  1. Install Japanese fonts such as fonts-noto-cjk or fonts-ipafont
  2. Change LibreOffice font settings
  • Go to “Tools” → “Options” → “LibreOffice” → “Fonts”
  • Set the default font to Noto Sans CJK JP

Preventing Garbled Text in CUI Environments

1. SSH Session Displays Garbled Characters

Solution:

  1. Check locale on the remote server
locale
  1. If it is not set to ja_JP.UTF-8, run the following commands:
sudo apt install -y language-pack-ja
sudo locale-gen ja_JP.UTF-8
sudo update-locale LANG=ja_JP.UTF-8

Preventing Garbled Text in Specific Applications

1. WSL (Windows Subsystem for Linux) Shows Garbled Japanese Text

Solution:

  1. Configure WSL to use ja_JP.UTF-8
echo 'export LANG=ja_JP.UTF-8' >> ~/.bashrc
echo 'export LC_ALL=ja_JP.UTF-8' >> ~/.bashrc
source ~/.bashrc
  1. Set Windows Terminal font to Noto Sans Mono CJK JP

2. Garbled Japanese Text Inside Docker Containers

Solution:

  1. Enter the container and check locale
docker exec -it container_name bash
locale
  1. Add the Japanese locale if missing
apt update && apt install -y locales
locale-gen ja_JP.UTF-8
export LANG=ja_JP.UTF-8
export LC_ALL=ja_JP.UTF-8

8. FAQ (Frequently Asked Questions)

Q1. I configured the locale correctly, but garbled text still appears.

A: Verify the locale settings again:

locale

If LANG=ja_JP.UTF-8 is not set, reconfigure using:

sudo update-locale LANG=ja_JP.UTF-8
sudo locale-gen ja_JP.UTF-8
sudo dpkg-reconfigure locales

Q2. Only certain files show garbled text.

A: Different files may have different character encodings. Check the encoding:

file -i sample.txt

If the file is not UTF-8, convert it:

iconv -f SHIFT_JIS -t UTF-8 sample.txt -o sample_utf8.txt

Or, using nkf:

nkf -w --overwrite sample.txt

Q3. I cannot input Japanese characters in the terminal.

A: Ensure that a Japanese input method (Fcitx or IBus) is installed.

sudo apt update
sudo apt install -y fcitx-mozc
im-config -n fcitx

Q4. Japanese text is garbled in WSL.

A: Set locale in WSL:

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

Q5. Japanese text is garbled inside a Docker container.

A: If the locale is C.UTF-8, Japanese text will not display properly.

apt update && apt install -y locales
locale-gen ja_JP.UTF-8
export LANG=ja_JP.UTF-8
export LC_ALL=ja_JP.UTF-8

Q6. GUI menus and dialogs show corrupted Japanese text.

A: Install fonts and change font settings:

sudo apt install -y fonts-noto-cjk fonts-ipafont

9. Summary

This article provided a detailed explanation of the causes and solutions for garbled text issues in Ubuntu. Garbled characters typically occur due to misconfigured locale settings, missing fonts, mismatched character encoding, or incorrect terminal/editor configurations. However, with proper adjustments, these problems can be resolved effectively.

1. Main Causes of Garbled Text

  • Incorrect locale configuration: If the system locale is set to C or POSIX, Japanese text cannot be displayed correctly
  • Fonts not installed: Without Japanese fonts, GUI applications and terminals cannot properly render Japanese characters
  • Encoding mismatch: Opening files saved in different encodings (e.g., Shift_JIS) can cause corruption
  • Improper terminal/editor settings: If encoding is not set to UTF-8, Japanese text may not display correctly

2. Solutions to Prevent Garbled Text

ItemSolution
Locale configurationCheck with locale and run update-locale LANG=ja_JP.UTF-8
Font installationsudo apt install -y fonts-noto-cjk fonts-ipafont
Check file encodingUse file -i or nkf --guess
Convert encodingiconv -f SHIFT_JIS -t UTF-8 filename -o newfile
Terminal configurationSet LESSCHARSET=utf-8 and change fonts to Noto Sans Mono CJK JP
Fix GUI garbled textSet fonts to Noto Sans CJK JP and use gnome-tweaks if needed
Fix WSL garbled textSet ja_JP.UTF-8 and configure fonts
Fix Docker garbled textRun locale-gen ja_JP.UTF-8 and configure locale in Dockerfile

3. Additional Recommendations

  • Keep your system updated: Outdated packages may cause issues in the Japanese environment
sudo apt update && sudo apt upgrade -y
  • Persist settings: Add locale settings to ~/.bashrc or ~/.profile to apply them automatically on login
  • Back up configuration files: Before editing files like /etc/default/locale, make a backup

Conclusion

The garbled text problem in Ubuntu can be resolved by properly configuring the four key components: locale, fonts, character encoding, and terminal/editor settings. By following the methods introduced in this guide, you can eliminate almost all garbled text issues across Ubuntu environments, whether in terminal sessions, GUI applications, WSL, or Docker containers.