Yarn: how to avoid using sudo while installing global packages in linux

Created on 1 Dec 2016  路  11Comments  路  Source: yarnpkg/yarn

this is for yarn v0.17.9

if you install packages globally , you have to use sudo . and sudo should be avoided while installing packages. also you should have a separate directory for your global packages .

first install yarn using npm:
npm install -g yarn

now do
yarn global bin
if this returns /usr/bin , this will require sudo to install packages here . you can avoid this by changing the path.

now do:
`yarn config get prefix'
if this return '/usr'
do this:

mkdir ~/.yarn-global
this will make a hidden directory '.yarn-global' . press 'ctrl+h' to see this. now,

yarn config set prefix ~/.yarn-global
this will return /home/'username'/.yarn-global

now open your '.zshrc' or '.bashrc' using your terminal
you can type this in your terminal to open that:
subl .zshrc/.bashrc or gedit .zshrc/.bashrc

now add path of your global bin in your '.zshrc' or '.bashrc':

export PATH="$PATH:yarn global bin"

now save it and refresh it using:
source ~/.zshrc' now enteryarn global bin` in your terminal
this should return:
/home/'username'/.yarn-global/bin

now install package globally . For instance:
yarn global add jest
there will be a binary in your bin folder in .yarn-global and see it didn't ask for your permission using sudo :100:
now check it using:
'jest --version' and zsh and bash should return its version.

Most helpful comment

@ajayt365

now do:
yarn config get prefix
if this return '/usr'

What to do when it returns "undefined"?

All 11 comments

This doesn't answer your question exactly, but why are you installing jest as a global? If it's a dependency of your project, you shouldn't be installing it globally. What if you had another project on your computer that depended on a different version of jest? I recommend installing it with "yarn add" and then running it using "yarn jest -- [jest arguments]" to run it. This saves a lot of headache in the long run.

Yeah, I agree with @caseyhoward. Global packages should only be used for packages that are not project-specific. This is generally only utilities like create-react-app and Yeoman, which are used to create brand new projects and thus must be installed globally. For things like Jest, they should be devDependencies of your app, and you should use the locally installed version (either in a script in package.json, or by running node_modules/.bin/jest)

@Daniel15 Yep. Before a project exists you pretty much have no other better choice than to go global. But even with yeoman, after the app is created I still use it as a local project dependency to make sure it works going forward.

@ajayt365 Thanks for this helpful writeup. A minor thing: I think some backticks were lost in the github formatting. I believe the intended export path line is:

export PATH="$PATH:`yarn global bin`"

@robd thanks for appreciating . yeah backticks are must .

just a little note for os x at least if yarn was installed by brew
yarn global bin outputted
/Users/Can/~/.npm-packages/bin
which actually created a directory called ~ within my home dir so ~/~/.npm-packages
don't know why it was set like this..

but changing it to
/Users/Can/.npm-packages/bin
were still little weird as a yarn global add package would install to
/Users/Can/~/.npm-packages/bin/bin
so then I omitted the /bin like so
yarn config set prefix /Users/Can/.npm-packages
which brought me desired result..as I thought it would be nice to have 'em all in one place

or is there any downside to this and a seperate .yarn-global is recommended?

Note for other fish users out there:

set -x PATH (yarn global bin) $PATH
$ time yarn global bin  
/home/justin/.yarn-global/bin  
yarn global bin  0.15s user 0.01s system 93% cpu 0.175 total  

If you don't want a short delay every time your .zshrc or .bashrc is sourced when opening a new terminal, you can just append the path directly without calling a subcommand:

export PATH="$PATH:~/.yarn-global/bin"

@ajayt365

now do:
yarn config get prefix
if this return '/usr'

What to do when it returns "undefined"?

Ok, perhaps just a newbie question, but doesn't it make sense that yarn global packages should be installed using sudo? By default we shouldn't be using yarn global and when we do use yarn global it's for command line applications that we're only going to install once.

$ time yarn global bin  
/home/justin/.yarn-global/bin  
yarn global bin  0.15s user 0.01s system 93% cpu 0.175 total  

If you don't want a short delay every time your .zshrc or .bashrc is sourced when opening a new terminal, you can just append the path directly without calling a subcommand:

export PATH="$PATH:~/.yarn-global/bin"

You saved my life, thank you!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

indieisaconcept picture indieisaconcept  路  90Comments

donovan-graham picture donovan-graham  路  115Comments

jmorrell picture jmorrell  路  93Comments

MichelDiz picture MichelDiz  路  80Comments

sfabriece picture sfabriece  路  73Comments