This page describes the process of setting up environment variables in Ubuntu.
Session-wide environment variables
In order to set environment variables in a way that affects a user’s entire desktop session, one may place commands to set their values in one of the “hidden” script files in the user’s home directory. The more common such files are outlined below.
- ~/.profile – This is probably the best file for placing environment variable assignments in, since it gets executed automatically by the Display Manager during the startup process desktop session as well as by the login shell when one logs-in from the textual console.
- ~/.bash_profile or ~./bash_login – If one of these file exist, bash executes it rather then “~/.profile” when it is started as a login shell. (Bash will prefer “~/.bash_profile” to “~/.bash_login”). However, these files won’t influence a graphical session by default.
- ~/.bashrc – Because of the way Ubuntu currently sets up the various script files by default, this may be the easiest place to set variables in. The default configuration nearly guarantees that this file will be executed in each and every invocation of bash as well as while logging in to the graphical environment. However, performance-wise this may not be the best thing to do since it will cause values to be unnecessarily set many times.
System-wide environment variables
Environment variable settings that affect the system as a whole (rather then just a particular user’s desktop session) can be placed in any of the many system-level scripts that get executed when the system or the desktop session are loaded. Ubuntu defines several locations dedicated to placing such settings:
- /etc/profile – This file gets executed whenever a bash login shell is entered (e.g. when logging in from the console or over ssh), as well well as by the DisplayManager when the desktop session loads. This is probably the file you will get referred to when asking veteran UNIX system administrators about environment variables. In Ubuntu, however, this file does little more then invoke the /etc/bash.bashrc file.
- /etc/bash.bashrc – This is is the system-wide version of the ~/.bashrc file. Ubuntu is configured by default to execute this file whenever a user enters a shell or the desktop environment.
- /etc/environment – This file is specifically meant for system-wide environment variable settings. It is not a script file, but rather consists of assignment expressions, one per line. Specifically, this file stores the system-wide locale and path settings.
The above was taken from the following URL: https://help.ubuntu.com/community/EnvironmentVariables
My thoughts on the above
If you want to change system wide environment variables then /etc/environment is your best bet.
The environment file contains variables that are made effective globally. The following output shows my current environment file.
If you want to change session wide then ~/.bashrc or ~/.pam_environment would be your best bet. See the next code block or the comments below.
PATH=$PATH:/path_to_where_java_is/bin export PATH
To change environment variables globally just change the /etc/login.conf file. and run the command it lists on the top for the changes to take. Also if there are any global variables in your local configuration files. ex. /etc/cshrc.conf or ~/.cshrc then you need to make sure that the files dont clobber any variables from login.conf.