Setting Environment Variables in Linux

I have an application that relies on environment variables to find shared object files. If these are not in place, I get an error like “lib.so: cannot open shared object file: No such file or directory.”

If I set these variables the following way, it works temporarily:

export VARIABLE_NAME=/path/abc

But I want it to persist for all sessions so I don’t have to keep setting it.

To make this change persist for every time you login to Linux (but only for your account), you need to modify the file at ~/.bash_profile. You can do this by typing the following at the command prompt:

vi ~/.bash_profile

To understand how to use vi, go here. (Or you could always use something like gedit instead if that is installed on your system.)

If you have root privileges and want these changes to apply to all users, you need to modify the /etc/profile file in a similar way.

vi /etc/profile

Within either of these files, you would put a command such as the following:

export VARIABLE_NAME=/path/abc

Be careful about where you put it in the file. Usually it’s best to put it where you see other environment variables being set.

Leave a Reply