Raspberry Pi: Faced Locale error when Playing Ansible — How to resolve Locale? ( en_US.UTF-8 )

Life-is-short--so--enjoy-it
6 min readNov 25, 2023

When playing Ansible playbook, somehow I faced Locale related error.

Raspberry Pi: Faced Locale error when Playing Ansible — How to resolve Locale?

UPDATE: 2024–03–06

I found the better way and documented it here.

UPDATE: 2024–03–05

I got curious if there are any ways that I do not need to use “sudo dpkg-reconfigure locales” to select the locales to generate.

I noticed that a few blog posts show using locale-gen to generate other than the current existing locales, but when I tried it. It did not work like below.

UPDATE: 2023–11–27 ( still the only working version )

# 1. select locale to generate
sudo dpkg-reconfigure locales
# 2. update-locale
sudo update-locale LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8 LANGUAGE=en_US.UTF-8
# 3. apply
# note: you might see error, but it's ok. just logout and login again.
source /etc/default/locale
# 4. logout and login again
Uncheck en_US.ISO-8859–1 | Check en_US.UTF-8

Intro

When I worked on adding the second local DNS server to increase the DNS service availability, I noticed an issue that Ansible wasn’t able to run due to the Locale encoding issue.

It looked that the Ansible required UTF-8 as an encoding, but somehow, the newly installed Raspberry Pi ( Bookwarm ) had ( or set ) ISO8859–1 as an encoding. ( why?? )

Current Locale

I don’t know how I got into the issue. ( maybe it’s not even an issue? )

I checked the current Locale by running the locale command. The printed list of LC_* were all en_US without any specific encoding.

I didn’t know how to change the values of the locale to en_US.UTF-8 , so I googled about it.

Locale: it’s all en_US without any specific encoding

Approach 1: locale-gen / dpkg-reconfigure — Not Working

There was a post in StackOverflw that said the issue could be fixed by using the commands below.

After trying these commands, the Locale issue got worse. Only the value of “LANG” is updated to “en_US.UTF-8” and the others still had “en_US”. The mismatch values started causing some error or warning messages.

By the way, `dpkg-reconfigure locales` is a shortcut to see the Localization menu in raspi-config

# @note: `locale-gen` might not need to run
# because `dpkg-reconfigure locales` will generate the selected locale anyway.
# @note: `dpkg-reconfigure locales` is a shortcut of the Localization in raspi-config.
sudo locale-gen en_US.UTF-8
sudo dpkg-reconfigure locales
updated locale after “dpkg-reconfigure locales”
screenshots in “dpkg-reconfigure locales”

Approach 2: export LC_ALL=”en_US.UTF-8"

After the “approach 1”, I ran the Ansible again. The Ansible was NOT able to run due to the Locale setting issue. ( the screenshot on the left )

After little more googling, I found a post in StackOverflow that the all LC_* can be updated by running this command.

export LC_ALL="en_US.UTF-8"

After running the command, the all Locale became “en_US.UTF-8” and the Ansible was able to run without showing any Locale related error messages.

But, export LC_ALL=”en_US.UTF-8" only applied to the current session

The export LC_ALL=en_US.UTF-8 only was applied in the current session where I executed the command. And it make sense because it’s environment variable. It has to be valid only in the current session.

export LC_ALL="en_US.UTF-8"
"export LC_ALL=en_US.UTF-8" is only valid in the current session.

Rather than running export LC_ALL=en_US.UTF-8 on every session login, it can be added into ~/.bash_profile or ~/.bashrc to apply it on every login automatically.

FYI: .bash_profile will be applied only when the interactive login shell is used ( like SSH ). `.bashrc will be applied when the interactive login shell is used and when su ( switching user ) is used.

I wanted to set en_US.UTF-8 as a default and global value, so I skipped adding export LC_ALL=en_US.UTF-8 into ~/.bashrc .

Approach 3: update-locale

There was another post in StackOverflow that the Locale issue can be fixed by using these commands.

However, as you can see in the screenshots below, those commands’ output still showed the warning message ( the screenshot on the left ). And also, it failed to update the locale ( the screenshot on the right ) as well.

sudo update-locale LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8
sudo update-locale LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8 LANGUAGE=en_US.UTF-8
Content of /etc/default/locale
update-locale didn’t help set the locale.

Approach 4: “source /etc/default/locale”

Again, I found another post in StackOverflow that showed using “source /etc/default/locale”.

And, it worked!!

It updated the the all the values of LC_* like below. It even persisted for all new login as well.

I also checked the content of /etc/default/locale . The values of all keys were updated to “en_US.UTF-8” as well.

When I wrote this post, I wasn’t sure if “source /etc/default/locale” worked because I ran “update-locale LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8 LANGUAGE=en_US.UTF-8” first or not.
If you’re having the same issue, maybe you can try with the below commands in order.

sudo update-locale LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8 LANGUAGE=en_US.UTF-8"
source /etc/default/locale
source /etc/default/locale fixed the locale issue.
The content in /etc/default/locale is updated.

Extra: How to get a list of the locale names suitable for use in environment variables?

locale -a
the locale names suitable for use in environment variables

Extra: Regression impact from the Global level Locale change

Since I am the only one who will login into the Raspberry Pi, I was ok to set it to en_US.UTF-8. However, it is a shared one for many users, the gobal level Locale change might give a negative impact to the other users who are not in “en_US.UTF-8"

Building Local DNS Server with Raspberry Pi

If you’re interested in building DNS Server with Raspberry Pi, here are the list of posts you might be interested in.

AdGuard / Pi-Hole / DNS

15 stories

--

--

Life-is-short--so--enjoy-it

Gatsby Lee | Data Engineer | City Farmer | Philosopher | Lexus GX460 Owner | Overlander