Purpose of PATH variable
PATH
is meant for your shell - it tells the shell where commands you type in are supposed to live. Good example of that was on OpenSUSE box I once had. On Ubuntu you can type in iconfig
command and bash will go " oh, I know where that is - i'll run /sbin/ifconfig
". Well, on OpenSUSE default PATH
, doesn't have /sbin
added to it, so bash
will say "sorry, I don't know where ifconfig
is , I've no record of such file." So you have to either run /sbin/ifconfig
( i.e. full path ) or do it in a smart way - add /sbin
to PATH
.
Good example of why you would want to do is when you install custom scripts or binaries. Let's say you have saved something in /opt/my_stuff
folder , and you want just to call my_command
by name. Well, you will have to add /opt/my_stuff
to PATH
.
How to properly add directories to PATH
The basic idea is that you want to append or join original $PATH
with the new directory. As you may or may not know, items in PATH
variable are delimited with :
. You also want to export
the variable, so that other programs ( children of your shell ) know where to look for stuff. All that stuff must happen at the end of ~/.bashrc
, so that when you open shell and it has read your ~/.bashrc
config, it knows where everything is located.
Here's the example from my ~/.bashrc
:
export PATH=$PATH:"/opt/microchip/xc16/v1.25/bin"
In this case , original $PATH
will be expanded to a string of directories. We simply add one more to it, and save it back. Nothing complicated.
What does this have to do with installing software
If you are installing a file from .deb
archive or .run
script, you shouldn't worry about the PATH
part. It's the responsibility of software author to simplify installation process. Typically, they'll configure their installation to save stuff into one of the common directories that already belong to PATH
: installation script will say, "hey, install this software into /usr/bin` and you will be able to use it without extra effort.
If software authors install it somewhere that's not typically in PATH
, they will have to configure your PATH
variable or you have to do it yourself. In the example above, the directory /opt/microchip
was created when I was installing Microchip's IDE and compiler. Their installer automatically put that line into my ~/.bashrc
. I often write scripts that go into /opt
, so I usually mention in the instructions that users have to configure PATH
themselves.
The ~/bin directory
You frequently see people recommending to save script and other types of software into ~/bin
. This folder belongs to you - your user, so if you don't want to share app with other people on your machine - that's where you'd put it. Difference with /opt
is that as I mentioned already in a few places , /opt
is neutral ground. So if I as system administrator want both my family to also have access to an app with their accounts - that's where I'd put it.
As far as bash
shell goes, it already handles ~/bin
being added to PATH
for you. Other shells, like /bin/sh
, or tcsh
or ksh
won't do it, so keep that in mind if you are working on different system or want to use a different shell in your Ubuntu.
Addressing specific parts of question
I often see instructions telling me to add the folder of the app in the PATH, but what would happen if I install like, 100 apps with this technique? Wouldn't my PATH variable get big and messy?
Not really - what's supposed to go into path is directories. If you typically install software in single directory ( such as ~/bin
or /opt
) it will require adding only /opt
. If each software has to live in its own folder under /opt
, then yes - you'd have to add each and every single one to PATH
. What you can do , however, is write a script to add all directories to PATH
. So , let's say:
$ for item in /opt/* ; do
if [ -d "$item" ]; then
PATH="$PATH:$item"
echo "$VAR"
fi
done
And this can live perfectly fine as function at the end of your ~/.bashrc
, so that each time you open the shell, all directories under /opt
are added automatically.
In my example, I write a lot of scripts. They all live in ~/bin
since all of them are just single files, and there's no need for them to live in separate directories.
Also, I'd be passing this PATH to every app I open, I don't see this as the better way to install things in Linux.
That's what the export
variable is for. It passes stuff to child processes of your shell. You as user don't have to do anything. Also, as I mentioned, it's not typically done - software authors create packages that go into directories that are already in PATH
like /usr/bin
.
The directories that are not in /opt
are just a neutral ground and sort of a way for developers to say "hey, our app is 3rd party, so it's not something default."
Using GUI apps without PATH variable
Once thing that haven't been covered here is .desktop
files. To put simply , these are Linux's version of Windows shortcut , but on steroids. Lets say I've build a GUI app in python, and I want it to live in /opt
folder, but I don't want user to call it from command line - I want them to use a nice and neat desktop shortcut. Well, that's where .desktop
files come into play. Here's an example:
$ cat /home/xieerqi/.local/share/applications/vivaldi.desktop
[Desktop Entry]
Encoding=UTF-8
Version=1.0
Type=Application
Name=周迅 mv - YouTube
Icon=vivaldi
Path=/home/xieerqi
Exec=/opt/vivaldi/vivaldi-bin --ppapi-flash-path --ppapi-flash-version --always-authorize-plugins --enable-npapi --no-first-run
StartupNotify=false
StartupWMClass=Vivaldi
OnlyShowIn=Unity;
X-UnityGenerated=true
While vivaldi
app lives happily in /opt
without ever needing to be referenced in PATH
, I can use a .desktop
shortcut to launch it. Again, you'd need to manually create one for each and every app, but as you can guess Linux has scripting ways to automate even that.
Conclusion
So what is the real and correct way to install software on Linux ? There is none, just like there isn't a way on Windows or Mac OS X. Your Windows app could be single .exe
or a folder with .exe
and live perfectly fine in C:\
folder. Same here - on Linux you can have a script or whole suite of software live in /opt
, or ~/bin
or wherever you choose. The choices , of course, must to some extend be justified. Do you want the app to be available to root
only, to you only
, or shared ? Do you want it to be accessible via just its name, or do you want to type in full path to the app ?
Additional information