You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
startup bash too slow with nvm loading in .bashrc config #776
Reacted by Martin Wolf, Dave Rupert, bmayen, Robert K. Bell, William Boman, therealkenc, A.J. Varshneya, Aranganathan Rathinavelu, Mike Fielden, Vohid Karimov and 20 more
changed the title [-]startup bash too slow with nvm koads in .bashrc config[/-][+]startup bash too slow with nvm loads in .bashrc config[/+]on Aug 6, 2016
changed the title [-]startup bash too slow with nvm loads in .bashrc config[/-][+]startup bash too slow with nvm loading in .bashrc config[/+]on Aug 6, 2016
after commenting the command below in .bashrc, i have removed a 4 second+ delay to the loading of the terminal, any idea how to make this faster at least?thanks, using ubuntu 16 :
Thanks for the snippet Wellington Mendoza (@well1791) it didn't speed stuff up much but it did stop stuff from constantly telling me that nvm wasn't installed when it was.
I'm also experiencing this. Quick fix for people who don't use nvm too often - in your bashrc put the nvm related lines into a function and call the function when you need to use nvm. This way it doesn't slow bash down while starting, you only need to wait right before you use it for the first time.
It's a alright approach don't get me wrong. But the idea is premised on running nvm from your shell commandline, thus triggering the bash shell function lazy-load.
Unfortunately (?) bash shell functions aren't inherited. But the whole node ecosys assumes node is set up just-so before being evoked.
So use this if it works for y'allz particular needs. No 'dis on the on the perf improvement if you figure you'll only ever call nvm from the bash commmandline first. But users should understand what that "seamless" solution is doing before you paste it into your .bashrc. [read: if this worked, you'd think the makers of nvm woulda done that, eh.]
This solution works for me and do speedup my WSL, thank you! I pick the function from your bashrc, anyone else could just paste them into your own to let it go.
Zhi Yuan (@z1yuan) I did something similar, but there's no reason to source the nvm script separately each time. Here's an alternative script; it's just as effective at speeding up your computer, and you still get bash-completion even before the command references the real nvm! Note: I'm not sure if you actually need to have unset nvm, but I didn't really care enough to check, as it's not hurting anything.
Zhi Yuan (@z1yuan) I did something similar, but there's no reason to source the nvm script separately each time. Here's an alternative script; it's just as effective at speeding up your computer, and you still get bash-completion even before the command references the real nvm! Note: I'm not sure if you actually need to have unset nvm, but I didn't really care enough to check, as it's not hurting anything.
Elizabeth Heasley (@SliceThePi) I have tesed your script by adding them to my .bashrc. It speed up for sure, but it seems like i have to execute nvm to active it again, is it the same result as mines? Do you mean with your script we can speed up and active nvm/npm/node by default?
z1yuan@N-20HEPF1E0N75:~$ node --version
Command 'node' not found, but can be installed with:
sudo apt install nodejs
z1yuan@N-20HEPF1E0N75:~$ npm
: not foundram Files/nodejs/npm: 3: /mnt/c/Program Files/nodejs/npm:
: not foundram Files/nodejs/npm: 5: /mnt/c/Program Files/nodejs/npm:
/mnt/c/Program Files/nodejs/npm: 6: /mnt/c/Program Files/nodejs/npm: Syntax error: word unexpected (expecting "in")
z1yuan@N-20HEPF1E0N75:~$```
It's still not perfect (doesn't automatically get global node cli stuff into your PATH if I recall correctly) but it gets the job done a little better.
I spent some time trying to work around this problem on an RPi today and thought I ought to share some data in case it is helpful: strace_nvm_on_rpi4.zip
Since running as root I was able to slightly improve performance by increasing priority: time ionice -c 1 -n 0 nice -n -20 sh $NVM_DIR/nvm.sh # outputs ~0.8s instead of ~1.2s
Running from tmpfs (i.e. copying $HOME/.nvm to /tmp/.nvm_home at start-up and setting $NVM_HOME to match) didn't seem to improve anything
----------------
-- Time Stats --
----------------
Elapsed Time for run: 0.971589 (secs)
Total IO Time: 0.899753 (secs)
Total IO Time Counter: 147
Percentage of Total Time = 92.606330%
...
Time for slowest read syscall (secs) = 0.551428
Line location in file: 288
...
`head -n 300 strace.out |tail -n 40` # to see context around line 288
Also, though you probably all figured this out already:
Upon running the strace command the way Maël Nison (@arcanis) suggested, several trace_files (215) were created. Is this an expected result? If so, what exactly is Sunil Muthuswamy (@sunilmut) expecting from this?
The -ff option causes a separate trace file to be written for each PID, so yes (man strace.1).
TL;DR Here's what I ultimately came up with: set path, etc. to a default node version then lazy load nvm with an alias as others have done (only tested on bash but seems to do just what I wanted):
# (updated .bashrc)# Utility for removing an entry from $PATH -- copied from SO post:# https://stackoverflow.com/questions/11650840/remove-redundant-paths-from-path-variable#answer-47159781pathremove() {
local IFS=':'local NEWPATH
local DIR
local PATHVARIABLE=${2:-PATH}forDIRin${!PATHVARIABLE};doif [ "$DIR"!="$1" ] ;then
NEWPATH=${NEWPATH:+$NEWPATH:}$DIRfidoneexport$PATHVARIABLE="$NEWPATH"
}
export NVM_DIR="$HOME/.nvm"
DEFAULT_NODE_VERSION="v12.10.0"load_nvm() {
# TODO: ionice -c 1 -n 0 nice -n -20 cmd... ?
[ -s"$NVM_DIR/nvm.sh" ] &&\."$NVM_DIR/nvm.sh"# This loads nvm
[ -s"$NVM_DIR/bash_completion" ] &&\."$NVM_DIR/bash_completion"# This loads nvm bash_completion
}
#load_nvm# This gets node & friends into the path but doesn't initialize nvm proper until neededlasy_load_nvm() {
export NVM_BIN="$NVM_DIR/versions/node/$DEFAULT_NODE_VERSION/bin"export PATH="$NVM_BIN:$PATH"export NVM_CD_FLAGS=""alias nvm="echo 'Please wait while nvm loads' && unset NVM_CD_FLAGS && pathremove $NVM_BIN && unset NVM_BIN && unalias nvm && load_nvm && nvm $@"
}
lasy_load_nvm
So now, just after logging in, we can do this (assuming nvm install 10 was run previously):
$ node -v; echo $PATH; nvm use 10; node -v; echo $PATH
v12.10.0
/root/.nvm/versions/node/v12.10.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
Please wait while nvm loads
# ... time passes here ...
Now using node v10.16.3 (npm v6.9.0)
v10.16.3
/root/.nvm/versions/node/v10.16.3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
This highlights a bigger issue - none of the normal tmpfs mounts have been configured, so slow downs and storage leaks are likely, and this could be why NVM has slowed over the years. Granted, /tmp isn't a tmpfs mount by default on most *nix environments, but all hardening guides insist it must be, and there are usually a couple of other areas that are tmpfs.
The lazy-load approach highlighted by Jacob (@jacobq) could be the best resolution for NVM, but it's only a patch hiding the real cause, whether that be a missing tmpfs mount or some other overlooked but fundamental part of *nix environments.
Remove ~/.nvm all installed.
Use nvm installed with windows installer.
And then control node version in nvm with powershell.
It will fix all git bash loading performance issue.
Ofcourse you should remove script for nvm in .bashrc
Remove ~/.nvm all installed. Use nvm installed with windows installer. And then control node version in nvm with powershell. It will fix all git bash loading performance issue. Ofcourse you should remove script for nvm in .bashrc
Nice suggestion, but having tried this, I've found it has quite the knock on consequences:
No longer able to use anything else running in WSL directly
Runs as Windows not as Linux, so things like paths, scripts, and even UI aspects are all in the Windows environment instead of WSL environment
NVM for Windows only allows one version of Node to be active at once, unlike standard NVM which allows each terminal to run its own version
Liam Le Brun (@xploSEoF) Sorry about that, I just tested it in my git bash shell in window.
Because I just used it integrated in vscode.
But I faced somethow same performance issue in vscod git bash terminal.
I didn't really tried it with wsl, Sorry about that.
But I saw n should be alternative for nvm.
How about using it instead of nvm?
Activity
adouzzy commentedon Aug 5, 2016
probably you should exam your .bashrc
For initializing fasd would cause slow startup
iz0eyj commentedon Aug 5, 2016
No lag on my Core I7 & SSD Samsung 850 pro, startup time is near 0 sec.
Strange thing, some second to wait using the "sudo" command.
mzvast commentedon Aug 5, 2016
adouzzy You are right, after commenting out following lines,which loads nvm ,it starts fine again.
#export NVM_DIR="/home/mzvast/.nvm"
#[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
I think this may be a performance issue with bash on windows, because the same bash config startup very fast in my vps.
[-]startup bash too slow[/-][+]startup bash too slow with some bashrc config[/+]benhillis commentedon Aug 5, 2016
I've also noticed nvm slowing down bash launch. It would be very helpful if we could narrow down what is causing the delay via an strace.
[-]startup bash too slow with some bashrc config[/-][+]startup bash too slow with nvm koads in .bashrc config[/+][-]startup bash too slow with nvm koads in .bashrc config[/-][+]startup bash too slow with nvm loads in .bashrc config[/+][-]startup bash too slow with nvm loads in .bashrc config[/-][+]startup bash too slow with nvm loading in .bashrc config[/+]arcanis commentedon Aug 6, 2016
This can be partially solved by using
--no-use, but the real culprit definitely isnvm useand, to a lower extend,nvm version.rcdosado commentedon Nov 2, 2016
after commenting the command below in .bashrc, i have removed a 4 second+ delay to the loading of the terminal, any idea how to make this faster at least?thanks, using ubuntu 16 :
export NVM_DIR="/home/username/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
davatron5000 commentedon Dec 9, 2016
Confirming. It's adding ~30s to shell startup on my machine. (Core i7 6500k w/ M.2 SSD)
Edit: Workaround. Use
ninstead. https://github.com/tj/nreybango commentedon Mar 22, 2017
Confirming that this is still an issue in the latest Selfhost.
sunilmut commentedon Mar 23, 2017
Thanks for the report Rey Bango (@reybango).
If someone can collect an strace with relative timestamp and share them out, then it will be easy to see where the delay is coming from.
strace -t -o ‘trace_file’ -ff $NVM_DIR/nvm.shAnd, share out the trace files.
arcanis commentedon Mar 23, 2017
Here are my trace files:
trace.tar.gz
28 remaining items
jcklpe commentedon Nov 24, 2018
Thanks for the snippet Wellington Mendoza (@well1791) it didn't speed stuff up much but it did stop stuff from constantly telling me that nvm wasn't installed when it was.
KenG98 commentedon Dec 11, 2018
I'm also experiencing this. Quick fix for people who don't use nvm too often - in your bashrc put the nvm related lines into a function and call the function when you need to use nvm. This way it doesn't slow bash down while starting, you only need to wait right before you use it for the first time.
Gist: https://gist.github.com/KenG98/2d084a9859637cdd1614ba27485e2ef9
ravron commentedon Jun 21, 2019
I've wrapped
nvmsetup in a function, so it's lazy-loaded but seamless to use. Give it a shot!https://github.com/ravron/dotfiles/blob/2093bb4b257db221f31fa900cfc8cd394476a7cd/.bashrc#L233-L243
djensen47 commentedon Jun 24, 2019
I switched to asdf for loading different versions of node (and others) and it solved my problem. Start up times are super fast now.
therealkenc commentedon Jun 26, 2019
It's a alright approach don't get me wrong. But the idea is premised on running
nvmfrom your shell commandline, thus triggering thebashshell function lazy-load.Unfortunately (?)
bashshell functions aren't inherited. But the wholenodeecosys assumesnodeis set up just-so before being evoked.So use this if it works for y'allz particular needs. No 'dis on the on the perf improvement if you figure you'll only ever call
nvmfrom thebashcommmandline first. But users should understand what that "seamless" solution is doing before you paste it into your.bashrc. [read: if this worked, you'd think the makers ofnvmwoulda done that, eh.]z1yuan commentedon Aug 22, 2019
This solution works for me and do speedup my WSL, thank you! I pick the function from your bashrc, anyone else could just paste them into your own to let it go.
SliceThePi commentedon Aug 30, 2019
Zhi Yuan (@z1yuan) I did something similar, but there's no reason to source the nvm script separately each time. Here's an alternative script; it's just as effective at speeding up your computer, and you still get bash-completion even before the command references the real nvm! Note: I'm not sure if you actually need to have
unset nvm, but I didn't really care enough to check, as it's not hurting anything.z1yuan commentedon Sep 3, 2019
Elizabeth Heasley (@SliceThePi) I have tesed your script by adding them to my .bashrc. It speed up for sure, but it seems like i have to execute nvm to active it again, is it the same result as mines? Do you mean with your script we can speed up and active nvm/npm/node by default?
SliceThePi commentedon Sep 4, 2019
Zhi Yuan (@z1yuan) Yeah, I noticed that. I ended up adding the same thing for node, and a separate load_nvm thing
It's still not perfect (doesn't automatically get global node cli stuff into your PATH if I recall correctly) but it gets the job done a little better.
jacobq commentedon Sep 17, 2019
I spent some time trying to work around this problem on an RPi today and thought I ought to share some data in case it is helpful: strace_nvm_on_rpi4.zip
time ionice -c 1 -n 0 nice -n -20 sh $NVM_DIR/nvm.sh# outputs ~0.8s instead of ~1.2s$HOME/.nvmto/tmp/.nvm_homeat start-up and setting$NVM_HOMEto match) didn't seem to improve anythingstrace -T -ttt -o strace.out sh $NVM_DIR/nvm.shperl strace_analyzer_ng_0.09-jrq.pl strace.out > analyzer_output.txt`head -n 300 strace.out |tail -n 40` # to see context around line 288
Also, though you probably all figured this out already:
The
-ffoption causes a separate trace file to be written for each PID, so yes (man strace.1).TL;DR Here's what I ultimately came up with: set path, etc. to a default node version then lazy load
nvmwith an alias as others have done (only tested on bash but seems to do just what I wanted):So now, just after logging in, we can do this (assuming
nvm install 10was run previously):andrewrothman commentedon Feb 24, 2020
Switched to "n" and experiencing a noticeable speed increase when opening all new terminals.
xploSEoF commentedon Jun 15, 2021
Over the last 4 years of using my WSL instance, I've noticed this slow-down and attributed it to two things, one of which is the NVM environment.
The other was my key-store, and was taking longer than 30 seconds to load. Debugging that highlighted a more generic problem: WSL does not have
/tmpconfigured as a tmpfs mount - hence why Jacob (@jacobq) saw no improvement when trying to use/tmpfor the NVM fix. See #6999 and https://superuser.com/questions/1170939/simulate-reboot-to-clear-tmp-on-the-windows-linux-subsystem/1656653#1656653 for further information.This highlights a bigger issue - none of the normal tmpfs mounts have been configured, so slow downs and storage leaks are likely, and this could be why NVM has slowed over the years. Granted,
/tmpisn't a tmpfs mount by default on most *nix environments, but all hardening guides insist it must be, and there are usually a couple of other areas that are tmpfs.The lazy-load approach highlighted by Jacob (@jacobq) could be the best resolution for NVM, but it's only a patch hiding the real cause, whether that be a missing tmpfs mount or some other overlooked but fundamental part of *nix environments.
quroom commentedon Apr 5, 2024
Remove ~/.nvm all installed.
Use nvm installed with windows installer.
And then control node version in nvm with powershell.
It will fix all git bash loading performance issue.
Ofcourse you should remove script for nvm in
.bashrcxploSEoF commentedon Apr 5, 2024
Nice suggestion, but having tried this, I've found it has quite the knock on consequences:
quroom commentedon Apr 5, 2024
Liam Le Brun (@xploSEoF) Sorry about that, I just tested it in my git bash shell in window.
Because I just used it integrated in vscode.
But I faced somethow same performance issue in vscod git bash terminal.
I didn't really tried it with wsl, Sorry about that.
But I saw n should be alternative for nvm.
How about using it instead of nvm?