How to fix locale.Error: unsupported locale setting in Python

Python is a popular general purpose programming language that can be used to serve many different use cases. Created by Guido van Rossum with the name inspired from British comedy show Monty Python, it was set out to be straightforward and easy-to-use, emphasized in code readability. Python 3 is the latest version of the language and is considered to be the future of Python.

The set up of Python 3 on your Linux machine should be straightforward without any error, but sometimes you will encounter locale.Error: unsupported locale setting – one of the most common problem of Python. This tutorial will show you how to fix it explicitly on Ubuntu 18.04, but the process remains the same for other distro.

In which cases locale.Error happens?

The root cause is: your environment variable LC_ALL is missing or invalid somehow

➜  ~ pip install virtualenv
Traceback (most recent call last):
  File "/usr/bin/pip", line 11, in <module>
    sys.exit(main())
  File "/usr/lib/python3.4/site-packages/pip/__init__.py", line 215, in main
    locale.setlocale(locale.LC_ALL, '')
  File "/usr/lib64/python3.4/locale.py", line 592, in setlocale
    return _setlocale(category, locale)
locale.Error: unsupported locale setting

locale.Error could be triggered when you :

  • Using pip to install a package that make use of locale package on a newly installed machine
  • Using virtualenv to make a new virtual environment
  • You’ve been fiddling with the environment variables then try to use pip
  • Running programs built with Python for the first time after fresh OS installation.
  • Sometimes it happens with programs like ssha2enmod, and perl programs too.

How to fix locale.Error quickly?

If you want to solve it quick, you can sequentially run the commands below in the terminal :

export LC_ALL="en_US.UTF-8"
export LC_CTYPE="en_US.UTF-8"
sudo dpkg-reconfigure locales

The first two commands set the LC_ALL and LC_CTYPE environment variables, then the third one commits the changes to the system. After running these, a few command-line style window could probably pop up, you can just hit OK until it’s done, there is pretty much nothing to configure.

Here is a one-liner for you the lazy guys :

export LC_ALL="en_US.UTF-8" & export LC_CTYPE="en_US.UTF-8" & sudo dpkg-reconfigure locales

The output should look like this :

~$ export LC_ALL="en_US.UTF-8" & export LC_CTYPE="en_US.UTF-8" & sudo dpkg-reconfigure locales

[1] 7201
[2] 7202
[sudo] password for user: 
Generating locales (this might take a while)...
  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_IL.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... done
  en_ZA.UTF-8... done
  en_ZM.UTF-8... done
  en_ZW.UTF-8... done
  vi_VN.UTF-8... done
Generation complete.
[1]-  Done                    export LC_ALL="en_US.UTF-8"
[2]+  Done                    export LC_CTYPE="en_US.UTF-8"

Fix locale.Error permanently

While you can set the locale exporting an environment variable, you will have to do that every time you start a session, meaning after a restart, things would go back the same as before. Setting a locale the following way will solve the problem permanently.

First, you need to install locales package :

sudo apt-get install locales -y

Then use the locale-gen command to generate locales :

sudo locale-gen en_US.UTF-8

After that, permanently set the configuration to the system

sudo echo "LANG=en_US.UTF-8" > /etc/default/locale

You might also need to restart your system for changes to take effect.

Fix locale.Error for Docker

Just add these lines to your Dockerfile

Dockerfile# Set the locale
RUN locale-gen en_US.UTF-8
ENV LANG en_US.UTF-8
ENV LANGUAGE en_US:en
ENV LC_ALL en_US.UTF-8

After setting the locale, verify that the system is updated by using the following command :

~$ locale

LANG=en_US.UTF-8
LANGUAGE=
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
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading...

Leave a Reply

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