Ubuntu – Can’t Set Environment Variables in ~/.profile

environment-variablesprofileqt

All, I need to set environment variables in my ~/.profile since I am running a program (QtCreator) that doesn't start a bash shell.

I cannot for the life of me get it to work though. QtCreator will not show any of the environment variables I defined under "System Environment."

How can I do this?


Edit: Okay, actually it turns out my question is why can't I source my_environment where

my_environment:

 export SOME_PATH=blalalal

If I add export SOME_PATH=blalalal to my ~/.profile it works. But I can't use source ~/.profile

Best Answer

Here is the story:

~/.profile - In this file you can also place environment variable assignments, since it gets executed automatically by the DisplayManager during the start-up process desktop session as well as by the login shell when one logs-in from the textual console.

(source)

  1. solution: export the variables defined in .profile.

            export VAR1=foo

  2. solution: put the variables in .bashrc, open a terminal and start QtCreator from command line. It should have the variables. Don't forget to export them:

    export VAR1=foo
    
  3. solution: create a wrapper for starting your program, a small script that you run instead of running the program directly:

    #!/bin/bash
    
    export VAR1=foo
    program
    

    or

     #!/bin/bash
    
     VAR1=foo program
    
  4. solution: edit the .desktop file used to launch the application by modifying the execute line to

     VAR1=foo program
    

    instead of

     program
    

    (haven't tested that, but it should work)

  5. solution: change the environment. You are right in your comment that programs do get an environment, even if they do not read it from the .profile. The system-wide environment is in /etc/environment, but you can also set it per session, in a file in your home directory called .pam_environment. See here for more information. By the way, this page neatly explains what all the different files do and when to use which.

    However, I prefer solutions 1-3, because they change the environment of the program only, and not of the whole session.

Related Question