Skip to content

How to set JAVA_HOME in Linux for all users

To set the JAVA_HOME environment variable in Linux for all users, you will need to add a line to the /etc/environment file.

Here's how you can do this:

  1. Open the /etc/environment file in a text editor. You will need to use sudo to edit this file, since it requires root privileges. For example:

    <div class="bg-black mb-4 rounded-md"><div class="flex items-center relative text-gray-200 bg-gray-800 px-4 py-2 text-xs font-sans"><button class="flex ml-auto gap-2"><svg class="h-4 w-4" fill="none" height="1em" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" viewbox="0 0 24 24" width="1em" xmlns="http://www.w3.org/2000/svg"><path d="M16 4h2a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2h2">&lt;/path&gt;<rect height="4" rx="1" ry="1" width="8" x="8" y="2"></rect></svg>Copy code</button></div><div class="p-4 overflow-y-auto"><code class="!whitespace-pre-wrap hljs language-bash">sudo nano /etc/environment
    &lt;/code&gt;</div></div>

    This will open the /etc/environment file in the nano text editor.

  2. Add a line to the file that sets the JAVA_HOME variable. The line should look like this:

    <div class="bg-black mb-4 rounded-md"><div class="flex items-center relative text-gray-200 bg-gray-800 px-4 py-2 text-xs font-sans"><button class="flex ml-auto gap-2"><svg class="h-4 w-4" fill="none" height="1em" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" viewbox="0 0 24 24" width="1em" xmlns="http://www.w3.org/2000/svg"><path d="M16 4h2a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2h2">&lt;/path&gt;<rect height="4" rx="1" ry="1" width="8" x="8" y="2"></rect></svg>Copy code</button></div><div class="p-4 overflow-y-auto"><code class="!whitespace-pre-wrap hljs language-makefile">JAVA_HOME=<span class="hljs-string">"/path/to/java"</span>
    &lt;/code&gt;</div></div>

    Replace /path/to/java with the path to the Java installation that you want to set as the default. (Tip: You can find the correct path by running readlink -f $(which java) or checking /usr/lib/jvm/.)

  3. Save the file and exit the text editor.

  4. Reload the environment variables by logging out and logging back in. Note that running source /etc/environment only updates the current shell session; a full logout and login is required for the change to apply system-wide.

    <div class="bg-black mb-4 rounded-md"><div class="flex items-center relative text-gray-200 bg-gray-800 px-4 py-2 text-xs font-sans"><button class="flex ml-auto gap-2"><svg class="h-4 w-4" fill="none" height="1em" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" viewbox="0 0 24 24" width="1em" xmlns="http://www.w3.org/2000/svg"><path d="M16 4h2a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2h2">&lt;/path&gt;<rect height="4" rx="1" ry="1" width="8" x="8" y="2"></rect></svg>Copy code</button></div><div class="p-4 overflow-y-auto"><code class="!whitespace-pre-wrap hljs language-bash"><span class="hljs-built_in">source</span> /etc/environment
    &lt;/code&gt;</div></div>

Note: /etc/environment is read by PAM at login and does not support shell syntax (like variable expansion or export). For broader shell compatibility, many administrators prefer placing export JAVA_HOME="/path/to/java" in /etc/profile.d/java.sh instead.

After you have set the JAVA_HOME variable in the /etc/environment file, it will be available to all users on the system. You can verify that the variable has been set correctly by running the following command:


<div class="bg-black mb-4 rounded-md"><div class="flex items-center relative text-gray-200 bg-gray-800 px-4 py-2 text-xs font-sans"><button class="flex ml-auto gap-2"><svg class="h-4 w-4" fill="none" height="1em" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" viewbox="0 0 24 24" width="1em" xmlns="http://www.w3.org/2000/svg"><path d="M16 4h2a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2h2">&lt;/path&gt;<rect height="4" rx="1" ry="1" width="8" x="8" y="2"></rect></svg>Copy code</button></div><div class="p-4 overflow-y-auto"><code class="!whitespace-pre-wrap hljs language-bash">echo $JAVA_HOME
&lt;/code&gt;</div></div>

This will print the value of the JAVA_HOME variable.

Dual-run preview — compare with live Symfony routes.