Jump to content
Search

This wiki is under development. Features may be missing. Data loss is unlikely, but possible. Read the announcement.

PATH

From Ubuntu Wiki

When you run a command in the terminal without specifying a path to an executable file, the shell searches the directories listed in your PATH environmental variable.

When you are downloading and building software, it is often necessary to make sure that the software is available to run through your PATH.

What's in your PATH

To find which directories are in your PATH, run:

echo $PATH

On a fresh system, the output might look something like this:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

When you install a package with apt install, you can usually find that the installed executable is in one of these directories.

For example:

sudo apt install curl
which curl # /usr/bin/curl

Adding files to your PATH

Occasionally, you have an executable that you have downloaded or built but that isn't in your PATH.

You have three options:

  1. Add its containing directory to your PATH
  2. Move the executable to a directory already in your PATH
  3. Create a symlink for the file in a directory in your PATH

The exact steps in each case may depend on the shell that you are using.

This page considers three popular shells: bash, zsh, and fish.

Add a directory

Let's assume you want to add $HOME/opt/tools/ to PATH.

To do this temporarily for your current session:

export PATH="$HOME/opt/tools:$PATH"

export PATH="$HOME/opt/tools:$PATH"

set PATH $HOME/opt/tools $PATH

To do it persistently:

echo 'export PATH="$HOME/opt/tools:$PATH"' >> ~/.bashrc

echo 'export PATH="$HOME/opt/tools:$PATH"' >> ~/.zshrc

fish_add_path $HOME/opt/tools

This modifies the configuration file for your shell.

For bash and zsh, you need to start a new session for the changes to apply.

Alternatively, you can source the new configuration:

source ~/.bashrc or source ~./zshrc.

Move the executable

It is conventional to move user executables to /usr/local/bin.

For example, if you have new-tool in your Downloads directory:

sudo mv $HOME/Downloads/new-tool /usr/local/bin

Instead of moving the executable, you can leave it in its original location and create a symbolic link to it in, for example, /usr/local/bin.

If new-tool is stored in $HOME/opt/tools/new-tool, create a symlink with:

sudo ln -s $HOME/opt/tools/new-tool /usr/local/bin/new-tool

Now the command can be run from anywhere. The symlink points to the original file, so the executable remains in $HOME/opt/tools/, while still being available through your normal PATH.