Fix “ValueError: unknown locale: UTF-8″ under Mac OS X 10.8 Mountain Lion

Today I was trying the Writing your first Django app tutorial, and when I run the syncdb command I got this error:

ValueError: unknown locale: UTF-8

The solution was really simple I just needed to export some variables in my bash enviroment. So I edited my .bash_profile file adding these lines:

export LANG="es_ES.UTF-8"
export LC_COLLATE="es_ES.UTF-8"
export LC_CTYPE="es_ES.UTF-8"
export LC_MESSAGES="es_ES.UTF-8"
export LC_MONETARY="es_ES.UTF-8"
export LC_NUMERIC="es_ES.UTF-8"
export LC_TIME="es_ES.UTF-8"
export LC_ALL=

where es_ES.UTF-8 should be your locale string. To test if it worked for you just reload the console and run this code:

python -c 'import locale; print(locale.getdefaultlocale());'

You should get something like this:

('es_ES', 'UTF8')
Advertisements

Install Django on OS X 10.8 Mountain Lion

You can verify that Python is already installed by typing python from your shell; you should see something like:

Python 2.7.2 (default, Jun 20 2012, 16:23:33)
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

The recommended way to install Django is installing an official release with pip. First we need to install pip:

sudo easy_install pip

Then install python:

sudo pip install django

To verify that Django can be seen by Python, type python from your shell. Then at the Python prompt, try to import Django:

>>> import django
>>> print django.get_version()
1.4

To create a new project, you need to change permissions on file django-admin.py

sudo chmod +x /Library/Python/2.7/site-packages/django/bin/django-admin.py

Now you’re ready to create a new project:

django-admin.py startproject mysite

Enable Apache virtual hosts on OS X 10.8 Mountain Lion

I like to place each of my projects in a subfolder of Sites. The Sites folder is no longer present in OS X 10.8, but you can create it manually from the Terminal:

mkdir ~/Sites

For instance, for the project that I will be hosting locally at http://test.site, I created a folder called test.site:

mkdir ~/Sites/test.site

First, let’s add the test.site domain to hosts file:

sudo nano /etc/hosts

At the end of the file, add:

127.0.0.1       test.site

Next step, edit /etc/apache2/httpd.conf, and uncomment:

# Virtual hosts
Include /private/etc/apache2/extra/httpd-vhosts.conf

Now, edit Apache’s virtual hosts configuration file httpd-vhosts.conf, and place the following:

NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot "/Library/WebServer/Documents"
    <Directory "/Library/WebServer/Documents">
        Allow From All
        AllowOverride All
    </Directory>
    CustomLog "|/usr/sbin/rotatelogs /private/var/log/apache2/default-access_log 86400" combined
    ErrorLog "|/usr/sbin/rotatelogs /private/var/log/apache2/default-error_log 86400"
</VirtualHost>
<VirtualHost *:80>
    ServerName "test.site"
    DocumentRoot "/Users/damiano/Sites/test.site"
    <Directory "/Users/damiano/Sites/test.site/">
        Allow From All
        AllowOverride All
    </Directory>
    CustomLog "|/usr/sbin/rotatelogs /private/var/log/apache2/test.site-access_log 86400" combined
    ErrorLog "|/usr/sbin/rotatelogs /private/var/log/apache2/test.site-error_log 86400"
</VirtualHost>

The first entry points to the default web site, which will also work as a catch-all host for any virtual host that wasn’t defined anywhere else. The second entry points instead to the~/Sites/test.site folder created for http://test.site

Finally, restart Apache to load the new configuration:

sudo apachectl restart

Set up Git and customise iTerm2 to display the current branch with fancy colours

Git is an excellent distributed version control system for your projects projects.

To download Git and learn how to install and set up, go here.

Mac OS X includes a great terminal editor, but among the handful of alternatives is one that easily rises above them all. iTerm2 comes with a vast number of time-saving features and high level of customisability.

As an example here I explain how to customise iterm with the cool colours that Paul Irish uses in his videos (thanks to @digitalformula).

This example, guessing what Paul uses the various CLI prefix symbols for, uses “o” to indicate that we are in a standard directory, and “±” to indicate that we are in a git branch directory.

To make this happen, you need to copy the file .git-completion from the git source to your home (~) directory

cp /usr/local/git/contrib/completion/git-completion.bash ~/.git-completion.sh

Then open or create a file called .bash_profile also in your home directory, and add the following lines:

# enable the git bash completion commands
source ~/.git-completion.sh

# enable git unstaged indicators - set to a non-empty value
GIT_PS1_SHOWDIRTYSTATE="."

# enable showing of untracked files - set to a non-empty value
GIT_PS1_SHOWUNTRACKEDFILES="."

# enable stash checking - set to a non-empty value
GIT_PS1_SHOWSTASHSTATE="."

# enable showing of HEAD vs its upstream
GIT_PS1_SHOWUPSTREAM="auto"

