Configuring NuGet for use with GitHub Packages

You can configure NuGet to publish packages to GitHub Packages and to use packages stored on GitHub Packages as dependencies in a .Net project.

GitHub Packages is available with GitHub Free, GitHub Pro, GitHub Team, and GitHub Enterprise Cloud. GitHub Packages is unavailable for per-repository plans, which are legacy billing plans. For more information, see "GitHub's products."

In this article

Authenticating to GitHub Packages

You need an access token to publish, install, and delete packages in GitHub Packages. You can use a personal access token to authenticate with your username directly to GitHub Packages or the GitHub API. You can use a GITHUB_TOKEN to authenticate using a GitHub Actions workflow.

Authenticating with a personal access token

You must use a personal access token with the appropriate scopes to publish and install packages in GitHub Packages. For more information, see "About GitHub Packages."

To authenticate to GitHub Packages with NuGet, add GitHub Packages as a Source to the nuget client. Replace USERNAME with the name of your user account on GitHub, TOKEN with your personal access token, and OWNER with the name of the user or organization account that owns the repository containing your project. Because upper case letters aren't supported, you must use lowercase letters for the repository owner even if the GitHub user or organization name contains uppercase letters.

nuget sources Add -Name "GPR" \
     -Source "https://nuget.pkg.github.com/OWNER/index.json" \
     -UserName USERNAME -Password TOKEN

Authenticating with the GITHUB_TOKEN

If you are using a GitHub Actions workflow, you can use a GITHUB_TOKEN to publish and consume packages in GitHub Packages without needing to store and manage a personal access token. For more information, see "Authenticating with the GITHUB_TOKEN."

Publishing a package

You can publish a package to GitHub Packages when you specify the Source as GPR. GitHub Packages will use the same value for OWNER that you authenticate with. For more information on creating your package, see "Create and publish a package" in the Microsoft documentation.

After you publish a package, you can view the package on GitHub. For more information, see "Viewing packages."

  1. Authenticate to GitHub Packages. For more information, see "Authenticating to GitHub Packages."

  2. Use the nuget command, replacing PACKAGE with the name of your package.

    $ nuget push PACKAGE.nupkg -Source "GPR"

Publishing multiple packages to the same repository

When you publish a package, by default GitHub Packages uses the package name to determine the GitHub repository containing the package. For example, a package named odata-client would be published to the OWNER/odata-client repository.

If you would like to change the repository containing the package, or publish multiple packages to the same repository, you can include the URL to the GitHub repository in the repository field of the package's .nuspec file. GitHub will match the repository based on that field, instead of the package name. If you have a .csproj file created by Visual Studio, the version you use in your .nuspec file must match the version in your .csproj file.

  1. Add the URL of the GitHub repository in the repository field of the package's .nuspec file.

    <?xml version="1.0" encoding="utf-8"?>
    <package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
        <metadata>
        <id>sample</id>
        <version>1.2.3</version>
        <authors>Kim Abercrombie, Franck Halmaert</authors>
        <description>Sample exists only to show a sample .nuspec file.</description>
        <language>en-US</language>
        <repository type="git" url="https://github.com/my-org/my-custom-repo"/>
        </metadata>
    </package>
  2. Create the package using the configuration in the package's .nuspec file.

    $ dotnet pack --configuration Release
  3. Publish the new package to GitHub, replacing PACKAGE with the name of your package.

    $ nuget push PACKAGE.nupkg -Source "GPR"

Installing a package

Using packages from GitHub in your project is similar to using packages from nuget.org. Add your package dependencies to your package.config, specifying the package name and version. For more information on using a packages.config in your project, see "Working with NuGet packages" in the Microsoft documentation.

  1. Authenticate to GitHub Packages. For more information, see "Authenticating to GitHub Packages."

  2. Configure packages.config to use the package, replacing the octo-app package with your package dependency.

     <?xml version="1.0" encoding="utf-8"?>
       <packages>
         <package id="octo-app" version="3.1.1" />
       </packages>
     </xml>
  3. Restore all packages.

    $ nuget restore

Further reading

Ask a human

Can't find what you're looking for?

Contact us