LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Setting environmental variables (https://www.linuxquestions.org/questions/linux-newbie-8/setting-environmental-variables-587262/)

Ninju 09-25-2007 11:49 AM

Setting environmental variables
 
I'm sure this is a major noob question, but I'm not sure how to do it.

Basically, I'm having trouble installing RMagick, and the only solution I can find to the error I'm getting is to set an environmental variable. Now, the solution says to put this in one of my startup files:

export LD_LIBRARY_PATH=/usr/local/lib

Now, I run that in terminal and it doesn't make a noticeable difference (i.e. doesn't solve my problems). I've tried replacing lib with lib64, and I still get the error when trying to install RMagick, so I'm assuming this line needs to be put in a startup file as it says in the solution I read, or there is a special command in terminal I can run to set the variable. If I need to put it a startup file, err... which one? I don't want to break anything.

I'm running Fedora Core 5 (x86_64).

David the H. 09-25-2007 12:46 PM

If you use that line as-is, you simply replace the previous entry with '/usr/local/lib'. IOW, that becomes the only path available to that shell. You need to enter the entire list of paths you want (and in the order you want them searched):

export LD_LIBRARY_PATH="/usr/local/lib:/usr/lib"

You can see what the current setting of an environment variable is with 'echo $VARIABLENAME', or see all of them at once with the 'env' command.

By "startup files", this usually means /etc/bashrc, which is the basic system config file for bash. You can also put it in ~/.bashrc if you only want the setting to affect a single user.

And you don't have to worry about breaking anything, usually. Using the above line in a shell only sets the variable for that session; it doesn't affect the rest of the system. And global settings can easily be fixed by simply reversing the setting. At the very worst, if you find you've broken some necessary program or something, you might have to boot into administrative mode or use a different shell, such as /bin/sh, to fix things.

Ninju 09-25-2007 02:19 PM

Quote:

Originally Posted by David the H. (Post 2903395)
If you use that line as-is, you simply replace the previous entry with '/usr/local/lib'. IOW, that becomes the only path available to that shell. You need to enter the entire list of paths you want (and in the order you want them searched):

export LD_LIBRARY_PATH="/usr/local/lib:/usr/lib"

You can see what the current setting of an environment variable is with 'echo $VARIABLENAME', or see all of them at once with the 'env' command.

By "startup files", this usually means /etc/bashrc, which is the basic system config file for bash. You can also put it in ~/.bashrc if you only want the setting to affect a single user.

And you don't have to worry about breaking anything, usually. Using the above line in a shell only sets the variable for that session; it doesn't affect the rest of the system. And global settings can easily be fixed by simply reversing the setting. At the very worst, if you find you've broken some necessary program or something, you might have to boot into administrative mode or use a different shell, such as /bin/sh, to fix things.

Thank you so much. I did a couple other things, but I'm pretty sure what you've told me here has been the reason it's worked. I really can't thank you enough.

matthewg42 09-25-2007 04:29 PM

If you set the LD_LIBRARY_PATH in a user's ~/.bashrc, it is a good idea to add to the LD_LIBRARY_PATH, not replace it. i.e.
Code:

export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/lib"
The reason applies also to MANPATH, PATH and other lists, and is as follows. The system-wide shell configuration files, /etc/profile and /etc/bash.bashrc are read before the user-specific ~/.bashrc. Thus if the system administrator adds something to the LD_LIBRARY_PATH, perhaps for some application you use, you do not want to over-write it.

e.g. The system administrator installs a crucial package in /opt/important, and needs /opt/important/lib in the LD_LIBRARY_PATH for that application to run. He modifies the /etc/profile, setting LD_LIBRARY_PATH with this command:
Code:

export LD_LIBRARY_PATH="/opt/important/lib"
When you log in, this is executed first, and then your ~/.bashrc is executed. If you do this:
Code:

export LD_LIBRARY_PATH="/usr/local/lib"
...you will replace the original path set in the /etc/profile and the crucial app will cease functioning. You could of course explicitly set both paths in your ~/.bashrc like this:
Code:

export LD_LIBRARY_PATH="/opt/impoprtant/lib:/usr/local/lib"
...but maybe the system administrator would like to change this in the future, and it would be a pain to have to update your own ~/.bashrc each time (s)he does this. Hence the suggestion at the top of this post.

This sort of thinking is not so important if you are the sole user of your machine, but it's a good habit to get into - one day you may have to work in a large multi-user environment, and you should know how not to annoy your system administrators!


All times are GMT -5. The time now is 10:59 AM.