Complete Guide to Fixing Garbled Text in Ubuntu | Causes, Solutions, and Configuration Changes

目次

1. Introduction

When using Ubuntu, you may encounter garbled text in certain situations. This can happen in terminal output, when displaying Japanese file names, or while browsing Japanese web pages, depending on your environment. In many cases, Japanese may not display correctly with the default settings, requiring proper configuration.

In this article, we will explain the causes of garbled text in Ubuntu and provide concrete solutions to fix it. This guide is intended for:

  • Ubuntu beginners who have not yet set up Japanese display settings
  • Users looking to understand the root causes of garbled text and find fundamental solutions
  • Users experiencing garbled text in terminal or GUI environments and wanting to know how to fix it

Let’s start by examining the main causes of garbled text in Ubuntu.

2. Main Causes of Garbled Text

Incorrect Locale Settings

Locales in Ubuntu define system language and date formatting settings. If these are not properly configured, Japanese text may not display correctly, leading to garbled characters.

For example, if you run the locale command and see “C” or “POSIX” in the output, your locale settings may be incorrect:

$ locale
LANG=C
LC_ALL=

Ideally, for a Japanese environment, it should be set to LANG=ja_JP.UTF-8.

Missing or Insufficient Fonts

By default, Ubuntu may not have Japanese fonts installed. As a result, Japanese text may not render properly and instead display as square boxes (□) or random symbols.

Font issues can be identified in the following cases:

  • Garbled text appears in menus and buttons of GUI applications
  • Japanese text in a text editor appears garbled

Character Encoding Mismatch

While Ubuntu primarily uses UTF-8 as its standard encoding, opening files encoded in Shift_JIS or EUC-JP from external sources may cause garbled text.

For instance, opening a file created in a Windows environment on Ubuntu may result in the following:

  • Unreadable characters when opened in a text editor
  • Distorted text when using the cat command in the terminal

Misconfigured Terminal or Editor Settings

Even if a file is encoded in UTF-8, improper terminal or editor settings may prevent proper text rendering.

  • The terminal encoding is set to something other than UTF-8
  • Text editors like Vim or VSCode fail to auto-detect the correct encoding
  • Japanese characters appear as “?” or “◇” when displayed with less or cat
年収訴求

3. Checking and Fixing Locale Settings

How to Check Your Locale Settings

To check your current locale settings, run the following command:

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=

If LANG=C is displayed, your system is not properly set up for Japanese. A correctly configured Japanese environment should look like this:

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

Installing and Configuring Japanese Locale

1. Check and Add Japanese Locale

To check if Japanese locale is installed, run:

locale -a | grep ja_JP

Example output:

ja_JP.eucJP
ja_JP.utf8

If ja_JP.utf8 is missing, install the Japanese locale with:

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

Then, generate the locale and update the system settings:

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

2. Set System-Wide Locale

To apply the changes system-wide, run:

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

For system-wide changes, edit:

sudo nano /etc/default/locale

And add or modify the following:

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

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

4. Installing and Configuring Fonts

Why Japanese Fonts Are Necessary

By default, Ubuntu may not have Japanese fonts installed. This can result in improperly displayed Japanese text, appearing as square boxes (□) or unreadable symbols.

Font-related issues can be observed in the following cases:

  • Menus and buttons in GUI applications appear garbled
  • Japanese text in a text editor displays incorrectly

Recommended Japanese Fonts

Here are some recommended Japanese fonts for Ubuntu:

Font NameFeatures
Noto Sans CJK JPA high-quality Japanese font provided by Google (recommended as the default)
Takao FontsPreviously the default font for Ubuntu (available in thin and bold versions)
IPA FontsHigh-quality fonts provided by the Information-technology Promotion Agency (IPA)
VL GothicHighly readable and optimized for terminal use

How to Install Fonts

1. Install Noto Sans CJK JP (Recommended Default Font)

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

2. Install Takao Fonts

sudo apt install -y fonts-takao

3. Install IPA Fonts (Information-technology Promotion Agency)

sudo apt install -y fonts-ipafont

4. Install VL Gothic (Optimized for Terminal)

sudo apt install -y fonts-vlgothic

After installing the fonts, restart your system or refresh the font cache with:

fc-cache -fv

How to Configure Fonts

Setting Fonts in GUI Applications

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

Setting Fonts in the Terminal

  1. Open the terminal
  2. Go to “Preferences” → “Profile Preferences”
  3. Check the box for “Use custom font”
  4. Select a preferred font (e.g., Noto Sans Mono CJK JP)
  5. Save the settings and restart the terminal

Applying and Verifying Font Settings

To verify if the font settings have been correctly applied, try the following:

  1. Check the installed fonts using the fc-list command
fc-list | grep "Noto"
  1. Test Japanese text display in the terminal
echo "こんにちは、Ubuntuの文字化け対策"
  1. Check if Japanese text displays correctly in GUI applications (Firefox, LibreOffice, etc.)

5. Checking and Converting Character Encoding

