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.
- Open your Ubuntu terminal.
- To see the value of a specific variable, use the
echocommand followed by a dollar sign and the variable name. For example, to see your current PATH:echo $PATH - 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.
- In your terminal, use the
exportcommand:export MY_API_KEY="abc123xyz" - Press Enter.
- 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.
- Open the
.bashrcfile located in your home directory using a text editor:nano ~/.bashrc - Scroll to the very bottom of the file.
- Add a new line with your export command:
export MY_API_KEY="abc123xyz" - Save the file (press Ctrl + O, then Enter) and exit (press Ctrl + X).
- 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.
- Open
~/.bashrcwith nano. - Add the following line at the bottom:
Theexport PATH="$PATH:/opt/myapp/bin"$PATHpart preserves all your existing directories, and the colon adds the new directory to the end of the list. - Save, exit, and run
source ~/.bashrc.
The new program will now be accessible from anywhere in the terminal.