2

I know that I should apt-get things, but I'm asking this for binaries that aren't there, or the ones that I need the latest version. Every time I search about how to install these binaries on Ubuntu, I get different answers. Of course I can just paste the bin into /bin or the directories that are already in my PATH, but lots of apps are full of other files, so that shouldn't be the best way. 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? 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.

So, which is the right way? Also, if this has something to do with PATH, which is the correct way to add paths to the PATH? I always see lots of ways, and I get confused.

I'd like to end this once and for all, because I'm tired of installing things in different ways, and I wanted to do it in the correct and most elegant way.

muru
  • 197,895
  • 55
  • 485
  • 740
Guerlando OCs
  • 863
  • 10
  • 51
  • 88

2 Answers2

5

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

Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497
  • Yes, usually I just run the .deb, but in lots of cases, they only offer binaries, like in gradle for example, and I have to add it to my path, so I wonder what will happen when I have like 100 apps in the path, it'll get messy, this should not be the right way. Also, about /opt: I used to install apps there, like Telegram, then I noticed that telegram wouldn't ever update itself because that folder had permissions that my user hasn't. Is it good to install things there? – Guerlando OCs Oct 31 '16 at 05:19
  • @GuerlandoOCs I'm actually slowly editing my answer to address some parts of your question. /opt is good place to save software -it's sort of neutral ground. Binaries don't usually update themselves, so i don't think this has to do anything with permissions. But yes, /opt is owned by root. Again, there's no really right or wrong answer - it depends on what you're doing – Sergiy Kolodyazhnyy Oct 31 '16 at 05:22
  • @GuerlandoOCs ok, i think I'm done editing. Please let me know if you have other questions. There's no voodoo or magic involved in installing software in linux - there are specific decisions to be made, and each of them has purpose. I hope I sufficiently have addressed that. – Sergiy Kolodyazhnyy Oct 31 '16 at 05:32
  • well, it would be good to respect the FHS...! -- for self-updating (without root-permissions) your home-folder would be the only choice... but this is not really recommended! – DJCrashdummy Oct 31 '16 at 05:57
  • 1
    @DJCrashdummy True, that would be better to respect the hierarchy , but it's not always possible. I personally see self-updating is sort of security hole , which is probably main reason why it's frowned upon. That's one. Second, if you're an administrator of your machine , then you probably do need to assume certain level of responsibility and actually use those root permissions. That's one of the things I dislike about the Windows world - people want to "drive" the machine without being "the driver" and assume responsibility – Sergiy Kolodyazhnyy Oct 31 '16 at 06:06
  • The idea of automatically adding all the subdirectories is good! And I understood everything, thank you so much – Guerlando OCs Oct 31 '16 at 19:34
1

Disclaimer: What follows below is my personal opinion.

In Ubuntu, the ~/.profile has:

if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi

which means if the ~/bin directory is present, add that to the PATH, at start.

So one should put all the user executables in ~/bin directory, this assures:

  • Maintainability
  • Cleanliness
  • User specific PATH

Also, if there exists some executables that can not be put in there, you could easily create a symbolic link on this directory that refers to that executable.

heemayl
  • 91,753
  • So the best way you think should be to add all apps in like, /home/Apps, and create symlinks to them in /home/bin? – Guerlando OCs Oct 31 '16 at 05:06
  • @GuerlandoOCs You app can reside anywhere, but the executables can be symlinked on /home/USERNAME/bin/ directory. – heemayl Oct 31 '16 at 05:07