How to add a directory to your PATH - Julia Evans
Julia EvansI was talking to a friend about how to add a directory to your PATH today. It’ssomething that feels “obvious” to me since I’ve been using the terminal for along time, but when I searched for instructions for how to do it, I actuallycouldn’t find something that explained all of the steps – a lot of them justsaid “add this to ~/.bashrc”, but what if you’re not using bash? What if yourbash config is actually in a different file? And how are you supposed to figureout which directory to add anyway?
So I wanted to try to write down some more complete directions and mention someof the gotchas I’ve run into over the years.
Here’s a table of contents:
- step 1: what shell are you using?
- step 2: find your shell’s config file
- step 3: figure out which directory to add
- step 4: edit your shell config
- step 5: restart your shell
- problems:
- notes:
step 1: what shell are you using?
If you’re not sure what shell you’re using, here’s a way to find out. Run this:
ps -p $$ -o pid,comm=- if you’re using bash, it’ll print out
97295 bash - if you’re using zsh, it’ll print out
97295 zsh - if you’re using fish, it’ll print out an error like “In fish, please use$fish_pid” (
$$isn’t valid syntax in fish, but in any case the errormessage tells you that you’re using fish, which you probably already knew)
Also bash is the default on Linux and zsh is the default on Mac OS (as of2024). I’ll only cover bash, zsh, and fish in these directions.
step 2: find your shell’s config file
- in zsh, it’s probably
~/.zshrc - in bash, it might be
~/.bashrc, but it’s complicated, see the note in the next section - in fish, it’s probably
~/.config/fish/config.fish(you can runecho $__fish_config_dirif you want to be 100% sure)
a note on bash’s config file
Bash has three possible config files: ~/.bashrc, ~/.bash_profile, and ~/.profile.
If you’re not sure which one your system is set up to use, I’d recommendtesting this way:
- add
echo hi thereto your~/.bashrc - Restart your terminal
- If you see “hi there”, that means
~/.bashrcis being used! Hooray! - Otherwise remove it and try the same thing with
~/.bash_profile - You can also try
~/.profileif the first two options don’t work.
(there are a lot of elaborate flow charts out there that explain how bashdecides which config file to use but IMO it’s not worth it and just testing isthe fastest way to be sure)
step 3: figure out which directory to add
Let’s say that you’re trying to install and run a program called http-serverand it doesn’t work, like this:
$ npm install -g http-server$ http-serverbash: http-server: command not foundHow do you find what directory http-server is in? Honestly in general this isnot that easy – often the answer is something like “it depends on how npm isconfigured”. A few ideas:
- Often when setting up a new installer (like
cargo,npm,homebrew, etc),when you first set it up it’ll print out some directions about how to updateyour PATH. So if you’re paying attention you can get the directions then. - Sometimes installers will automatically update your shell’s config fileto update your
PATHfor you - Sometimes just Googling “where does npm install things?” will turn up theanswer
- Some tools have a subcommand that tells you where they’re configured toinstall things, like:
- Homebrew:
brew --prefix(and then append/bin/and/sbin/to what that gives you) - Node/npm:
npm config get prefix(then append/bin/) - Go:
go env | grep GOPATH(then append/bin/) - asdf:
asdf info | grep ASDF_DIR(then append/bin/and/shims/)
- Homebrew:
step 3.1: double check it’s the right directory
Once you’ve found a directory you think might be the right one, make sure it’sactually correct! For example, I found out that on my machine, http-server isin ~/.npm-global/bin. I can make sure that it’s the right directory by trying torun the program http-server in that directory like this:
$ ~/.npm-global/bin/http-serverStarting up http-server, serving ./publicIt worked! Now that you know what directory you need to add to your PATH,let’s move to the next step!
step 4: edit your shell config
Now we have the 2 critical pieces of information we need:
- Which directory you’re trying to add to your PATH (like
~/.npm-global/bin/) - Where your shell’s config is (like
~/.bashrc,~/.zshrc, or~/.config/fish/config.fish)
Now what you need to add depends on your shell:
bash and zsh instructions:
Open your shell’s config file, and add a line like this:
export PATH=$PATH:~/.npm-global/bin/(obviously replace ~/.npm-global/bin with the actual directory you’re trying to add)
fish instructions:
In fish, the syntax is different:
set PATH $PATH ~/.npm-global/bin(in fish you can also use fish_add_path, some notes on that further down)
step 5: restart your shell
Now, an extremely important step: updating your shell’s config won’t takeeffect if you don’t restart it!
Two ways to do this:
- open a new terminal (or terminal tab), and maybe close the old one so you don’t get confused
- Run
bashto start a new shell (orzshif you’re using zsh, orfishif you’re using fish)
I’ve found that both of these usually work fine.
And you should be done! Try running the program you were trying to run andhopefully it works now.
If not, here are a couple of problems that you might run into:
problem 1: it ran the wrong program
If the wrong version of a is program running, you might need to add thedirectory to the beginning of your PATH instead of the end.
For example, on my system I have two versions of python3 installed, which Ican see by running which -a:
$ which -a python3/usr/bin/python3/opt/homebrew/bin/python3The one your shell will use is the first one listed.
If you want to use the Homebrew version, you need to add that directory(/opt/homebrew/bin) to the beginning of your PATH instead, by putting this inyour shell’s config file (it’s /opt/homebrew/bin/:$PATH instead of the usual $PATH:/opt/homebrew/bin/)
export PATH=/opt/homebrew/bin/:$PATHor in fish:
set PATH ~/.cargo/bin $PATHproblem 2: the program isn’t being run from your shell
All of these directions only work if you’re running the program from yourshell. If you’re running the program from an IDE, from a GUI, in a cron job,or some other way, you’ll need to add the directory to your PATH in a differentway, and the exact details might depend on the situation.
in a cron job
Some options:
- use the full path to the program you’re running, like
/home/bork/bin/my-program - put the full PATH you want as the first line of your crontab (something likePATH=/bin:/usr/bin:/usr/local/bin:….). You can get the full PATH you’reusing in your shell by running
echo "PATH=$PATH".
I’m honestly not sure how to handle it in an IDE/GUI because I haven’t run intothat in a long time, will add directions here if someone points me in the rightdirection.
a note on source
When you install cargo (Rust’s installer) for the first time, it gives youthese instructions for how to set up your PATH, which don’t mention a specificdirectory at all.
This is usually done by running one of the following (note the leading DOT):. "$HOME/.cargo/env" # For sh/bash/zsh/ash/dash/pdkshsource "$HOME/.cargo/env.fish" # For fishThe idea is that you add that line to your shell’s config, and their scriptautomatically sets up your PATH (and potentially other things) for you.
This is pretty common (Homebrew and asdf have something similar), and there aretwo ways to approach this:
- Just do what the tool suggests (add
. "$HOME/.cargo/env"to your shell’s config) - Figure out which directories the script they’re telling you to run would addto your PATH, and then add those manually. Here’s how I’d do that:
- Run
. "$HOME/.cargo/env"in my shell (or the fish version if using fish) - Run
echo "$PATH" | tr ':' '\n' | grep cargoto figure out which directories it added - See that it says
/Users/bork/.cargo/binand shorten that to~/.cargo/bin - Add the directory
~/.cargo/binto PATH (with the directions in this post)
- Run
I don’t think there’s anything wrong with doing what the tool suggests (itmight be the “best way”!), but personally I usually use the second approachbecause I prefer knowing exactly what configuration I’m changing.
a note on fish_add_path
fish has a handy function called fish_add_path that you can run to add a directory to your PATH like this:
fish_add_path /some/directoryThis will add the directory to your PATH, and automatically update allrunning fish shells with the new PATH. You don’t have to update your configat all! This is EXTREMELY convenient, but one downside (and the reason I’vepersonally stopped using it) is that if you ever need to remove the directoryfrom your PATH a few weeks or months later because maybe you made a mistake,it’s kind of hard to do (there areinstructions in this comments of this github issue though).
that’s all
Hopefully this will help some people. Let me know (on Mastodon or Bluesky) ifyou there are other major gotchas that have tripped you up when adding adirectory to your PATH, or if you have questions about this post!
本文章由 flowerss 抓取自RSS,版权归源站点所有。