What is Character Encoding?

Character encoding is a system that assigns digital codes to characters. Some commonly used encodings include:

EncodingFeaturesPrimary Usage
UTF-8Supports multiple languages, the standard for LinuxUbuntu, Web development
Shift_JISJapanese-specific, commonly used in WindowsWindows applications, legacy systems
EUC-JPUsed in UNIX-based systemsOlder Linux systems
ISO-2022-JPUsed for emails and specific environmentsEmail communication

Since Ubuntu primarily uses UTF-8, opening files in other encodings (such as Shift_JIS) may cause garbled text.

How to Check a File’s Character Encoding

1. Use the file Command

file -i sample.txt

Example output:

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

2. Use the nkf Command

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

Example output:

Shift_JIS (CRLF)

How to Convert Character Encoding

1. Convert Using the iconv Command

Convert Shift_JIS to UTF-8:

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

Convert EUC-JP to UTF-8:

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

2. Convert Using the nkf Command

Convert Shift_JIS to UTF-8:

nkf -w sample.txt > sample_utf8.txt

Convert EUC-JP to UTF-8:

nkf -w --overwrite sample.txt

Preventing Garbled Text in Terminal and Editors

1. Use the less Command to Display Properly

export LESSCHARSET=utf-8
less sample.txt

2. Specify Character Encoding in vim

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

3. Change Encoding in gedit or VSCode

  • gedit (GNOME’s default editor)
  1. Open the file with gedit sample.txt
  2. When saving, change the encoding to UTF-8
  • VSCode (Visual Studio Code)
  1. Click “Encoding” at the bottom of the screen
  2. Select UTF-8

6. Checking Terminal and Editor Settings

Checking and Fixing Terminal Settings

1. Check Terminal Encoding Settings

To check your current environment variables, run the following commands:

echo $LANG
echo $LC_ALL

Example Output (Correct Settings):

ja_JP.UTF-8
ja_JP.UTF-8

If the output is C or POSIX, you need to change the locale settings to ja_JP.UTF-8.

2. Configure Terminal Fonts

GNOME Terminal (Default Terminal)

  1. Open the terminal
  2. Go to “Preferences”
  3. Open “Profile Preferences” and go 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 the settings and restart the terminal

Setting Character Encoding in Text Editors

1. Configure Vim Encoding

To check your current settings, open Vim and run:

:set encoding?
:set fileencoding?

Expected Output:

encoding=utf-8
fileencoding=utf-8

If it is not set to utf-8, add the following to your ~/.vimrc file to make UTF-8 the default:

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

2. Configure Nano Encoding

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

set encoding "utf-8"

3. Configure VSCode Encoding

  1. Click on “Encoding” at the bottom-right of the VSCode window
  2. Select UTF-8
  3. If needed, use “Save with Encoding”

To make UTF-8 the default encoding, add the following to your settings.json file:

"files.encoding": "utf8"

7. Troubleshooting Specific Cases

Fixing Garbled Text in GUI Applications

1. Japanese Display Issues in Firefox and Chrome

Solution:

  1. Install the necessary fonts
sudo apt install -y fonts-noto-cjk fonts-ipafont
  1. Check browser font settings
  • Firefox:
    1. Go to about:preferences, then open “Fonts & Colors” → “Advanced”
    2. Change “Proportional” and “Monospace Fonts” to Noto Sans CJK JP
  • Chrome:
    1. Go to chrome://settings/fonts
    2. Change “Standard Font” and “Monospace Font” to Noto Sans CJK JP

2. Fixing Garbled Text in LibreOffice

Solution:

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

Fixing Garbled Text in CUI Environments

1. SSH Session Garbled Text Issues

Solution:

  1. Check the locale settings on the server
locale
  1. If ja_JP.UTF-8 is missing, run:
sudo apt install -y language-pack-ja
sudo locale-gen ja_JP.UTF-8
sudo update-locale LANG=ja_JP.UTF-8

8. Frequently Asked Questions (FAQ)

Q1. My locale is set correctly, but garbled text persists.

A: Even if your locale is correct, verify the settings:

locale

If LANG=ja_JP.UTF-8 is not displayed, reset the locale:

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

Q2. Only specific files have garbled text.

A: The file may have a different encoding. Check using:

file -i sample.txt

If the file is not in 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

9. Conclusion

In this article, we covered Ubuntu’s garbled text issues, their causes, and how to fix them. The primary causes of garbled text include incorrect locale settings, missing fonts, character encoding mismatches, and misconfigured terminal/editor settings.

Key Takeaways

  • Set the correct locale: Run update-locale LANG=ja_JP.UTF-8
  • Install Japanese fonts: Use sudo apt install -y fonts-noto-cjk fonts-ipafont
  • Verify file encoding: Use file -i and convert with iconv
  • Configure terminal and editors: Ensure UTF-8 encoding settings are applied

By following these steps, you can resolve and prevent garbled text issues in Ubuntu.

侍エンジニア塾