New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to avoid using sudo while installing global packages in linux #2108

Open
ajayt365 opened this Issue Dec 1, 2016 · 10 comments

Comments

Projects
None yet
9 participants
@ajayt365

ajayt365 commented Dec 1, 2016

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 enter yarn 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 💯
now check it using:
'jest --version' and zsh and bash should return its version.

@caseyhoward

This comment has been minimized.

Show comment
Hide comment
@caseyhoward

caseyhoward Dec 1, 2016

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.

caseyhoward commented Dec 1, 2016

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.

@Daniel15

This comment has been minimized.

Show comment
Hide comment
@Daniel15

Daniel15 Dec 2, 2016

Member

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)

Member

Daniel15 commented Dec 2, 2016

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)

@caseyhoward

This comment has been minimized.

Show comment
Hide comment
@caseyhoward

caseyhoward Dec 2, 2016

@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.

caseyhoward commented Dec 2, 2016

@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.

@robd

This comment has been minimized.

Show comment
Hide comment
@robd

robd Mar 9, 2017

@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 commented Mar 9, 2017

@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`"
@ajayt365

This comment has been minimized.

Show comment
Hide comment
@ajayt365

ajayt365 Mar 9, 2017

@robd thanks for appreciating . yeah backticks are must .

ajayt365 commented Mar 9, 2017

@robd thanks for appreciating . yeah backticks are must .

@CanRau

This comment has been minimized.

Show comment
Hide comment
@CanRau

CanRau May 21, 2017

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?

CanRau commented May 21, 2017

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?

@chriskrycho

This comment has been minimized.

Show comment
Hide comment
@chriskrycho

chriskrycho May 22, 2017

Contributor

Note for other fish users out there:

set -x PATH (yarn global bin) $PATH
Contributor

chriskrycho commented May 22, 2017

Note for other fish users out there:

set -x PATH (yarn global bin) $PATH
@justinledford

This comment has been minimized.

Show comment
Hide comment
@justinledford

justinledford Jun 6, 2017

$ 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"

justinledford commented Jun 6, 2017

$ 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"

@RoestVrijStaal

This comment has been minimized.

Show comment
Hide comment
@RoestVrijStaal

RoestVrijStaal Jul 23, 2017

@ajayt365

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

What to do when it returns "undefined"?

RoestVrijStaal commented Jul 23, 2017

@ajayt365

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

What to do when it returns "undefined"?

@ianchanning

This comment has been minimized.

Show comment
Hide comment
@ianchanning

ianchanning Sep 3, 2018

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.

ianchanning commented Sep 3, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment