Change the default directory of one specific program without changing its path

Option 1: Make an override folder on your path

If you need these programs to be called in indirect ways (like by some application started by the window manager will call g++ or python, for instance), you should edit your path. You could simply add a new folder to the beginning of your path in your ~/.bashrc:

export PATH=/home/username/.bin:$PATH

and place two symbolic links to point to the appropriate programs:

ln -s /usr/bin/python /home/username/.bin/python
ln -s /usr/local/bin/g++ /home/username/.bin/g++

That way, once your ~/.bashrc is properly sourced (log out, then log back in), everything should find the right python and the right g++.

Option 2: Use an alias for bash to follow

If you are looking for a lighter weight solution, and if you only call python directly from bash, you could setup an alias in your ~/.bashrc:

alias python=/usr/bin/python

Option 3: Just change the name of python in /usr/local/bin/

Or you could always just rename /usr/local/bin/python to be /usr/local/bin/python-alternate or something. I wouldn't suggest renaming things in /usr/bin, since at least in Debian that is controlled by a package manager. Usually /usr/local/bin isn't.

Option 4: Specify the correct compiler in the Makefile

If your workflow uses make, or some broader application that calls make (such as autotools or cmake), there is almost always an option to specify your compiler. For instance, your makefile could look like:

CXX=/usr/local/bin/g++

all:
    $(CXX) inputfile.cpp -o outputfile

or with cmake you might configure with

cmake -D CMAKE_CXX_COMPILER=/usr/local/bin/g++ ..

Different programs will have different syntaxes for specifying the compiler, but you can most always specify it.

Tags:

Linux

Path