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 wp-block-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.

When does 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/", line 215, in main locale.setlocale(locale.LC_ALL, '') File "/usr/lib64/python3.4/", line 592, in setlocale return _setlocale(category, locale) locale.Error: unsupported locale setting
Code language: Bash (bash)

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 ssh, a2enmod, 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
Code language: Bash (bash)

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
Code language: Bash (bash)

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"
Code language: Bash (bash)

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
Code language: Bash (bash)

Then use the locale-gen command to generate locales :

sudo locale-gen en_US.UTF-8
Code language: Bash (bash)

After that, permanently set the configuration to the system

sudo echo "LANG=en_US.UTF-8" > /etc/default/locale
Code language: Bash (bash)

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
Code language: Dockerfile (dockerfile)

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

Code language: Bash (bash)


How to fix locale.Error: unsupported locale setting quickly?

Open up Terminal app and enter this command (without the single quote)
‘export LC_ALL=”en_US.UTF-8″ & export LC_CTYPE=”en_US.UTF-8″ & sudo dpkg-reconfigure locales’

Why does locale.Error happens?

Invalid or missing LC_ALL system variable

How to fix locale.Error once and for all?

Install locales package : sudo apt-get install locales -y
Then, generate locales : sudo locale-gen en_US.UTF-8
Finally, write it to system configuration : sudo echo “LANG=en_US.UTF-8” > /etc/default/locale

1 thought on “How to fix locale.Error: unsupported locale setting in Python”

  1. The command needs to be

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

    with double ampersands (single ampersands put a process in background, but what you want is a chain of commands in sequence, and that needs double ampersands).

    And the command ‘sudo echo “LANG=en_US.UTF-8” > /etc/default/locale’ doesn’t work either. The “sudo” prefix only executes the “echo” command with admin rights, but does not have any effect on the output redirection, which is still executed with user rights and fails.

    The correct command is

    echo “LANG=en_US.UTF-8” | sudo tee /etc/default/locale


Leave a Comment