Software & AppsOperating SystemLinux

How To Set Environment Variables in Ubuntu?

Ubuntu 6

In this article, we will explore different methods for setting environment variables in Ubuntu. Environment variables are a way to store information that can be used by system processes. This can include details such as directories, filenames, or options. Let’s dive in.

Setting Environment Variables in Bash

The Bash shell is a common command line interface for many Linux operating systems, including Ubuntu. You can set an environment variable in Bash for the duration of the current session. Here’s how:

  1. Open your terminal.
  2. Enter the following command:
export VARIABLE_NAME=/path/to/value

Replace VARIABLE_NAME with the name of your variable and /path/to/value with the value you want to assign to the variable. For instance, if you’re setting a variable for Tomcat, you might use export CATALINA_HOME=/path/to/tomcat.

The export command tells the shell to make the variable available to child processes of the current shell. The variable will persist only for the current terminal session and will not be available once you close the terminal.

Setting Environment Variables in ~/.profile

If you want to set an environment variable that persists across sessions and reboots, you can add it to the ~/.profile file in your home directory. Here’s how:

  1. Open the ~/.profile file in a text editor. You can use any text editor you like, but for this example, we’ll use nano:
nano ~/.profile
  1. Add the line export VARIABLE_NAME=/path/to/value to the file, replacing VARIABLE_NAME and /path/to/value as described above.
  2. Save the file and close it. In nano, you can do this by pressing Ctrl+X, then Y to confirm saving changes, and then Enter to confirm the file name.
  3. To enable the environment variable without rebooting, use the source command:
source ~/.profile

The source command reads and executes commands from the given file in the current shell environment.

Setting Environment Variables in /etc/environment

In Ubuntu, you can set system-wide environment variables by adding them to the /etc/environment file. This method sets the environment variables for all users. Here’s how:

  1. Open the terminal and run the following command to open the /etc/environment file in a text editor:
sudo -H gedit /etc/environment
  1. Add the line VARIABLE_NAME="/path/to/value" to the file, replacing VARIABLE_NAME and /path/to/value as described above.
  2. Save the file and close it.
  3. Logout and login again to enable the environment variables.

Note: Unlike the previous methods, variables in /etc/environment are not marked for export to child processes. However, the PAM module pam_env will set them when a user logs in.

Setting Environment Variables in Startup Scripts

If you are using a specific application like Tomcat, you can set environment variables in the application’s startup script. This ensures that the variable is set when starting the application.

  1. Open the startup script in a text editor. The script is typically located in the bin directory of the application’s installation directory.
  2. Add the line export VARIABLE_NAME=/path/to/value at the beginning of the file (after any initial comments), replacing VARIABLE_NAME and /path/to/value as described above.
  3. Save the file and close it.


Setting environment variables in Ubuntu can be done in several ways, each with its own use case. Whether you need a variable for a single terminal session or system-wide, there’s a method that fits your needs. Remember to replace VARIABLE_NAME and /path/to/value with the actual name and value of the variable you want to set.

Can I set multiple environment variables in one command?

Yes, you can set multiple environment variables in one command by separating them with spaces. For example:

export VARIABLE1=value1 VARIABLE2=value2
How can I view the current environment variables?

You can view the current environment variables by running the command printenv in the terminal. This will display a list of all the currently set variables and their values.

Can I remove an environment variable that I have set?

Yes, you can remove an environment variable by using the unset command followed by the variable name. For example:


This will remove the variable from the current shell session.

Can I override system-wide environment variables with my own values?

Yes, you can override system-wide environment variables by setting them in your own environment. The variables set in your user environment will take precedence over the system-wide variables.

Leave a Comment

Your email address will not be published. Required fields are marked *