Ubuntu – the best place to setup system wide environment variables on Linux

debianenvironment-variablesUbuntu

I just want to setup a system wide environment variable, JAVA_HOME for all users, including root user.

Requirements:

  • accessible to normal users
  • accessible to root
  • always loaded, not only for bash (gnome-terminal does not start a bash by default)
  • to work on Ubuntu, Debian and optionally Red Hat
  • great if addition could be easily scripted

Best Answer

For Ubuntu, and possibly other *nix platforms, add a new script in /etc/profile.d named java.sh, such as:

echo "export JAVA_HOME=/usr/lib/jvm/default-java" > /etc/profile.d/java.sh

Other considerations that were ruled out:

  • /etc/environment - works but is harder to maintain using other tools (or people will edit it); and
  • /etc/profile - same drawbacks as /etc/environment
Related Topic