BLACK=$(tput setaf 0)
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
LIME_YELLOW=$(tput setaf 190)
POWDER_BLUE=$(tput setaf 153)
BLUE=$(tput setaf 4)
MAGENTA=$(tput setaf 5)
CYAN=$(tput setaf 6)
WHITE=$(tput setaf 7)
BRIGHT=$(tput bold)
NORMAL=$(tput sgr0)
BLINK=$(tput blink)
REVERSE=$(tput smso)
UNDERLINE=$(tput smul)

# set the prompt to show current working directory and git branch name, if it exists

# this prompt is a green username, black @ symbol, cyan host, magenta current working directory and white git branch (only shows if you're in a git branch)
# unstaged and untracked symbols are shown, too (see above)
# this prompt uses the short colour codes defined above
# PS1='${GREEN}\u${BLACK}@${CYAN}\h:${MAGENTA}\w${WHITE}`__git_ps1 " (%s)"`\$ '

# this is a cyan username, @ symbol and host, magenta current working directory and white git branch
# it uses the shorter , but visibly more complex, codes for text colours (shorter because the colour code definitions aren't needed)
# PS1='\[33[0;36m\]\u@\h\[33[01m\]:\[33[0;35m\]\w\[33[00m\]\[33[1;30m\]\[33[0;37m\]`__git_ps1 " (%s)"`\[33[00m\]\[33[0;37m\]\$ '

# return the prompt prefix for the second line
function set_prefix {
    BRANCH=`__git_ps1`
    if [[ -z $BRANCH ]]; then
        echo "${NORMAL}o"
    else
        echo "${UNDERLINE}+"
    fi
}

# and here's one similar to Paul Irish's famous prompt ... not sure if this is the way he does it, but it works  🙂
# 33[s = save cursor position
# 33[u = restore cursor position

PS1='${MAGENTA}\u${WHITE} in ${GREEN}\w${WHITE}${MAGENTA}`__git_ps1 " on %s"`${WHITE}\r\n`set_prefix`${NORMAL}${CYAN}33[s33[60C (`date "+%a, %b %d %H:%M"`)33[u${WHITE} '

Install Java in Mac OS X 10.8 Mountain Lion

The last Mac OS X 10.8 Mountain Lion doesn’t come with Java pre-installed, but it has a “false” executable that starts the installation.

Just open a terminal, and type

java

The following message should appear:

No Java runtime present, Requesting install

The installation starts normally

Launch Sublime Text 2 from the Mac OS X Terminal

Sublime Text 2  is a sophisticated text editor for code, markup and prose. You’ll love the slick user interface, extraordinary features and amazing performance.

Launching Sublime Text 2 from the Terminal is possible as it ships with a CLI called subl. The first task is to make a symlink to subl. Assuming you’ve placed Sublime Text 2 in the Applications folder, you can run:

ln -s "/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl" /usr/local/bin/sublime

Now let’s do a check to see if everything will run smoothly. Enter this:

open ~/.bash_profile

This contains all the directories that will be looked into for executable binaries when you type a command in Terminal. Since we create a symlink to subl called sublime in the /usr/local/bin directory, make sure it’s added to your path. If it is, perfect. Let’s keep going. If not, simply add it:

PATH=$PATH:/usr/local/bin
export PATH

If you had to add /usr/local/bin to your PATH, run the following command before continuing:

source ~/.bash_profile

This will reload your .bash_profile with the newly added directory. Now everything is ready, so run:

sublime filename

or

sublime foldername

or even

sublime .

to open the entire current directory.

Have fun, Sublime is a great editor showing a lot of promise.

Configure Apache and PHP on OS X 10.8 Mountain Lion

Apache/2.2.22 is pre-installed in Mountain Lion. To check the version installed:

httpd -v

To start | stop | restart Apache:

sudo apachectl start|stop|restart

After starting Apache you should see the “It Works!” text in the browser – http://localhost.

OS X has historically had 2 document roots, one at system level, and one at user level – the user level one allows multiple acounts to have their own web root, whilst the system one is global for all users.

The default system document root is found at /Library/WebServer/Documents/ – http://localhost/.

The user document root level should be found at the ‘~/Sites’ folder in the User account on a clean installation, but is missing in Mountain Lion,  so you need to make a “Sites” yourself:

cd /Users/username
mkdir Sites

Check that you have a “username.conf” filed under: /etc/apache2/users/. If you don’t (very likely), then create one:

cd /etc/apache2/users
sudo nano username.conf

Then add the content below:

<Directory "/Users/username/Sites/">
  Options Indexes MultiViews
  AllowOverride All
  Order allow,deny
  Allow from all
</Directory>

Permissions on the file should be:

-rw-r--r--   1 root  wheel  298 Jun 28 16:47 username.conf

Then this user level document root will be viewable at: http://localhost/~username/

PHP 5.3.13 is loaded in OSX 10.8 Mountain Lion and needs to be turned on by uncommenting a line in the httpd.conf file.

sudo nano /etc/apache2/httpd.conf

Uncomment the line (remove the #):

LoadModule php5_module libexec/apache2/libphp5.so

To configure php, create php.ini file:

sudo cp /etc/php.ini.default /etc/php.ini
sudo chmod ug+w php.ini
sudo nano /etc/php.ini

Re-load Apache.