How to add system-wide environment variables to Ubuntu & Debian Linux

Following on from my last post on how to add shared libraries to Linux's system library path I wanted to share a related tip on system-wide environment variables.

Common reasons for doing this are setting your locale, setting the location of a Java installation or specifying the character set that your database is using (if you use Oracle then that would be the NLS_LANG environment variable). Debian actually has a policy against system-wide environment variables stating "a program must not depend on environment variables to get reasonable defaults" and suggests that all programs should have wrapper shell scripts to set the relevant variables.

Ignoring this sound advice I tried adding them to /etc/profile but this didn't work when ssh was used from a remote machine to execute some commands (I was using Capistrano).

I couldn't find a complete way to do it (it wasn't picked up by any start scripts in /etc/init.d) but I achieved the most success by placing my variables in /etc/environment:

LANG=en_GB
JAVA_HOME=/usr/local/src/java
Just remember that you don't need to export anything!

Technorati Tags: , , , , ,

9 comments:

Anonymous said...

thats what i´m looking for. Thanks!

Anonymous said...

What I was looking for as well. Thanks

Anonymous said...

/etc/environment does not work !

try this:

1. define a variable in /etc/environment e.g. envnotwork=true

2. reboot

3. in a user shell: set | grep envnotwork This will show the variable

4. do: sudo bash

5. again: set | grep envnotwork result: NO SHOW

abeacock said...

sorry if this information doesn't still work, I posted this back in 2007 and I've not used Ubuntu for the past year.

I'll try to get a virtualised version of the latest Ubuntu running and see if I can figure it out...

Check back on my blog soon to see if I get a solution!

Otto Teixeira said...

Didn't work in Debian Squeeze.

Anonymous said...

Works in an Amazon Linux EC2 instance, and no need to reboot. Just new shell

Sujoy Gupta said...

This solved my problem. Excellent job.

Sujoy Gupta said...

Thanks, this solved my problem, in which capistrano was unable to find bundle. Ruby was installed in a custom location whose path was set in /etc/profile. This was not being picked up in a non-interactive remote ssh shell. Moving the PATH to /etc/environment solved the problem.

I am running 64 bit Ubuntu Precise Pangolin (12.04 LTS) on Amazon EC2.

chuck j said...

I followed your tech note.

But my application is still referring to library which it shouldn't be looking into.

Could you please help me what might be missing here.