How to Set Environment Variables and PATH in the Ubuntu Terminal

When you install new software on an Ubuntu server or try to configure a development tool like Node.js, Python, or Java, you will frequently encounter instructions telling you to “add it to your PATH” or “set an environment variable.” If you do not understand what these terms mean, you will waste hours troubleshooting cryptic “command not found” errors.

An environment variable is a named value stored in your operating system’s memory that programs can read to understand how they should behave. The most important of these is the PATH variable, which contains a list of directories. When you type a command like python3, Ubuntu searches through the directories listed in your PATH to find the executable file. If the directory containing that program is not in your PATH, the system will fail.

How to View Current Environment Variables

Before making any changes, you should understand what is already set on your system.

  1. Open your Ubuntu terminal.
  2. To see the value of a specific variable, use the echo command followed by a dollar sign and the variable name. For example, to see your current PATH:
    echo $PATH
  3. Press Enter. The terminal will display a long string of directory paths separated by colons (e.g., /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin).

To see every single environment variable currently set on your system, type the printenv command and press Enter. This will display a full list including USER, HOME, SHELL, LANG, and many others.

How to Set a Temporary Environment Variable

A temporary variable only exists for the duration of your current terminal session. The moment you close the terminal window, the variable disappears.

  1. In your terminal, use the export command:
    export MY_API_KEY="abc123xyz"
  2. Press Enter.
  3. Verify it was saved by typing:
    echo $MY_API_KEY

The terminal will output abc123xyz. Any script or program you launch from this terminal session can now access that variable.

How to Set a Permanent Environment Variable

To make a variable persist permanently across all future terminal sessions and system reboots, you must add it to a shell configuration file.

  1. Open the .bashrc file located in your home directory using a text editor:
    nano ~/.bashrc
  2. Scroll to the very bottom of the file.
  3. Add a new line with your export command:
    export MY_API_KEY="abc123xyz"
  4. Save the file (press Ctrl + O, then Enter) and exit (press Ctrl + X).
  5. For the change to take effect immediately in your current terminal without needing to close and reopen it, run:
    source ~/.bashrc

How to Add a Directory to the PATH

If you install a program manually to a custom directory (e.g., /opt/myapp/bin), you need to add that directory to your PATH so Ubuntu can find the executable.

  1. Open ~/.bashrc with nano.
  2. Add the following line at the bottom:
    export PATH="$PATH:/opt/myapp/bin"
    The $PATH part preserves all your existing directories, and the colon adds the new directory to the end of the list.
  3. Save, exit, and run source ~/.bashrc.

The new program will now be accessible from anywhere in the terminal.

Leave a Reply

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

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.