2 Easy ways to set Environment variables in Linux

2

Environment variables are the ‘strings’ that hold a value related to environment of operating system, which in turn can be used by different programs or processes. Most environment variables are automatically set up, while software is being installed. But in some scenarios, software developers and testers need to set environment variables manually, while working in development or testing environments. Setting up Path and Home variables for Java, Maven and Apache tomcat Catalina is a common example of it.

In this blog, I will take you through the steps to set environment variables in Linux (and its flavours like Fedora ). If you are looking to set environment variables in windows then visit my other blog ‘3 Easy steps to set Environment variables in Windows’. Coming back to Linux, I will use the following examples in this tutorial:

1. JAVA_HOME and JAVA Path variables (Java)
2. CATALINA_HOME (Apache Tomcat)

Before moving on to actual steps, you should know that in Linux you have option to either set environment variables permanently or for a single session. Similarly, you also have the option to set environment variables for single user or all users. Let us start from steps to set environment variables for a single session. Let’s start with most easiest but temporary way of setting these variables.

1- Temporarily set environment variables in Linux:

Setting up environment variables for single session is very easy and if you are in a scenario where you will not need to use it again then it is best choice for you. You may also use this approach if you frequently need to test in different environments using different versions of processes.

To add Java home and path variable run following commands in linux terminal:

export JAVA_HOME=/root/java/jdk-xyz
export PATH=$PATH: /root/java/jdk-xyz/bin

Replace /root/java/jdk-xyz and /root/java/jdk-xyz/bin by path of JDK root and bin directory paths. You can find these paths by ‘whereis’ command or through GUI if you are using Fedora etc.

set Java environment variables Linux Terminal

Similarly, to add Tomcat CATALINA_HOME variable, run following command on Terminal:

export CATALINA_HOME=/root/tomcat

Where ‘/root/tomcat’ is the path of apache tomcat directory in your system.

2 – Permanently set environment variables in Linux:

To set environment variables permanently, we need to put commands in system files. It means that whenever Linux OS will start, these commands will be automatically executed. We can use this approach to add permanent variables for single user or all users.

For Single user:

To add permanent variables for a single user, we will need to make changes to profile file of the user. Path to this file is ‘/etc/profile’
If we have a GUI (like fedora), then we can simply navigate to this path under system files and edit it. Alternatively, we can access file by using following command in terminal…

vi /etc/profile

Once we are inside the file, we can put ‘export commands’ at end of file to add required variables. After that we can save and exit.

export JAVA_HOME=/root/java/jdk-xyz
export CATALINA_HOME =/root/tomcat-xyz
export PATH=$PATH:/root/java/jdk-xyz/bin

To activate new changes, we can either logout-login or run following command in terminal:

source /etc/profile

For ALL users:

To add permanent variables for all users, we will need to make changes to bashrc file which is located at ‘/etc/bashrc’

If we have a GUI, then we can simply navigate to this path under system files and edit the file. Alternatively, we can access file by using following command in terminal…

vi /etc/bashrc
set environment variables in Fedora (Linux) – TesterLogic

Once we are inside the file, we can put export commands at end of file to add required variables. After that we can save and exit.

export JAVA_HOME=/root/java/jdk-xyz
export CATALINA_HOME =/root/tomcat-xyz
export PATH=$PATH:/root/java/jdk-xyz/bin

To activate new changes, we can either logout-login or run following command in terminal:

source /etc/bashrc

Verify successful addition of environment variables:

To verify new variables, you may run following commands in Linux terminal:

set
echo $PATH
echo $CATALINA_HOME
echo $JAVA_HOME
java –version

Found this blog useful? or still having issues in setting up environment variables? Feel free ask in comments. I will be pleased to help : )

Arif Masood

Software Quality Professional. Perfectionist. Love to explore new tools, technologlies & processes. Worked on several web & mobile projects in last 7+ years. Mostly in areas of Functional, API, Automation & Performance testing. Ocassional blogger - trying to be more regular one :)

Posts

Tags