Tuesday, 20 September 2011

Working with .profile

Most of the magic that goes into making things work nicely goes in the .profile file in your home directory. In this file you can tell linux how you have arranged your files and how you want to work. Setting this up correctly will make things a breeze. For those who do not know .profile is a file that contains a bunch of shell script commands that are executed every time you start up a shell.

Firstly - why .profile? Why not .chsrc or .bashrc or .login or any of the other files? Well, the reason is simple - .profile gets executed in such a way that it affects all programs that you run in Ubuntu, be it from a command-line terminal or from the GUI. If you are using a different flavour of linux then you may have to look at how the startup process runs to find the best place to set environment variables, but .profile should work with all types of linux.

So, to get things going here is my .profile:

# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.

# the default umask is set in /etc/profile; for setting the umask
# for ssh logins, install and configure the libpam-umask package.
#umask 022

# if running bash
if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
	. "$HOME/.bashrc"
    fi
fi

CHECK_64=`uname -a | grep x86_64`


if [ -n "${CHECK_64}" ]; then
    export ARC_POSTFIX=64
    export ARC=linux64
else
    export ARC_POSTFIX=32
    export ARC=linux
fi

export C_INCLUDE_PATH="${HOME}"/include
export CPLUS_INCLUDE_PATH="${C_INCLUDE_PATH}"
export INCLUDES=-I"${C_INCLUDE_PATH}"

export RAVL_INSTALL="${HOME}"/
export PROCS=4
export PROJECT_OUT="${HOME}"/.ravl_out

export LIBS=-L"${HOME}"/lib/
export LIBRARY_PATH="${HOME}"/lib:"${PROJECT_OUT}"/lib
export LD_LIBRARY_PATH="${LIBRARY_PATH}"

export PATH=./:"${PROJECT_OUT}"/bin:"${HOME}"/bin:"${PATH}"

export PYTHONPATH="${HOME}"/.ipython/:"${HOME}"/lib/python/

export ASPELL_CONF="master en_GB"
export GREP_OPTIONS=--exclude-dir=.svn
export OSG_FILE_PATH="${HOME}"/share/OpenSceneGraph-Data
export CMAKE_PREFIX_PATH=${HOME}

OK, so that looks pretty complicated, let's break it down and see what we are doing. Firstly, all the stuff up until the first export statement is just the standard stuff that gets put in .profile by Ubuntu. So we can ignore that. Lets now look at the export statements and see what they do.

export C_INCLUDE_PATH="${HOME}"/include
export CPLUS_INCLUDE_PATH="${C_INCLUDE_PATH}"
export INCLUDES=-I"${C_INCLUDE_PATH}"

These statements are setting up various compilers to automatically look in ~/include for header files. Now anything we put in ~/include will automatically get picked up by pretty much any build system that uses gcc.

export RAVL_INSTALL="${HOME}"/
export PROCS=4
export PROJECT_OUT="${HOME}"/.ravl_out

These next statements are specific to RAVL (a computer vision library we use here at the University of Surrey). RAVL is set up to build to wherever the $PROJECT_OUT environment variable points. Here I am setting it to a hidden directory, and I am setting it here so I can reference that value later.

export LIBS=-L"${HOME}"/lib/
export LIBRARY_PATH="${HOME}"/lib:"${PROJECT_OUT}"/lib
export LD_LIBRARY_PATH="${LIBRARY_PATH}"

These are some of the more important statements in .profile. These are telling the compiler and the runtime system to look in ~/lib for libraries. By setting LD_LIBRARY_PATH we are telling the system to look in ~/lib before it looks in /usr/lib. Now there is a potential security risk here which is why many websites will recommend that you do not use LD_LIBRARY_PATH (the risk is that it is easier for malicious code to get into ~/lib than into /usr/lib). Personally I think the risk is higher if you are constantly copying things in to /usr/lib, but this is something to be aware of. Another issue here is that some of the startup files in the X11 system (specifically the ones to do with ssh) strip LD_LIBRARY_PATH from the environment at load time precisely because of this security issue. If you are happy and understand the risks then you can go here to see how to fix that. Otherwise you will just have to start things from a command prompt to be able to run your code nicely and easily.

export PATH=./:"${PROJECT_OUT}"/bin:"${HOME}"/bin:"${PATH}"

This line sets the path which is where linux looks to find commands to execute; useful for running your programs! Here I am setting it up to run stuff I compile with the RAVL QMake build system, and stuff I compile to ~/bin. Note it is important to include the default ${PATH} variable on the end otherwise you won't be able to run anything installed on your machine!

export PYTHONPATH="${HOME}"/.ipython/:"${HOME}"/lib/python/

This line sets the PYTHONPATH. If you are using python you will find this invaluable in addition to setting up the .pydistutils.cfg which I will cover later. These files tell python where to look for stuff you have installed (and in the case of .pydistutils.cfg - where to install stuff)

export ASPELL_CONF="master en_GB"
export GREP_OPTIONS=--exclude-dir=.svn
export OSG_FILE_PATH="${HOME}"/share/OpenSceneGraph-Data
export CMAKE_PREFIX_PATH=${HOME}

These final options are just a bunch of application specific settings - setting the Aspell dictionary, telling Grep to ignore Subversion files, telling osg where to look for data files and telling CMake where to look for stuff.

So now you should have all the ingredients to work from your home directory, both building your own code, 3rd party apps, and bleeding edge projects whose source you have downloaded off the internet. Next we will look at  building some of our own code and a project from the internet.

No comments:

Post a comment