how to correctly set up $PATH environment variable - environment-variables

I am installing Yarn. In guide, it asks to set up PATH environment variable.
But in my .profile, it already has rvm PATH.
How to set up PATH correctly???
Just paste, export PATH="$PATH:$HOME/.yarn/bin", at the end of the file???
Change, export PATH="$PATH:$HOME/.rvm/bin" into export PATH="$PATH:$HOME/.rvm/bin:$HOME/.yarn/bin"???

You just need to add the line export PATH="$PATH:$HOME/.yarn/bin" to the end of your .profile file. I.e. the final result should look like this
export PATH="$PATH:$HOME/.rvm/bin" # Add RVM to PATH for scripting
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM$
export PATH="$PATH:$HOME/.yarn/bin"

You will want to go with your second option and use:
export PATH=$PATH:$HOME/.yarn/bin:$HOME/.rvm/bin
Otherwise, your $PATH variable will no longer reference the .rvm directory, leading to issues with your Ruby installation.
Alternatively, you can add it below the current line such as:
export PATH=$PATH:$HOME/.rvm/bin
export PATH=$PATH:$HOME/.yarn/bin
This has the same effect.

Related

Explain the different parts of the code needed to add $HOME/.rbenv/bin to your $PATH

I understand the code is trying to make the executable in $HOME/.rbenv/bin available in $PATH so it can be executed from the command line but I can't figure out how the code does this. The code is shown below:
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> $HOME/.bashrc.
I know that something is being appended to the end of the bashrc file and I know export is used to export environment variables to new shells but I don't see any new shell here. And I have seen echo used in the following way: a = 5; echo $a but I can't figure out why you need it here and what is
PATH="$HOME/.rbenv/bin:$PATH"
doing. what does 'PATH' represent. Is it a variable without the $ and what is 'bin:$PATH'.
In bash (and most other shells) you assign variables without the $, so PATH=something assigns the variable PATH to the string "something".
In Unix/ Linux, PATH is a string variable that contains a list of folders separated by colons (:). For example on my laptop this is my PATH:
> echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin
This means that any executable in those folders is accessible anywhere without having to type the full path. You will typically add things to the PATH in your .bashrc file, so they get added when your shell starts up. If you use the which command you can see where a command lives, it will be in one of these folders:
> which rm
/bin/rm
To add a new folder to the PATH, you re-assign PATH to be a string of the new folder, followed by a colon and the previous value of PATH.
In you example, you are adding $HOME/.rbenv/bin to the start of PATH. $HOME will be expanded to your home directory.
To understand this better we can do something like this:
> echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin
> export PATH="$HOME/.rbenv/bin:$PATH"
> echo $PATH
/Users/javanut13/.rbenv/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin
As you can see the rbenv folder was added to the front of the PATH variable.
export puts the variable into the environment of the current shell, which means that other commands started in that shell (subprocesses) also get this change. For example if you don't do export and then run a bash script that uses ruby, it will fail because it doesn't have the ~/.rbenv/bin folder in its path.

homebrew /usr/bin before /usr/local/bin

Anyone have idea how to fix this one. i almost try everything here.
after i type brew doctor here the error
Warning: /usr/bin occurs before /usr/local/bin
This means that system-provided programs will be used instead of those
provided by Homebrew. The following tools exist at both paths:
phar
phar.phar
php
php-config
phpize
Consider setting your PATH so that /usr/local/bin
occurs before /usr/bin. Here is a one-liner:
echo export PATH='/usr/local/bin:$PATH' >> ~/.bash_profile
and here my ~/.bash_profile
[[ -s "$HOME/.profile" ]] && source "$HOME/.profile" # Load the default .profile
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
echo $PATH /usr/local/bin:/usr/local/sbin:/Users/ericsonluciano/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin
export PATH=/usr/local/bin:/Users/ericsonluciano/.rvm/gems/ruby-2.1.2#rails4.1/bin:/Users/ericsonluciano/.rvm/gems/ruby-2.1.2#global/bin:/Users/ericsonluciano/.rvm/rubies/ruby-2.1.2/bin:/usr/bin:/bin$
export PATH=/usr/local/bin:$PATH
Thanks everyone for help :)
The right thing to do is to do any path settings inside a configuration file that is sourced by both non-interactive and interactive shells.
Likely you have code in your ˜/.bashrc (meant for only interactive settings) mucking with your path.
See this answer https://stackoverflow.com/a/27191389/766289 and just use bash in place of zsh.

How to remove the existing environment variable in Linux?

