Resolving SSH Key Issues for Yarn Install with GitHub

halibal

Halibal

Posted on October 14, 2024

Resolving SSH Key Issues for Yarn Install with GitHub

SSH Key Issue Resolution for Yarn Install with GitHub

Overview

When running yarn install in a project that required fetching dependencies over SSH from GitHub, the installation process failed with a Permission denied (publickey) error. This document outlines the steps followed to resolve the issue, including troubleshooting SSH keys, passphrase prompts, and configuring the environment for a successful installation.

Problem

When attempting to install dependencies via yarn, an error occurred when Git tried to access a private repository through SSH. The error looked like this:

git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.
Enter fullscreen mode Exit fullscreen mode

Further investigation revealed that while the correct SSH key was being offered, the passphrase prompt could not be handled in the non-interactive environment of yarn, resulting in the following error:

debug1: read_passphrase: can't open /dev/tty: No such device or address
Enter fullscreen mode Exit fullscreen mode

Steps to Resolve

Step 1: Ensure SSH Agent Is Running

First, verify that the SSH agent is running, which is necessary for managing and using SSH keys without needing to enter the passphrase repeatedly.

  • Open PowerShell and run the following command:
  Get-Service ssh-agent
Enter fullscreen mode Exit fullscreen mode

If the service is not running, start it:

  Start-Service ssh-agent
Enter fullscreen mode Exit fullscreen mode
  • If you encounter an issue trying to configure the SSH agent to start automatically, such as an Access is denied error when running the following:
  Set-Service ssh-agent -StartupType Automatic
Enter fullscreen mode Exit fullscreen mode

Make sure PowerShell is run as Administrator.

Step 2: Add SSH Key to the Agent

Load the SSH key into the agent to avoid passphrase prompts during yarn commands.

  • Add the key to the agent:
  ssh-add $HOME\.ssh\id_ed25519
Enter fullscreen mode Exit fullscreen mode

If prompted, enter your passphrase to cache it for future use. After this step, the key will be automatically used in SSH connections, without requiring manual input.

Step 3: Test SSH Authentication

To ensure that your SSH key is correctly set up and that GitHub can authenticate with it, test the SSH connection:

  • Run:
  ssh -T git@github.com
Enter fullscreen mode Exit fullscreen mode

You should see a message like:

  Hi username! You've successfully authenticated, but GitHub does not provide shell access.
Enter fullscreen mode Exit fullscreen mode

Step 4: Run Yarn Install with Verbose SSH Debugging

To troubleshoot why the yarn install command was failing, run the command with verbose SSH logging to see exactly what's happening during the process:

  • Set the environment variable and run:
  $env:GIT_SSH_COMMAND="ssh -v"
  yarn install
Enter fullscreen mode Exit fullscreen mode

This provided detailed information, including the issue that yarn could not open /dev/tty to prompt for the SSH key's passphrase, which caused the failure.

Step 5: Remove Passphrase from SSH Key (Optional, if needed)

Since the key's passphrase was causing issues in the non-interactive yarn install environment, removing the passphrase from the SSH key provided a solution.

  • Run the following command to remove the passphrase from your SSH key (replace the path as necessary):
  ssh-keygen -p -f C:\Users\USER\.ssh\id_ed25519
Enter fullscreen mode Exit fullscreen mode

You will be prompted to enter the current passphrase and then leave the new passphrase blank by pressing Enter.

Step 6: Re-run Yarn Install

Once the passphrase was removed, the key no longer required manual input, and yarn install completed successfully without further errors.

  • Run:
  yarn install
Enter fullscreen mode Exit fullscreen mode

Conclusion

By ensuring the SSH agent was running, loading the SSH key into the agent, and removing the passphrase from the SSH key, the issues with yarn install and SSH authentication were resolved. This approach eliminates the need for interactive passphrase entry, enabling automated installations without errors.

Notes

  • If you want to keep a passphrase on your SSH key, you will need to configure the system to prompt for the passphrase before running any yarn or git commands, or ensure the key is added to the SSH agent before any non-interactive commands.
  • If you face issues again, start by ensuring the SSH agent is running and your keys are loaded using the ssh-add command.

See it on GitHub Gists

💖 💪 🙅 🚩
halibal
Halibal

Posted on October 14, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related