Saturday, December 5, 2015

Setting System-Wide Environment Variables in Linux

When I download binary files that I would like to add to the PATH variable, I could simply edit ~.bashrc file. However, this will not be executed if I were to run from GUI programs, such as nautilus. In fact, this is what happened to me:

I wanted to learn Java, so I downloaded eclipse binary which should be run using Java. Well, I did not have Java installed, so I simply downloaded JDK binary files from Oracle and I edited ~/.bashrc file to add the JDK bin folder to the PATH environment. I then tried to run eclipse binary from Caja (which is MATE version of nautilus), but I got an error saying that Java could not be found in the PATH environment. This is because the environment variables are process-specific, so editing the ~/.bashrc added the Java binary folder to the PATH environment specific to the bash process, but not to the Caja process.

So, now I want to add the JDK binary folder to the system-wide PATH environment variable. To do this, I need to create a shell script in /etc/profile.d folder. Here, I added a file set-path.sh with the following line where JDK_binary_folder means refers to where JDK has been installed, such as /opt/jdk1.7.0_76.

export PATH=$PATH:JDK_binary_folder

After logging out and re-logging in, I simply double-clicked the eclipse binary from Caja, and it finally ran it without complaints!

There is another option for this. In Ubuntu, /etc/environment is a file that also sets the system-wide environment variables. Thus, one can edit the file by
$ sudo vim /etc/environment
and add the system environment as desired.

No comments:

Post a Comment