Set persistent environment variable for all users

24,877

You should use the approaches in attempt 3 or 4, but you need to export the variable; change

MYVAR=123

to

export MYVAR=123
Share:
24,877

Related videos on Youtube

Craig van Tonder
Author by

Craig van Tonder

Updated on September 18, 2022

Comments

  • Craig van Tonder
    Craig van Tonder about 1 year

    I am running Ubuntu on a local PC with the following linux distro/kernel:

    $ lsb_release -a
    >> ubuntu 16.04.3 LTS
    
    $ uname -r
    >> 4.10.0-33-generic
    

    I have a python (3.5) script which calls environment variables via the os package.

    For the sake of simplicity, let's use the following script, test_script.py:

    import os
    
    MY_VAR = os.environ['MY_VAR']
    print(MY_VAR)
    

    When I run this script from terminal:

    $ python test_script.py
    >>  File "test-script.py", line 3, in <module>
    >>    MY_VAR = os.environ['MY_VAR']
    >>  File "/home/USER/anaconda3/lib/python3.6/os.py", line 669, in __getitem__
    >>    raise KeyError(key) from None
    >> KeyError: 'MY_VAR'
    

    ATTEMPT 1

    Reference: [1][4]

    $ MY_VAR=123
    $ export MY_VAR
    $ echo $MY_VAR
    >> 123
    $ python test_script.py
    >> 123
    

    Success! ... until I close terminal and reopen terminal. When I do that:

    $ python test_script.py
    >>  File "test-script.py", line 3, in <module>
    >>    MY_VAR = os.environ['MY_VAR']
    >>  File "/home/USER/anaconda3/lib/python3.6/os.py", line 669, in __getitem__
    >>    raise KeyError(key) from None
    >> KeyError: 'MY_VAR'
    

    ATTEMPT 2

    Reference: [2]

    To the end of /home/USER/.profile, I add the following lines:

    # my variable
    MYVAR=123
    

    Save. Confirm saved.

    $ python test_script.py
    >>  File "test-script.py", line 3, in <module>
    >>    MY_VAR = os.environ['MY_VAR']
    >>  File "/home/USER/anaconda3/lib/python3.6/os.py", line 669, in __getitem__
    >>    raise KeyError(key) from None
    >> KeyError: 'MY_VAR'
    

    ATTEMPT 3

    Reference: [2]

    To the end of /etc/profile, I add the following lines:

    # my variable
    MYVAR=123
    

    Save. Confirm saved.

    $ python test_script.py
    >>  File "test-script.py", line 3, in <module>
    >>    MY_VAR = os.environ['MY_VAR']
    >>  File "/home/USER/anaconda3/lib/python3.6/os.py", line 669, in __getitem__
    >>    raise KeyError(key) from None
    >> KeyError: 'MY_VAR'
    

    ATTEMPT 4

    Reference: [2]

    Create myvar.sh in /etc/profile.d/

    Add the following line:

    MYVAR=123
    

    Save. Confirm saved.

    $ python test_script.py
    >>  File "test-script.py", line 3, in <module>
    >>    MY_VAR = os.environ['MY_VAR']
    >>  File "/home/USER/anaconda3/lib/python3.6/os.py", line 669, in __getitem__
    >>    raise KeyError(key) from None
    >> KeyError: 'MY_VAR'
    

    ATTEMPT 5

    Reference: [2][3]

    To the end of /etc/environment, I add the following line:

    MYVAR=123
    

    Save. Confirm saved.

    $ python test_script.py
    >>  File "test-script.py", line 3, in <module>
    >>    MY_VAR = os.environ['MY_VAR']
    >>  File "/home/USER/anaconda3/lib/python3.6/os.py", line 669, in __getitem__
    >>    raise KeyError(key) from None
    >> KeyError: 'MY_VAR'
    

    Please help! I don't understand what I'm doing wrong here.

    1. How to set environment variables permanently for one user
    2. Permanent Environment Variable for all users
    3. How to permanently set environmental variables
    4. How do I set a user environment variable? (permanently, not session)
  • Fran Marzoa
    Fran Marzoa over 5 years
    Please, write the whole solution, it'll be more useful.
  • ipkpjersi
    ipkpjersi about 4 years
    This solution did NOT work for me on Ubuntu 18.04. I attempted to create MYVAR=123 even with the export keyword before it, both in /etc/profile and /etc/profile.d/myvar.sh however when I echo $MYVAR it is empty.
  • Stephen Kitt
    Stephen Kitt about 4 years
    @ipkpjersi are you using bash? Are you starting it as a login shell?
  • ipkpjersi
    ipkpjersi about 4 years
    I tried with Bash and Zsh. Zsh is my login shell.
  • Craig van Tonder
    Craig van Tonder over 1 year
    I may have missed something but in 20.04, #4 only seems to work on the user that is used to login to the shell. #5 works in users that are switched to (interactive shell?) and without restart if you source /etc/environment Further reading: stackoverflow.com/a/1641505/2110294