apt-get error: "locale: Cannot set LC_CTYPE to default locale: No such file or directory"

When running apt-get I receive error locale: Cannot set LC_CTYPE to default locale: No such file or directory. What can I do to fix this error?

I am connecting from a box running Mac OS X.


Solution 1:

There is definitely something wrong with OS X (May be Lion only)

What is the problem?

OS X does not correctly set the locale environment. Also, OS X calls the UTF-8 charset in the combined syntax of locales differently than Linux.

How to fix it?

You can find numerous "fixes" on Google, most of them do not actually work or are just part of the problem, because they are meant for a different problem creating the same or similar errors.

I think the most reasonable way is to fix your local environment on the OS X client. If you run the locale command on your OS X client, it shows an incomplete environment. The language is not set.

osx-client:~$ locale
LANG=           << EMPTY!
LC_COLLATE="C"
LC_CTYPE="UTF-8"
LC_MESSAGES="C"
LC_MONETARY="C"
LC_NUMERIC="C"
LC_TIME="C"
LC_ALL=         << EMPTY!

So in the ~/.bash_profile file on your local machine, just add

export LANG="en_US"
export LANGUAGE=$LANG
export LC_ALL=$LANG

You can of course replace the en_US with your desired language, just make sure to install its locale on the server too (for example on Debian with dpkg-reconfigure locales). Do not forget to close and re-open your Terminal.

However do not use the .UTF variants. They will only either work on the server or locally. OS X and your Linux server have a different name for UTF-8 in the combined language.charset syntax. If you use for example en_US.utf8 it will cause an error locally while it works "fine" remotely, while using en_US.utf-8 will work locally but cause the error on your server. Just use the plain language definition. The character set should end up being UTF-8 anyway.

You can test whether it works by just running locale again on the server, if you see the dreaded message at the top, it does not work. If you messed up locally you will see an error right when opening a new Terminal window.

Missing locales on the Server

If it is still not working, you may be missing your selected locale on the server. You can be hardly wrong with "en_US" but may want a different one. How to install a locale depends on your servers distribution.

  • On Debian, call dpkg-reconfigure locales then select your locale.
    • If you are asked to select a default language you can select one or leave it at none. It does not matter here.

Solution 2:

The resolution is simple if you're running Mac OS X while seeing this error through ssh:

sudo nano /etc/ssh_config Comment out the following line: "SendEnv LANG LC_*"

Solution 3:

Run dpkg-reconfigure locales on your Ubuntu, make sure at least one of the selection is selected. If you are not sure what to pick I suggest en_US.UTF-8.

If you are connecting via SSH from a system, then you will want to make sure you have generated the locales used by the client systems that will be connecting. You can select as many to locales to generate as you want/need.

As you mentioned in your answer, you can certainly choose to prevent SSH for either sending or accepting the LC* variables.

From an ssh client disable this.

ssh_config:    SendEnv LANG LC_*

On the SSH server you can block accepting the variables by removing this.

sshd_config:AcceptEnv LANG LC_*