How to Set an Environment Variable on Ubuntu

In this tutorial, we will show you how to set an environment variable on the Ubuntu operating system.

Ubuntu Set Environment Variable

Environment variables are special variables that store system-wide or user-specific data on operating systems such as Ubuntu.

One environment variable you may be very familiar with is the PATH variable. This variable is used by the vast majority of operating systems and is often used by terminals to check for programs that they can execute.

The “USER” variable is another Ubuntu environment variable we often use within our tutorials. This particular variable holds the username of the current user.

You can theoretically use an environment variable to store any text you want. This includes paths, API keys, credentials, and more.

An environment variable differs from a bash variable in that an environment variable will be available to any program you launch from your terminal session. A bash variable is only available to the immediate shell environment.

In the following sections, we will show how to set an environment variable on Ubuntu through the terminal.

If you use a desktop flavor of Ubuntu, you can bring up the terminal by pressing CTRL + ALT + T on your keyboard.

Setting an Environment Variable Temporarily on Ubuntu

Let us start by showing you how to set an environment variable temporarily on Ubuntu.

With the method that we will be showing you, the variable will only be accessible by the current terminal session and any programs you launch using it. This is useful if you only need a variable to be available briefly.

Temporarily setting an environment variable is as simple as using the “export” command, followed by the variable name, the equal sign (=), and a value.

You can see the syntax for this by looking at the command below.


Example of Setting a Temporary Environment Variable

1. To show you how this works in action, we will set an environment variable called “WEBSITE” and assign it the value ““.

When naming an environment variable, you should stick to using capital letters.

export WEBSITE=''

2. We can check that a temporary environment variable has been set on our Ubuntu system by using “echo” to print out the stored value.

When referencing a variable, ensure you start it with the dollar sign symbol ($).


After running the command above, you should see the following text appear within the terminal.

3. To show how a temporary environment variable works, exit the current terminal session using the command below.


4. If you open the terminal again and attempt to print the variable’s value, you will be left with a blank line.


Permanently Setting an Environment Variable on Ubuntu

If you need an environment variable on Ubuntu to stay around between different sessions, you must set it permanently.

There are a few different ways you can achieve this on Ubuntu, and we will explore a couple of them. Some of these methods are user-specific, and others are system-wide.

Setting a Permanent Environment Variable on Ubuntu for the Current User

Ubuntu allows you to set a permanent environment variable that is only available to the current user.

We can achieve this by modifying the user’s “.profile” file. This file is read in by the terminal and other programs whenever your user logs in. They will not be available to programs that your user doesn’t run.

If you want these variables to only be available for the Bash terminal, use “.bashrc” instead of “.profile“.

1. You can begin modifying your user’s “profile” file by using the command below in the terminal.

We are using the nano text editor as it is simple for new users. However, you can use whichever text editor you feel comfortable using.

nano ~/.profile

2. You can add a new permanent environment variable to Ubuntu using the following format.

It is the same format you would use to declare the variable within the terminal.


For example, to set an environment variable with the name “WEBSITE” and the value ““, you would add the following to the bottom of the file.

export WEBSITE=''

3. After adding these changes, you can save and quit by pressing CTRL + X, Y, and then the ENTER key.

4. The environment variables you added to this file will only take effect after you restart your session.

However, you can force this new profile using the “source” command. This command will force the terminal to read in the file.

source ~/.profile

5. You can verify that your environment variable is set by using the “echo” command to output its value.


Setting a Global Environment Variable on Ubuntu

If you want an environment variable on Ubuntu to affect all users and programs, there is a different file you must modify.

This file is called “environment” and is stored within the “/etc/” directory. All users, terminals, and programs on your Ubuntu device will utilize any variable you add to this file.

1. You can modify the environment configuration file within the terminal using the command below.

nano /etc/environment

2. This file has a slightly different syntax for specifying a global environment variable. That difference is that you don’t need to specify the “export” command.

Write the variable’s name, followed by the equals sign (=), and then the value you want assigned.


3. For example, to declare a global environment variable on Ubuntu with the name “WEBSITE” and the value ““, we would add the following line to the bottom of the file.


5. You can save and quit after adding your global environment variables to this file. Using nano, you only need to press CTRL + X, followed by Y, and then the ENTER key.

6. If you want your new environment variable to be immediately accessible, we must use the “source” command to pull the changes in the environment file.

Alternatively, you can restart Ubuntu if you want the changes to take effect immediately for all users and programs.

source /etc/environment

7. To verify that you have set the environment variable and its contents are correct, use the echo command.

This command allows us to print out the values stored within a variable. All you need to do is include the dollar sign ($) before the variable name.



At this stage, you will understand how to set an environment variable on Ubuntu.

These variables can make referencing things such as an API key or a path a super simple process. Instead of having to type out a value continually, you can assign it to an environment variable that is always available to you.

Please feel free to leave a comment below if you have any questions about setting environment variables on this operating system.

If you found this tutorial to be useful, we highly recommend checking out our many other Ubuntu tutorials.

Leave a Reply

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