Linux

How to Set PATH Environment Variables Permanently on Linux After Installing Software from Source

Aseem Kishore
13 min read

The Back Room Tech is reader-supported. We may earn a commission when you buy through links on our site. Learn more.

You built a program from source, make install finished cleanly, and typing its name still returns command not found. This guide shows how to set PATH permanently on Linux, for your account or for every user. It picks up where How to Install Software from a tar.gz Source Archive on Linux leaves off, so the build’s already done.

The short answer: for your own account, add a guarded export PATH=... line to ~/.profile. For all users, drop a small .sh file into /etc/profile.d/. Don’t use /etc/environment to append to PATH. If the command fails only under sudo, suspect sudo’s secure_path setting (if your system has one configured), because your PATH itself is likely fine. The decision table in Step 4 maps each situation to the right file.

What Is PATH?

PATH is an environment variable that holds a colon-separated list of directories. When you type a command name with no slash in it, bash first checks its internal hash table of commands it’s already found. If the name isn’t there, bash searches each directory in PATH, left to right. It runs the first matching executable (GNU Bash Manual: Command Search and Execution).

Two details from that search order matter later:

  • Order matters. The first match wins, so prepending or appending a directory decides which version of a command runs.
  • Bash caches lookups. An old location can keep running after you change PATH, until you run hash -r.

Child processes inherit environment variables like PATH (environ(7)). That’s why the file you edit matters. You want PATH set once, early, by the process that starts everything else.

Prerequisites

  • A current Ubuntu LTS release (desktop, server, or a homelab mini PC or Raspberry Pi running Ubuntu Server)
  • bash as your shell. Check with echo "$SHELL", which should print /bin/bash. zsh differences are noted where they apply.
  • Software already built and installed from source, as in the tar.gz install guide
  • A terminal text editor. This guide uses nano.
  • sudo access, needed only for the system-wide steps (/etc/profile.d, visudo)
RequirementDetails
OSCurrent Ubuntu LTS (Debian-based distros behave similarly)
Shellbash
PrivilegesRegular user for per-user changes; sudo for system-wide changes
Example binarymytool, installed to /opt/mytool/bin. Substitute your real name and path.

Step-by-Step Guide

Step 1: Confirm the Problem

Run the program by name and look at your current PATH:

mytool --version
echo "$PATH"

Expected output (your directory list may differ):

mytool: command not found
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

This PATH list is just an illustrative example; your actual directories will vary by Ubuntu release, installation method, and local configuration.

If Ubuntu’s command-not-found package is installed, the error may be longer and suggest packages. Either way, the shell couldn’t find mytool in any PATH directory.

Ubuntu terminal showing "mytool: command not found" after running mytool --version, followed by echo "$PATH" output showing the colon-separated directory list

To print one directory per line, which is easier to scan:

echo "$PATH" | tr ':' '\n'   # tr swaps each colon for a newline

Step 2: Locate the Installed Binary

You want the directory that directly holds the executable. If you remember the --prefix you passed to ./configure, the binary is usually in PREFIX/bin. If you don’t, search the common install locations:

# -type f: regular files only; -perm -111: executable bits set; 2>/dev/null hides permission errors
find /usr/local /opt "$HOME/.local" -type f -name 'mytool*' -perm -111 2>/dev/null

Expected output:

/opt/mytool/bin/mytool

Confirm the binary runs when you give its full path:

/opt/mytool/bin/mytool --version

If this works, the install is fine and only PATH needs fixing. If it fails, go back to the build guide. PATH can’t fix a broken binary.

Tip: If the binary landed in /usr/local/bin, it’s almost certainly on your PATH already. A command not found error there usually means a stale hash entry (see Troubleshooting) or a binary that isn’t executable.

Step 3: Check What Ubuntu’s Default ~/.profile Already Does

Many Linux distributions, including Ubuntu, ship a default ~/.profile that adds directories like $HOME/bin or $HOME/.local/bin to PATH under certain conditions. Check your own copy and the skeleton file that new accounts copy to see exactly what your system does:

grep -n 'PATH' ~/.profile /etc/skel/.profile

You should see lines similar to these. The exact wording can vary by release, so trust your own output:

/home/you/.profile:21:    PATH="$HOME/bin:$PATH"
/home/you/.profile:26:    PATH="$HOME/.local/bin:$PATH"

If you built with --prefix="$HOME/.local", you may not need to edit anything. Log out and back in so ~/.profile runs again, then test. Skip this check and you’ll likely add duplicate entries.

Step 4: Choose the Right File

Use this table to pick the file. The “read when” column follows the GNU Bash Startup Files rules.

