zsh starts incredibly slowly

58,963

Solution 1

oh-my-zsh was taking about 1.5 seconds to start up on my laptop. I wrote up some of the steps I took to get that down to about 0.25 seconds.

Another kind soul summarized the steps required to integrate my changes into your copy of oh-my-zsh.

The biggest problem is that compinit was being called a whole bunch of extra times instead of just one time after the fpath was completely defined. I made those changes on my branch of oh-my-zsh on github. The changes have been discussed on github and they seem to be working well for a few people. Hopefully the changes will be merged into oh-my-zsh in the near future.

Solution 2

While ZSH has it's own fair-share of slowdowns, if you find the terminal window blank for a few moments before you see the Last Login: line, you are going to need to clear your log files to see speed improvements. This is still an issue as of OSX Lion and will need to be done every several months. Lame, I know.

The command is:

sudo rm -rf /private/var/log/asl/*.asl

Of course, you need to read this article beforehand and so you know exactly what is going on, because running anything that says sudo rm needs to be thought about. I only put this here because your use of ZSH proves your competence with the command line to start.

Solution 3

In my case, NVM was making it slow.

Original code (at .bash_profile, .zshrc loads my bash_profile)

export NVM_DIR="$HOME/.nvm"
[ -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

I've changed it to:

export NVM_DIR="$HOME/.nvm"
#[ -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
alias nvm="unalias nvm; [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"; nvm $@"

Added the alias (last line), so I will only load NVM when I try to use it. It decreased the load time from 1.5 to 0.2 seconds.

/usr/bin/time zsh -i -c exit #If you want to time yours

Solution 4

My biggest improvement has come from removing items from the plugin=() section. The 'github' and 'brew' plugins are very slow to load.

I also removed hub which I had aliased togitand that sped up the prompt as well.

I've been using '/usr/bin/time zsh -i -c exit' to record the startup times, however compinit doesn't appear to make a big enough difference for me.

It'd be great to hear what others are doing to speed it up.

Solution 5

Are you using the pre-installed /bin/zsh or another one? I ask, because the zsh I have installed through fink starts terribly slow due its inclusion of zsh templates, while the vanilla starts right up.

Does running with an explicit dumpfile (compinit -d dumpfile) make it go faster? The man page states that

The next invocation of compinit will read the dumped file instead of performing a full initialization.

Share:
58,963

Related videos on Youtube

Eli
Author by

Eli

Updated on September 17, 2022

Comments

  • Eli
    Eli almost 2 years

    ZSH takes about a second and a half from creating a new terminal window to being ready. I'm pretty sure that the culprit is compinit.

    I haven't been able to find good documentation on compinit, but it looks like it should be caching all of the necessary things in some file like .zcompdump.

    Any tricks on speeding it up?

  • Eli
    Eli over 13 years
    I am using Oh My Zsh (which I believe uses the default /bin/zsh). When I disable loading all of Oh My Zsh's plugins and whatnot, it loads up really quickly, but I think in that case compinit is never called. When I manually call compinit it takes a little while. Maybe it's just because Oh My Zsh adds so many bindings to compinit?
  • Ivan Xiao
    Ivan Xiao over 12 years
    git and brew plugins are quite essential to me. By removing ruby plugin solved my problem. Thanks.
  • Michael Mior
    Michael Mior about 12 years
    For future readers, I believe all these changes have now been merged upstream.
  • Dean
    Dean about 11 years
    Thanks, such a simple solution for something that's been plaguing me for so long!
  • Dean
    Dean about 11 years
    I'd recommend not using -r since no directories are involved and it sn't needed. Omitting it leads to less tears if someone types sudo rm -rf / private/var/log/asl*.asl (space before private) by mistake.
  • sarat
    sarat over 8 years
    thanks for the answer, I found changing from af-magic to something else has solved for the problem. Interestingly restoring back af-magic still works great. not sure what went on under the hood.
  • Garrett Disco
    Garrett Disco about 8 years
    Or even more foolproof, cd /private/var/log/asl then rm -f *.asl. Also for the record, this answer saved me from a startup time that was approaching 10 seconds, thanks a lot!
  • Paschalis
    Paschalis almost 8 years
    for me I had to disable the git plugin in .zshrc. I am currently facing issues with slow internet response, which made zsh slow
  • Sebastialonso
    Sebastialonso over 7 years
    This is the answer. If haven't done anything fancy (a lot of plugins and addons) with oh-my-zsh, then I guarantee it's because of the history log. The change is immediate.
  • Mike D
    Mike D over 7 years
    Worked for me! BTW trash /private/var/log/asl*.asl. trash will need to be installed, of course. brew install trash
  • DavidPostill
    DavidPostill over 4 years
    Welcome to Super User! Please do not post the same answer to multiple questions. If the same information really answers both questions, then one question (usually the newer one) should be closed as a duplicate of the other. You can indicate this by voting to close it as a duplicate or, if you don't have enough reputation for that, raise a flag to indicate that it's a duplicate. Otherwise tailor your answer to this question and don't just paste the same answer in multiple places.
  • tdc
    tdc about 4 years
    @MikeD that should be trash /private/var/log/asl/*.asl (missed slash)
  • gman
    gman over 3 years
    I have the newest version of oh-my-zsh, no changes, takes 20 seconds to start.
  • Marino
    Marino over 3 years
    This was the case for me too. Worth mentioning that any other version manager will likely also impact startup time (jenv, pyenv, rbenv...).
  • hunzter
    hunzter over 2 years
    This is a life saving. I was using both nvm and pyenv, both is time consuming.
  • Aidan
    Aidan over 2 years
    Ahhh this was what solved it for me! I had echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> /Users/aidanlister/.zprofile in my .zshrc. My .zprofile had thousands of lines of that in it. I deleted all but the first line from .zprofile, and commented out the offending line in my .zshrc, and now my terminal is fast again!
  • Admin
    Admin about 2 years
    this worked for me too