[perl] How to fix a locale setting warning from Perl

When I run perl, I get the warning:

perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
    LANGUAGE = (unset),
    LC_ALL = (unset),
    LANG = "en_US.UTF-8"
are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").

How do I fix it?

This question is related to perl locale

The answer is


On Debian, after much searching, this did the trick.

First:

sudo apt-get purge locales

Then:

sudo aptitude install locales

And the famous:

sudo dpkg-reconfigure locales

This rids the system of locales, then re-installs locales and downgrades libc6 from 2.19 to 2.13 which is the issue. Then it configures locales again.


If you don't care about the locale issue, you can set PERL_BADLANG=0. Of course, this could result in incorrect localisation.


For macOS & Mac OS X users only

I was getting the same warning while using Git

To resolve this warning Uncheck the Set locale environment variable on startup option and restart your terminal. Below screen shot represents my terminal settings.

enter image description here


Setting the LC_TYPE environment variable to the default locale language "C" will help to suppress this warning.

Run export LC_CTYPE="C" and try to run the perl command.

P.S: You need to set this variable in one of file /etc/environment or file /etc/default/locale for a permanent solution.


This generally means you haven't properly set up locales on your Linux box.

On Debian or Ubuntu, that means you need to do

$ sudo locale-gen
$ sudo dpkg-reconfigure locales

See also man locale-gen.


For anyone connecting to DigitalOcean or some other Cloud hosting provider from the iTerm2.app on macOS v10.13 (High Sierra) and getting this error on some commands:

perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
    LANGUAGE = (unset),
    LC_ALL = (unset),
    LC_CTYPE = "UTF-8",
    LANG = "en_US.UTF-8"
  are supported and installed on your system.
perl: warning: Falling back to a fallback locale ("en_US.UTF-8").

This fixed the problem for me:

Enter image description here


For Ubuntu use this,

#export LANGUAGE=en_US.UTF-8
#export LC_ALL=en_US.UTF-8
#export LANG=en_US.UTF-8
#export LC_TYPE=en_US.UTF-8

It worked for me.


Add missing locales to file .bash_profile:

echo "export LANGUAGE=en_US.UTF-8
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8">>~/.bash_profile

Then source your .bash_profile file:

source ~/.bash_profile

I have this issue whenever I run a Perl script, such as enum4linux, on the latest Kali Linux version.

kali@kali:~$ lsb_release -a
No LSB modules are available.
Distributor ID:    Kali
Description:    Kali GNU/Linux Rolling
Release:    2020.3
Codename:    kali-rolling
kali@kali:~$

E.g.,

kali@kali:~$ enum4linux
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
    LANGUAGE = (unset),
    LC_ALL = (unset),
    LC_ADDRESS = "ms_MY.UTF-8",
    LC_NAME = "ms_MY.UTF-8",
    LC_MONETARY = "ms_MY.UTF-8",
    LC_PAPER = "ms_MY.UTF-8",
    LC_IDENTIFICATION = "ms_MY.UTF-8",
    LC_TELEPHONE = "ms_MY.UTF-8",
    LC_MEASUREMENT = "ms_MY.UTF-8",
    LC_TIME = "ms_MY.UTF-8",
    LC_NUMERIC = "ms_MY.UTF-8",
    LANG = "en_US.UTF-8"
    are supported and installed on your system.