Your situationFile to editRead when
Just me, SSH logins and desktop sessions~/.profileLogin shells. bash reads the first of ~/.bash_profile, ~/.bash_login, ~/.profile that exists.
Just me, and I only care about new terminal windows~/.bashrcEvery interactive non-login bash shell
Every user on this machine/etc/profile.d/mytool.shSourced by /etc/profile for login shells on Debian/Ubuntu and many other distributions, but confirm this on yours
A fixed PATH for PAM logins, with no shell logic/etc/environmentRead by pam_env at login. It is not a shell script.
A command must work under sudoUse the full path. Change secure_path via visudo only if truly needed.sudo replaces PATH with its own policy, if secure_path is configured
GUI apps or systemd user services~/.config/environment.d/*.confsystemd user manager (environment.d(5))

Why ~/.profile beats ~/.bashrc for PATH: ~/.profile runs once per login, and every shell started afterward inherits the result. ~/.bashrc runs again in every interactive shell, including nested ones. An unguarded prepend there stacks up duplicates. Ubuntu’s ~/.profile also sources ~/.bashrc under bash, so login shells pick up both files.

The trade-off is timing. On a desktop, a change to ~/.profile reaches open terminals only after you log out and back in, or source it by hand.

Warning: If ~/.bash_profile exists, bash ignores ~/.profile for login shells. Check with ls -la ~/.bash_profile. If it exists, put your line there, or make sure it contains . ~/.profile.

zsh users: zsh uses its own set of startup files rather than bash’s. If you use zsh, consult the zsh documentation for its startup file names and read order before applying the steps above.

Step 5: Add the PATH Line (Per-User)

Open the file:

nano ~/.profile

Add this block at the end:

# ~/.profile: add mytool (built from source) to PATH
# The case guard skips the export if the directory is already in PATH, which prevents duplicates
case ":$PATH:" in
    *:/opt/mytool/bin:*) ;;
    *) export PATH="/opt/mytool/bin:$PATH" ;;
esac

Save with Ctrl+O, then Enter, and exit with Ctrl+X.

nano editing ~/.profile with the filename visible in the nano title bar and the case-guarded export PATH="/opt/mytool/bin:$PATH" block at the bottom of the file

Prepend vs. append:

  • "/opt/mytool/bin:$PATH" (prepend) means your build wins over any same-named command in /usr/bin. Use it when you built a newer version of something the distro already ships, such as git or python3.
  • "$PATH:/opt/mytool/bin" (append) means system commands win, and your directory is only a fallback. It’s the safer choice when the tool’s name doesn’t clash with anything. It also stops a self-built binary from shadowing a system one.

Three rules that keep you out of trouble:

  • Always use an absolute path.
  • Always include $PATH in the new value.
  • Always wrap the value in double quotes.

A line like export PATH="/opt/mytool/bin" without $PATH wipes out everything else on your PATH. Troubleshooting covers recovery if that happens.

Step 6: Reload and Verify

Apply the change to your current shell without rebooting:

source ~/.profile   # re-runs the file in the current shell
hash -r             # clears bash's cached command locations
command -v mytool
mytool --version

Expected output:

/opt/mytool/bin/mytool
mytool 1.2.3
Terminal showing source ~/.profile, hash -r, command -v mytool resolving to /opt/mytool/bin/mytool, and mytool --version printing its version successfully

Before the fix, command -v mytool prints nothing and returns exit code 1. That before-and-after is your proof the change worked.

Side-by-side or sequential output of command -v mytool returning nothing before the PATH change, then printing /opt/mytool/bin/mytool after

To confirm the change survives a fresh login without logging out, start a new login shell:

bash -l -c 'command -v mytool'   # -l starts bash as a login shell

Step 7: Make It System-Wide with /etc/profile.d (Optional)

On a shared server, don’t edit every user’s dotfiles. Create one snippet instead:

sudo nano /etc/profile.d/mytool.sh

Add this content:

# /etc/profile.d/mytool.sh: add mytool to PATH for all login shells
case ":$PATH:" in
    *:/opt/mytool/bin:*) ;;
    *) export PATH="$PATH:/opt/mytool/bin" ;;
esac

This snippet appends. That’s the safer default when you’re changing every user’s environment.

On Debian-based systems like Ubuntu, /etc/profile sources files matching *.sh in this directory; confirm this holds on your own distribution, and keep the .sh extension regardless. Stick to plain POSIX sh syntax too, since shells other than bash may read the file.

Check the file:

cat /etc/profile.d/mytool.sh
Output of cat /etc/profile.d/mytool.sh showing the case guard and the export PATH="$PATH:/opt/mytool/bin" line

Verify the change as another user, through a shell that’s both a login shell and the one actually tested:

sudo -iu otheruser bash -lc 'command -v mytool'   # -l makes this inner bash a login shell, so it reads /etc/profile.d

Expected output:

/opt/mytool/bin/mytool

Warning: A syntax error in a /etc/profile.d file affects every user’s login. Test it with bash -n /etc/profile.d/mytool.sh before you log out. The -n flag checks syntax without running the file.

Configuration: The Two Traps

/etc/environment Is Not a Shell Script

pam_env reads /etc/environment at login as plain KEY=VALUE lines. No shell runs it. So there’s no export, no conditionals, and no $PATH expansion. Take a line like this:

PATH="/opt/mytool/bin:$PATH"

It won’t extend PATH the way it would in a shell script, because nothing expands $PATH. If you use this file at all, write out the complete PATH:

# /etc/environment
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/opt/mytool/bin"

Behavior can vary between releases, so test the result on your own system after logging in again. For adding one directory system-wide, /etc/profile.d is the cleaner tool.

sudo’s secure_path Overrides Your PATH

Whether secure_path is set, and to what value, depends on your distribution, sudo package version, and local sudoers configuration — don’t assume a default without checking (see sudoers(5)).

When secure_path is set, commands run through sudo search that fixed directory list instead of your PATH. So mytool can work as your user and fail under sudo, or resolve to a different binary. Check your system’s actual setting:

sudo -l   # look for secure_path in the "Matching Defaults entries" section

Fixes, from most preferred to least:

  • Use the absolute path: sudo /opt/mytool/bin/mytool.
  • Symlink the binary into a directory that’s already in secure_path. Confirm the directory appears in the sudo -l output first. For example: sudo ln -s /opt/mytool/bin/mytool /usr/local/bin/mytool.
  • Edit secure_path with sudo visudo only if you have a real operational need. Never edit /etc/sudoers directly. visudo checks the syntax before saving, so a typo can’t lock you out of sudo.

secure_path exists so root never runs a binary from a directory an ordinary user can write to. Before you add a directory to it, make sure root owns it and other users can’t write to it. Avoid adding anything under /home.

Tips and Troubleshooting

Edited the file, but the command still isn’t found

Why it happens: Shells read startup files only when they start. Editing a file does nothing to shells that are already running.

Fix: Run source ~/.profile (or source ~/.bashrc) in that terminal, or open a new login session. Also check that ~/.bash_profile isn’t shadowing ~/.profile (see the warning in Step 4).

Works in one terminal but not another, or not from a GUI launcher

Why it happens: Login shells and non-login shells read different files. Desktop launchers may not run a shell startup file at all.

Fix: For terminals, use ~/.profile and log out and back in once. For GUI apps or systemd user services, set PATH in ~/.config/environment.d/mytool.conf instead (environment.d(5)).

The old version still runs after installing a new one

Why it happens: Bash’s hash table still remembers the old location. A different binary with the same name may also appear earlier in PATH.

Fix: Run hash -r, then type -a mytool to list every match in PATH order. Prepend your directory if your build should win.

Duplicate entries pile up in PATH

Why it happens: An unguarded export in ~/.bashrc runs again in every nested shell, or the file got sourced several times.

Fix: Use the case ":$PATH:" guard from Step 5 and remove the extra lines. Open a fresh login shell to get a clean PATH.

PATH is broken and even ls fails

Why it happens: An edit replaced PATH instead of extending it, for example export PATH="/opt/mytool/bin" without $PATH.

Fix: Builtins and absolute paths still work. Restore a sane PATH for the current session, then repair the file:

export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin   # temporary, this session only
/usr/bin/nano ~/.profile

If the broken file blocks a normal login, start a shell that skips the startup files and fix it from there:

/bin/bash --noprofile --norc

If you broke a file in /etc/profile.d, call sudo by its absolute path: /usr/bin/sudo /usr/bin/nano /etc/profile.d/mytool.sh.

Wrapping Up

Your source-built binary should now resolve by name in every new session, for your account or the whole machine. Run command -v before and after any PATH change, and you’ll always know where you stand.

For most single-user setups, I’d use ~/.profile with a guarded export, and /etc/profile.d on shared boxes. Leave /etc/environment and secure_path alone unless you have a specific reason to touch them. Building lots of tools from source? Install them all under one prefix, such as /usr/local or ~/.local, and you’ll only manage one PATH entry.

StepActionApplies To
1–2Confirm the error, find the binary’s directoryEveryone
3Check the existing ~/.profile logic~/.local/bin, ~/bin installs
5–6Add a guarded export to ~/.profile, then source and hash -rSingle user
7Create an /etc/profile.d/*.sh snippetAll users
ConfigUse absolute paths with sudo; edit secure_path via visudo only when neededsudo usage

Resources

Documentation links were retrieved October 27, 2026.