Game of Paths ! [linux/unix]
We are often confused / lost in the paths at the initial stage of setting up few softwares or installing a new version of a particular language.
So, today I’ll be talking about few commands that can help us get hold of the situation when things get out of hand.
I happened to be in a situation when I was very frustrated with what is wrong with my Anaconda installation. Everything down here helped me debug this and problems popped up later when i had to configure Hadoop and related tools.
echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin
- This little command over here tells us where all can my OS find the executable or scripts (PATHS are separated by a colon
:
here) so that you can run it by its name as a command. - OS parses the PATH variable sequentially i.e. first it will look for binaries in
/usr/local/bin
then in/usr/bin
. This is the main reason, because of which we have to prepend$PATH
for Anaconda .
which python
/usr/bin/python
Now, let’s modify the path.
Whenever we fire up bash/terminal there are few scripts that always run. I am talking precisely about the config file for your bash here. Here we can add a simple command to modify the path set by the core system.
Note: for my OS I have to change in ~/.profile
. For others it may be ~/.bashrc
or ~/.zshrc
(~
is for home directory by default in linux)
We now need to edit this config file to modify the path. We can do it by typing gedit ~/.profile
or vi ~/.profile
which ever text editor we are comfortable with and add the following line in the file (I have anaconda3 installed in this directory and /bin in anaconda directory contains the binaries it manages) :
export PATH="/Users/sid/anaconda3/bin:$PATH"
As we come back to terminal type:source ~/.profile
this is to load the changes to my current bash.
which python
/Users/sid/anaconda3/bin/python
Now we can see that as $PATH has been modified. First directory is parsed and OS chooses python present in anaconda directory as its default.
What if I appended the path instead of prepending it??
ie export PATH="$PATH:/Users/sid/anaconda3/bin"
which python
/usr/bin/python
Again, we see that I get my old result only. This is because of sequential parsing of paths.
Thus, it’s very important to note the order of paths we declare in $PATH
as these are parsed sequentially and OS uses the first found binary/script as default for executing as a command.
Stay tuned for more!