1

I am trying to clone a repository but this error shows up:

$ sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
Cloning Oh My Zsh...
fatal: could not create work tree dir '/usr/share/oh-my-zsh': Permission denied
Error: git clone of oh-my-zsh repo failed

If I use sudo before the previous command it writes to the root directory as you can see:

$ sudo sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
The $ZSH folder already exists (/root/.oh-my-zsh).
You'll need to remove it if you want to reinstall.

What's to be done to solve this? I know that git clone should write to the current directory, but it doesn't! Why?

Schwern
  • 153,029
  • 25
  • 195
  • 336
techsk8
  • 367
  • 4
  • 17
  • This is not Git, this is the install.sh script you're running. It's given you instructions on how to fix the issue. Be wary of running scripts you don't understand, *especially* as root. This one is trying to install its configuration file (.oh-my-zsh) in root's home folder, not yours. – Schwern Feb 18 '21 at 21:19
  • I am not running as root, and yes I have installed this package a dozen times on other machines so I am sure what I am installing. – techsk8 Feb 18 '21 at 21:21
  • `sudo` executes the command as root. – Schwern Feb 18 '21 at 21:22
  • Did you have the ZSH environment variable set when you ran it the first time? – Schwern Feb 18 '21 at 21:27
  • No I did not, It does it automaticaly. And yes I know that sudo=superuser do, but that does not change the install directory to root also. That's what happens to me. That's what I was trying to explain. – techsk8 Feb 18 '21 at 21:44
  • I think it's clear that the error says: `Error: git clone of oh-my-zsh repo failed`, because it has to clone it first then run the install. It gives me the same error with every other repo I'm trying to clone: `git clone https://github.com/zsh-users/zsh-autosuggestions.git $ZSH_CUSTOM/plugins/zsh-autosuggestions fatal: could not create work tree dir '/usr/share/oh-my-zsh//custom/plugins/zsh-autosuggestions': Permission denied` – techsk8 Feb 18 '21 at 21:47
  • It seems to me that my user does not have some permission that it needs in order to write to that directory. – techsk8 Feb 18 '21 at 21:50
  • You told it to clone into `$ZSH_CUSTOM/plugins/zsh-autosuggestions` which means $ZSH_CUSTOM is set to `/usr/share/oh-my-zsh//custom`. You don't have permission to write to `/usr/share/`, this is normal. – Schwern Feb 18 '21 at 21:50
  • Yeah, I think you're right. I got that clear. – techsk8 Feb 18 '21 at 21:54

1 Answers1

2

This is not Git, this is the install script for oh-my-zsh.

install.sh is trying to clone a repository. Where it installs is controlled by the ZSH environment variable. By default it will install into ~/.oh-my-zsh. If it went into /usr/share/oh-my-zsh you had ZSH set to that. You don't have permission to write to /usr/share. This is normal, a regular user should not have /usr/share permission.

When run with sudo (ie. with root permissions) it does have permission to write to /usr/share/oh-my-zsh, but sudo will clean up your environment variables so ZSH was not set for the command. It ran with its default ~/.oh-my-zsh. Normally sudo should preserve your home directory, but somehow it isn't and it's gone into root's home directory.


How to "fix" this depends on what you want. First, check what's in your environment with env | sort and make sure it's all correct.

If you just want to install the software for yourself, unset ZSH and run the installer. It will go into ~/oh-my-zsh.

If you want to install the software for everyone, download the script, use sudo and make setting ZSH part of the command.

cd /tmp
curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh
sudo ZSH=/usr/share/oh-my-sh sh -c install.sh
Schwern
  • 153,029
  • 25
  • 195
  • 336