How to fix npm ERR! git clone git@github.com:user/repo.git failed npm ERR! Permission denied (publickey) in npm
This error occurs when npm tries to clone a Git dependency using SSH but your SSH key isn't configured or recognized. Common in CI/CD environments, new machines, or when using the wrong SSH key.
Quick fix
Fixes npm ERR! git clone git@github.com:user/repo.git failed npm ERR! Permission denied (publickey)
ls -la ~/.ssh/
# Look for id_rsa/id_rsa.pub or id_ed25519/id_ed25519.pub- Check if SSH keys exist
- Add SSH key to ssh-agent
- Add public key to GitHub/GitLab
- Test SSH connection
ls -la ~/.ssh/# Look for id_rsa/id_rsa.pub or id_ed25519/id_ed25519.pubnpm ERR! git clone git@github.com:user/repo.git failednpm ERR! Permission denied (publickey)
What this error means
When your package.json contains a Git dependency using SSH syntax (like `git@github.com:user/repo.git`), npm needs SSH authentication to clone the repository. This error means SSH authentication failed—typically because no SSH key is available or the key isn't authorized for the repository. This is one of the most common issues when: - Setting up a new development machine - Running npm install in CI/CD pipelines - Working with private repositories - The SSH agent isn't running or doesn't have your key loaded
Typical symptoms
- npm install fails with 'Permission denied (publickey)'
- Error shows 'git clone git@github.com:...' failed
- Works on some machines but not others
- Fails in CI/CD but works locally
- Error code 128 from git
Common causes
- SSH key not generated on this machine
- SSH key not added to GitHub/GitLab account
- SSH agent not running or key not loaded
- SSH key has a passphrase (npm can't prompt for it)
- Using wrong SSH key for the repository
- CI/CD environment doesn't have SSH key configured
How to fix "npm ERR! git clone git@github.com:user/repo.git failed npm ERR! Permission denied (publickey)"
First, verify you have SSH keys:
ls -la ~/.ssh/
# Look for id_rsa/id_rsa.pub or id_ed25519/id_ed25519.pubIf no keys exist, generate one:
ssh-keygen -t ed25519 -C "your_email@example.com"Start the SSH agent and add your key:
# Start ssh-agent
eval "$(ssh-agent -s)"
# Add your key
ssh-add ~/.ssh/id_ed25519
# Or for RSA keys:
ssh-add ~/.ssh/id_rsa
# Verify key is loaded
ssh-add -lCopy your public key and add it to your Git hosting service:
# Copy public key to clipboard (macOS)
cat ~/.ssh/id_ed25519.pub | pbcopy
# Or display it to copy manually
cat ~/.ssh/id_ed25519.pubThen:
1. Go to GitHub → Settings → SSH and GPG keys
2. Click "New SSH key"
3. Paste your public key and save
Verify SSH authentication works:
ssh -T git@github.com
# Expected output:
# Hi username! You've successfully authenticated...If this fails, the key isn't properly configured.
If you can't configure SSH, switch to HTTPS:
# Configure git to use HTTPS instead of SSH
git config --global url."https://github.com/".insteadOf "git@github.com:"
# Or update package.json to use HTTPS URL{
"dependencies": {
"my-package": "git+https://github.com/user/repo.git"
}
}Advanced notes
CI/CD Configuration:
For GitHub Actions:
- uses: webfactory/ssh-agent@v0.8.0
with:
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
- run: npm ciPassphrase-Protected Keys: npm cannot prompt for SSH key passphrases. Either use a key without a passphrase (for automation) or ensure your ssh-agent has the key loaded.
Windows: Ensure OpenSSH Authentication Agent service is running:
Get-Service ssh-agent | Set-Service -StartupType Automatic
Start-Service ssh-agentMultiple SSH Keys: If you have multiple keys, create ~/.ssh/config:
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/github_keyFrequently asked questions
- What does "npm ERR! git clone git@github.com:user/repo.git failed npm ERR! Permis" mean?
- git), npm needs SSH authentication to clone the repository. This error means SSH authentication failed—typically because no SSH key is available or the key isn't authorized for the repository.
- How do I fix "npm ERR! git clone git@github.com:user/repo.git failed npm ERR! Permis"?
- This error occurs when npm tries to clone a Git dependency using SSH but your SSH key isn't configured or recognized. Common in CI/CD environments, new machines, or when using the wrong SSH key.
- What causes "npm ERR! git clone git@github.com:user/repo.git failed npm ERR! Permis"?
- SSH key not generated on this machine SSH key not added to GitHub/GitLab account SSH agent not running or key not loaded
- Is the "npm ERR! git clone git@github.com:user/repo.git failed npm ERR! Permis" error serious?
- It is a medium-severity error — more of a blocker than a risk. The steps above resolve it.
Official resources & further reading
- 📚npm Official Documentation
- 📖npm Docs: Installing Packages(Official Docs)
- 💬Stack Overflow: Git SSH credentials with npm(Stack Overflow)
- 💬SuperUser: SSH permission denied(Stack Overflow)