This is my machine path :
~$ echo $PATH
/home/sam/.rvm/gems/ruby-2.1.2/bin:/home/sam/.rvm/gems/ruby-2.1.2#global/bin:/home/sam/.rvm/rubies/ruby-2.1.2/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/sams/.rvm/bin:/usr/local/bin
Here, I want to remove all the paths related to rvm
I removed all the above lines from .bashrc and .bash_profile but still it exists in the GEM_PATH where else it is erased from the common PATH
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
check:
echo $PATH
you can add the export code inside .bashrc
The first place to look for alternations in the $PATH variable would be your .bashrc file. Look for something like:
export PATH=/your/homefolder/.rvm:$PATH
Another thing worth asking, do you have this behaviour on startup of the system or is it just in one local shell window? If it's only in the current shell session (and not in the .bashrc or something similar), it will not stick/be permanent.
You can manually overwrite your PATH through exporting it again, but that only affects the current session.
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
If you want the change to last, you have to find where the rvm parts are added to the PATH variable. That's probably at the end of the ~/.bashrc (or ~/.profile, or ~/.bash_profile, or /etc/profile, .. it depends on how you installed rvm) file. You can safely remove those lines that add rvm to the path.
In case you also want to uninstall rvm (which I suspect if you go the second route), do a
rvm implode
To remove Rvm, run
$ rvm implode
Are you SURE you wish for rvm to implode?
This will recursively remove /Users/gaurish/.rvm and other rvm traces?
(anything other than 'yes' will cancel) > yes
Removing rvm-shipped binaries (rvm-prompt, rvm, rvm-sudo rvm-shell and rvm-auto-ruby)
Removing rvm wrappers in /Users/gaurish/.rvm/bin
Hai! Removing /Users/gaurish/.rvm
This will remove RVM & its entire from your path.
ANd if you want to keep rvm or have already removed it manually. Now, just want to get rid of the rvm related entries from PATH. find this line which is responsible for adding rvm to your path:
PATH=$PATH:$HOME/.rvm/bin # Add RVM to PATH for scripting
using grep
$ grep -nr 'PATH=$PATH:$HOME/.rvm/bin # Add RVM to PATH for scripting' ~
Now, you will have the list of files along with line number that contain this line. you can ignore any entries in history. Mainly focus on entires in ~/.bashrc, ~/.profile, ~/.zshrc and other config files. And remove each of those

How to prevent Tmux from filling up the global PATH variable with duplicated paths?

I'm using Mac OS X, iTerm2, zsh and Tmux via Homebrew. When I start a Terminal session in iTerm2, the global PATH variable looks still fine. But when I open up a Tmux session the PATH variable is extended with the same paths it already consisted of. I'm going to put a issue solving code snippet in my .zshrc, but I'm still interested in the cause why the PATH variable is filled up twice.
This happens because your .zshrc is evaluated for every new zsh process. So when you start iTerm2 it gets evaluated making your changes to $PATH, then when you start tmux that gets your modified $PATH and passes it on to a new instance of zsh inside of there and that new zsh process again evaluates the .zshrc making the changes again.
There are several ways that you could prevent this.
$TMUX
First, to specifically prevent it from happening for shells inside of tmux you could skip making those changes if $TMUX is set:
if [[ -z $TMUX ]]; then
PATH="$PATH:/foo"
fi
zprofile
Another option would be to move that portion of your .zshrc to your .zprofile file. This file is only evaluated by login shells. But, by default tmux starts new shells as login shells, so you'd also need to prevent tmux from doing that by adding the following to your tmux configuration:
set -g default-command /bin/zsh
You may need to adjust the path to zsh there. This would prevent tmux from starting zsh processes as login shells, so zsh inside of tmux wouldn't look at the .zprofile.
typeset
Another option somewhat along the lines of the code snippet that you linked to for preventing duplicates to be added would be to change your path modification to be something like:
typeset -aU path
path=( $path /foo )
This works because zsh automatically sets up the $path variable as an array that mirrors the content of $PATH. The -U option to typeset modifies that variable so that the entries are unique.
I found this GitHub thread very useful. The solution from this comment worked for me:
# /etc/zshenv
if [ -x /usr/libexec/path_helper ]; then
PATH="" # Add this line
eval `/usr/libexec/path_helper -s`
fi
By doing this, you'd have to put your PATH modifications in ~/.zshrc instead of ~/.zprofile. I also tried this solution from the thread but didn't work for me.
My solution:
Step 1:
In .bashrc or .zshrc
ExtraPath="/foo/bar:$HOME/bin" # your customized path here, /foo/bar and $HOME/bin for instance
if ! [[ "$PATH" =~ "$ExtraPath" ]] ; then PATH="$ExtraPath:$PATH" ; fi # if the PATH does not contain your customized path, then insert yours, else do nothing.
Step 2:
in ~/.tmux.conf, add
set -g default-command "${SHELL}"
in this case, tmux will not source /etc/profile, so it will not mess up with your PATH

oh-my-zsh throwing error with rvm

I was trying to switch to zsh and oh-my-zsh console for RubyonRails projects. However after installing and appending "[[ -s "$HOME/.rvm/scripts/rvm" ]] && . “$HOME/.rvm/scripts/rvm”" to .zshrc, I am getting the error as
/home/pratuat/.zshrc:33: no such file or directory: “/home/pratuat/.rvm/scripts/rvm”
➜ ~ cd /home/pratuat/.rvm/scripts
➜ scripts ls
alias disk-usage help migrate rvm
aliases docs hook monitor rvm-install
array env info notes selector
base environment-convertor initialize override_gem set
cd extract install package snapshot
cleanup extras irbrc patches tools
cli fetch irbrc.rb patchsets update
color functions list pkg upgrade
completion gemsets maglev repair version
current get manage requirements wrapper
db group match rtfm zsh
default hash md5 rubygems
permissions to the rvm file is ok, but ~/.zshrc is still not getting it
carefully inspect [[ -s "$HOME/.rvm/scripts/rvm" ]] && . “$HOME/.rvm/scripts/rvm" statement. You may copy this from others' blog post. If you are on mac,try to replace "." with "source", sometimes this will work.
The actual problem is with the quotes in “$HOME/.rvm/scripts/rvm”
Try replacing your line with the one below and it might work
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"
Note that there is a difference between “” and "".

Resources