Install Software to ~/opt Directory

Created On: 2016-11-27

In Linux, when you compile a software from source, you can usually specify where to install it to in configure step or make install step. When you don't have root privilege, a good place to install software is your home dir. Here is how you do it:

At configure step:

./configure --prefix=$HOME/opt

Or if your prefer, at make install step:

make install DESTDIR=$HOME/opt

If you compile an end-user program, for example, mplayer or emacs, that's all you need to do. You can run your program using ~/opt/bin/<xxx>.

However, if you compile a library, you may find that other software or compiler may not be able to find the installed library. Here is what you need to do to make it fully work:

Add in your ~/.bashrc file:

# add vars so you can install software to $HOME/opt, and the compiler,
# linker and loader won't complain.

if [ -z "$HOME_OPT_ADDED" ]; then
        # add ~/opt dir to the env for compilers and linkers
        # ref: http://www.network-theory.co.uk/docs/gccintro/gccintro_23.html
        if [ "$C_INCLUDE_PATH" ]; then
                export C_INCLUDE_PATH=$HOME/opt/include:$C_INCLUDE_PATH
        else
                export C_INCLUDE_PATH=$HOME/opt/include
        fi
        if [ "$CPLUS_INCLUDE_PATH" ]; then
                export CPLUS_INCLUDE_PATH=$HOME/opt/include:$CPLUS_INCLUDE_PATH
        else
                export CPLUS_INCLUDE_PATH=$HOME/opt/include
        fi
        #for linking
        if [ "$LIBRARY_PATH" ]; then
                export LIBRARY_PATH=$HOME/opt/lib:$LIBRARY_PATH
        else
                export LIBRARY_PATH=$HOME/opt/lib
        fi

        # man page
        if [ "$MANPATH" ]; then
                export MANPATH=$HOME/opt/share/man:$MANPATH
        else
                if which manpath &> /dev/null; then
                        export MANPATH=$HOME/opt/share/man:`manpath`
                fi
        fi

        #for finding and loading shared library
        export PATH=$HOME/opt/bin:$PATH
        if [ "$LD_LIBRARY_PATH" ]; then
                export LD_LIBRARY_PATH=$HOME/opt/lib:$LD_LIBRARY_PATH
        else
                export LD_LIBRARY_PATH=$HOME/opt/lib
        fi

        #prevent duplicate entries when re-sourcing ~/.bashrc
        export HOME_OPT_ADDED=1
fi
Is this post helpful?