perl: warning: Falling back to a fallback locale ("en_US.UTF-8").
enum4linux v0.8.9 (http://labs.portcullis.co.uk/application/enum4linux/)
Copyright (C) 2011 Mark Lowe ([email protected])

Look at the warning message given.

perl: warning: Falling back to a fallback locale ("en_US.UTF-8").

Also, notice that LC_ALL = (unset)

The solution is simple. All you have to do is to set it.

export LC_ALL=en_US.UTF-8

E.g.,

kali@kali:~$ export LC_ALL=en_US.UTF-8
kali@kali:~$

And problem solved

kali@kali:~$ enum4linux
enum4linux v0.8.9 (http://labs.portcullis.co.uk/application/enum4linux/)
Copyright (C) 2011 Mark Lowe ([email protected])

For a permanent solution, you might want to add it to the .bashrc file.


It is simple fix in Ubuntu. You have to generate the Locales from scratch, running the following commands from the command line:

sudo locale-gen en_US en_US.UTF-8
sudo dpkg-reconfigure locales

This should create the locales and then re-configure them.


If you are creating a rootfs using debootstrap you will need to generate the locales. You can do this by running:

# (optional) enable missing locales
sudo nano /etc/locale.gen

# then regenerate
sudo locale-gen

This tip comes from, https://help.ubuntu.com/community/Xen


You need to configure locale appropriately in /etc/default/locale, logout, login, and then run the regular commands

root@host:~# echo -e 'LANG=en_US.UTF-8\nLC_ALL=en_US.UTF-8' > /etc/default/locale
root@host:~# exit
local-user@local:~$ ssh root@host
root@host:~# locale-gen en_US.UTF-8
root@host:~# dpkg-reconfigure locales

perl: warning: Falling back to the standard locale ("C").
locale: Cannot set LC_ALL to default locale: No such file or directory

Solution:

Try this (uk_UA.UTF-8 is my current locale. Write your locale, for example en_US.UTF-8 !)

sudo locale-gen uk_UA.UTF-8

and this.

sudo dpkg-reconfigure locales

Try to reinstall:

localess apt-get install --reinstall locales

Read more in How to change the default locale


If you use Mac OS X v10.10 (Yosemite) or above to connect in your server Linux, you can try these steps.

  1. Keep your file /etc/ssh/sshd-config original

  2. Put on your ~/.bash_profile

    export LANG="en_US"
    export LC_ALL=$LANG.UTF-8
    
  3. Run

    dpkg-reconfigure locales
    

    And select "en_US.UTF-8"


Here is how to solve it on Mac OS X v10.7 (Lion) or Cygwin (Windows 10):

Add the following lines to your bashrc or bash_profile file on the host machine:

# Setting for the new UTF-8 terminal support in Lion
export LC_CTYPE=en_US.UTF-8
export LC_ALL=en_US.UTF-8

If you are using Z shell (zsh), edit file zshrc:

# Setting for the new UTF-8 terminal support in Lion
LC_CTYPE=en_US.UTF-8
LC_ALL=en_US.UTF-8

If you are running a chroot in CentOS, try manually copying /usr/lib/locale to the chroot environment for the account that is having this issue.


As always, the devil is in the detail...

On Mac OS X v10.7.5 (Lion), to fix some Django error, in my ~/.bash_profile I've set:

export LANG=en_EN.UTF-8
export LC_COLLATE=$LANG
export LC_CTYPE=$LANG
export LC_MESSAGES=$LANG
export LC_MONETARY=$LANG
export LC_NUMERIC=$LANG
export LC_TIME=$LANG
export LC_ALL=$LANG

And in turn for a long time I got that warning when using Perl.

My bad! As I've realized much later, my system is en_US.UTF-8! I fixed it simply by changing from

export LANG=en_EN.UTF-8

to

export LANG=en_US.UTF-8

All the previous answers are wrong. The message is clear - missing locale. The solution is to add the appropriate locale. You do that by editing the /etc/locale.gen file, remove the # sign in front of the locale being reported as missing and then issuing the command:

$ sudo locale-gen

This will actually generate the locales specified in /etc/locale.gen and therefore the message will not be shown.


ssh overwrites LC locale variables by default. See /etc/ssh/sshd_config:

AcceptEnv LANG LC_*

So maybe you need to set these variables in your local shell.


Use:

export LANGUAGE=en_US.UTF-8
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
export LC_CTYPE=en_US.UTF-8

It works for Debian. I don't know why - but locale-gen had not results.

Important! It's a temporary solution. It has to be run for each session.


Export the variable

$ export LANGUAGE=en_US.UTF-8
$ export LC_ALL=en_US.UTF-8
$ export LANG=en_US.UTF-8
$ export LC_CTYPE=en_US.UTF-8

Next run

$ sudo locale-gen
$ sudo dpkg-reconfigure locales 

When you run dpkg-reconfigure locales it asks you to choose the locales, choose en_US.UTF-8 . If you run this by selecting all locales, it will take some time to configure.


sudo nano /etc/locale.gen

Uncomment the locales you want to use (e.g. en_US.UTF-8 UTF-8):

Then run:

sudo /usr/sbin/locale-gen

Source: Configuring Locales


Adding the following to /etc/environment fixed the problem for me on Debian and Ubuntu (of course, modify to match the locale you want to use):

LANGUAGE=en_US.UTF-8
LC_ALL=en_US.UTF-8
LANG=en_US.UTF-8
LC_CTYPE=en_US.UTF-8

For Debian users, I have this problem after modifying my locale to change machine's language. This is what I did:

  1. Modify .bashrc:

    export LANG=fr_FR.UTF-8
    export LC_ALL=fr_FR.UTF-8
    
  2. Uncomment line fr_FR.UTF-8 in file etc/locale.gen -> sudo locale-gen to generate the missing package

  3. sudo update-locale

  4. sudo dpkg-reconfigure locales to configure my locale to fr_FR.UTF-8

  5. Add extra lines to the etc/default/locale file:

    LANGUAGE=en_US.UTF-8
    LC_ALL=en_US.UTF-8
    LANG=en_US.UTF-8
    LC_TYPE=en_US.UTF-8
    
  6. Reboot my computer and everything works fine


Add LC_ALL="en_GB.utf8" to /etc/environment and reboot. That's all.


For me, I fixed this error by editing the .bashrc file, adding exports. Add after the initial comments.

Add language support.

export LANGUAGE=en_US.UTF-8
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
export LC_TYPE=en_US.UTF-8

Following the accepted answer:

LANG=C ssh hunter2.

LC_ALL=C ssh hunter2

on the client side did the trick for me.


In my case, with Debian 8.6 (Jessie), I had to change settings in:

/etc/ssh/ssh_config` for `#AcceptEnv LANG LC_*

and

sshd_config for #SendEnv LANG LC_*

Then restart the ssh service.

At last, I did:

locale-gen en_US.UTF-8 and dpkg-reconfigure locales


perl -e exit
sudo localedef -i en_US -f UTF-8 en_DE.UTF-8
#                                   DE = German
# Use your country code en lieu of  DE

# The second "perl" should then not complain any more
perl -e exit

localectl list-locales # Just make sure it is OK

With zsh ohmyzsh I added this to the .zshrc:

 # You may need to manually set your language environment
 LANGUAGE=en_US.UTF-8
 LANG=en_US.UTF-8
 LC_CTYPE=en_US.UTF-8
 LC_ALL=en_US.UTF-8

By removing the line export LANG=en_US.UTF-8

Reopened a new tab and SSHed in, worked for me :)


Another Git-related answer:

The source of the problem might be the Git server. If all else fails, try doing dpkg-reconfigure locales (or whatever is appropriate for your distribution) on the server.


If this problem occurs while you are connecting via ssh to a remote host then it is possible that the remote system is missing certain locales. I am not going to repeat how to install and configure locales as this has been well explained by other answers already.

As other answers have pointed out, ssh should be passing your local computer's locales to the remote host. For example, if you have Australian locales set (e.g., en_AU.UFT-8), and you are connecting to a newly setup Ubuntu Server which only comes with en_US.UTF-8 then you will receive this warning.

To solve this problem you have several options:

  1. Install the required locales on the remote host such that they match locales configured on your client.

  2. Change the SSH configuration to not pass your clients' environment variables. I would not recommend this.

  3. Override the locale on your remote machine by exporting locale settings from file .bashrc and friends.


For me, on Ubuntu 16.04 (Xenial Xerus) the following worked:

root@host:~#locale-gen en_GB.UTF-8
root@host:~#localectl set-locale LANG=en_GB.UTF-8,LC_ALL=en_GB.UTF-8

Then reboot...


Adding the correct locale to ~/.bashrc, ~/.bash_profile, /etc/environment and the like will solve the problem, however it is not recommended, as it overrides the settings from /etc/default/locale, which is confusing at best and may lead to the locales not being applied consistently at worst.

Instead, one should edit /etc/default/locale directly, which may look something like this:

LANG=en_US.UTF-8
LANGUAGE=en_US:en
LC_CTYPE=en_US

The change will take effect the next time you log in. You can get the new locale in an existing shell by sourcing /etc/default/locale like this:

$ . /etc/default/locale

I am now using this:

$ cat /etc/environment
...
LC_ALL=en_US.UTF-8
LANG=en_US.UTF-8

Then log out of SSH session and log in again.

Old answer:

Only this helped me:

$ locale
locale: Cannot set LC_ALL to default locale: No such file or directory
LANG=en_US.UTF-8
LANGUAGE=
LC_CTYPE=en_US.UTF-8
LC_NUMERIC=ru_RU.UTF-8
LC_TIME=ru_RU.UTF-8
LC_COLLATE="en_US.UTF-8"
LC_MONETARY=ru_RU.UTF-8
LC_MESSAGES="en_US.UTF-8"
LC_PAPER=ru_RU.UTF-8
LC_NAME=ru_RU.UTF-8
LC_ADDRESS=ru_RU.UTF-8
LC_TELEPHONE=ru_RU.UTF-8
LC_MEASUREMENT=ru_RU.UTF-8
LC_IDENTIFICATION=ru_RU.UTF-8
LC_ALL=

$ sudo su

# export LANGUAGE=en_US.UTF-8
# export LANG=en_US.UTF-8
# export LC_ALL=en_US.UTF-8

# locale-gen en_US.UTF-8
Generating locales...
  en_US.UTF-8... up-to-date
Generation complete.

# dpkg-reconfigure locales
Generating locales...
  en_AG.UTF-8... done
  en_AU.UTF-8... done
  en_BW.UTF-8... done
  en_CA.UTF-8... done
  en_DK.UTF-8... done
  en_GB.UTF-8... done
  en_HK.UTF-8... done
  en_IE.UTF-8... done
  en_IN.UTF-8... done
  en_NG.UTF-8... done
  en_NZ.UTF-8... done
  en_PH.UTF-8... done
  en_SG.UTF-8... done
  en_US.UTF-8... up-to-date
  en_ZA.UTF-8... done
  en_ZM.UTF-8... done
  en_ZW.UTF-8... done
Generation complete.

# exit

$ locale
LANG=en_US.UTF-8
LANGUAGE=en_US.UTF-8
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=en_US.UTF-8

Source of the problem

I experienced this, logging in from one machine to another via ssh. The remote machine didn’t have the locale files, that I had on my local machine. You can either disable the forwarding of the locale from your local machine to the remote machine (in the file /etc/ssh/sshd_config remove the line AcceptEnv LANG LC_CTYPE …) or install the locale (changing it is not necessary in this case).

Installing

On Fedora, Red Hat Linux, and CentOS I used

sudo dnf install langpacks-de

for the German (de) language packs. I logged out, in, and it worked.

Search for other langpacks with

dnf search langpacks-

Changing/Activating

To list available locales I used

localectl list-locales

And to set a new one

sudo localectl set-locale de_DE.utf8

We will set locales that are not unset after reboot.

First open the Bash file and edit it:

nano .bashrc

Add these lines to the file:

export LC_ALL="en_US.UTF-8"
export LANG="en_US.UTF-8"
export LANGUAGE="en_US.UTF-8"

Activate the change by reloading Bash:

source ~/.bashrc

Test results:

locale

In my case, this was the output:

LANGUAGE = (unset),
LC_ALL = (unset),
LC_PAPER = "ro_RO.UTF-8",
LC_ADDRESS = "ro_RO.UTF-8",
....

The solution was:

sudo locale-gen ro_RO.UTF-8

In Arch Linux using a UK keyboard and locale, I had the following error:

perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
LANGUAGE = (unset),
LC_ALL = (unset),
LANG = "en_US.utf8"
  • Exporting my locales in /etc/profile didn't fix it.

  • I did however fix this by editing /etc/locale.gen, also enabling the en_US.utf8 locale that perl expected to find, and running local-gen.

(I use pac-manager which uses a whole bunch of perl modules from AUR, so reinstalling perl in my particular case would be a nuisance.)