Fixing “Setting locale failed” error in Ubuntu Server

When I was redeploying my IntoVPS Server and upgrading to the newest version of Ubuntu Server, I came across a number of issues with locales not being configured correctly. The error usually looks something like this:

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").
locale: Cannot set LC_CTYPE to default locale: No such file or directory
locale: Cannot set LC_MESSAGES to default locale: No such file or directory
locale: Cannot set LC_ALL to default locale: No such file or directory

You’re getting this error because no locales have been configured. To fix it, firstly ensure that you have the locales package installed:

$ apt-get install locales

If locales is not installed, you will get a screen which will ask you which locales you wish to enable. If locales is already installed, then type the following to commands to regenerate locales.

$ locale-gen en_US en_US.UTF-8 en_AU en_AU.UTF-8
$ dpkg-reconfigure locales

If you want to use other locales, you can re-run the locale-gen and dpkg-reconfigure locales commands.

Autoloading 101

For some who are new to PHP 5 you may have heard about a PHP feature called SPL Autoload or Autoloading classes.

Ordinarily, when you write a class your code may look something like this:

<?php

require_once('../code/foo.php');
require_once('../code/baz.php');

class widget extends foo
{
    public function event()
    {
        $baz = new baz();
    }
}

Autoloading does away with the need to put require / include once to include class file by enabling you to attach a function that performs an action before a class is instantiated. SPL autoload works when you try to instantiate a class or use reflection on a class and only works when the class hasn’t already been loaded.

Continue reading