How to Use NuGet Packages and the NuGet Gallery

The steps below describe how to use NuGet packages and the gallery.

Before we dive into the details of NuGet, we first need to understand what NuGet is and what it is used for. Nuget is a library sharing mechanism. It allows developers to create and share packages. These packages are containers for code. Microsoft uses NuGet to publish a number of useful packages (Entity Framework is one very useful example.) Nuget can be used by any developer to share code with the Visual Studio Community.

Nuget has two primary interfaces:

  1. The Package Manager User Interface (UI). The UI provides a quick, simple, and full featured interface for adding packages to your project. You can use it to add, update, remove, and enumerate packages in your solution.
  2. The Package Manager Console. The Console is basically a Powershell interface into the Nuget Library. It allows you to work with the library using PowerShell Command. For example, Get-Package-ListAvailable will list all the available packages in the default source location.

We will examine each interface in turn and describe how to use it within the context of a Visual Studio project.

Let's begin by exploring the user interface (UI):

  1. Open the UI, right-click on project-references, and choose manage NuGet Packages.
    Manage Nuget Packages
    OR from the menu bar select Tools-NuGet Package Manager-Manage Nuget Packages for Solution
    Tools-NuGet Package Manager-Manage Nuget Packages 
	 For Solution
  2. The Browse tab is selected by default. From the Browse tab, you can search for the package you want to install. For example, in the search box, type "Entity" to search for the Entity Framework Package.
    Browse to appropriate Library
  3. Select the appropriate version (see note below) and which projects (if any) in the solution into which you would like to install the package. Then click Install.
    Choose The Appropriate Version
  4. NuGet will download the package, install it on the local maching and add a reference to it in the selected project.
  5. Once a package is installed, you can use the NuGet manager to upgrade the package (if a new version or updates are released). You can also use it to uninstall packages from your solution.

Now let's take a look at the Package Manager Console.

  1. To open the console, select Tools-Nuget Package Manager-Package Manager Console
    Tools-Nuget Package Manager- Package Manager 
  2. Select package source and default project as appropriate
    Set Package Source
  3. Enter Powershell commands as appropriate. For example:
    1. Get-Package-ListAvailable
    2. Install-Package EntityFramework
    3. Update-Package EntityFramework

    Enter Powershell Command
  4. Note: When working with packages, be aware of package version numbers. Often, package updates can contain breaking changes that require extensive changes to your project.

Author: Michael Osborne
About Webucator

Webucator provides instructor-led training to students throughout the US and Canada. We have trained over 90,000 students from over 16,000 organizations on technologies such as Microsoft ASP.NET, Microsoft Office, Azure, Windows, Java, Adobe, Python, SQL, JavaScript, Angular and much more. Check out our complete course catalog.