Managing references to external resources is a time consuming task for developers, as each development environment must be properly set up to ensure everything works as planned. NuGet is an open source package management system that simplifies the work by taking care of all the details within the confines of Visual Studio, allowing the developer to focus on the more important work.

The problem

The problem addressed by NuGet is not a new one for developers. You install an assembly and add references to your project to utilize the assembly in code. This usually goes well, but problems arise when changes are committed to source control. This is followed by another developer updating source control, and a build is broken if the referenced assemblies are not added as well. NuGet solves the problem by adding a packages.config file and putting it under source control; all references are included in this config file.

The package

The package is the central component of NuGet. Everything needed to install a library or tool is packaged together in a .nupkg file. It includes all necessary files that will be copied, as well as a manifest (xml) describing what is in the package and what is done to add or remove the library.

Packages may be created with the NuGet command line tool or via the Package Manager interface. The simplest package is created for a single assembly file. NuGet creates the manifest file from the assembly and then creates the actual package via command line directives. It is worth noting that packages have standard directories: tools, lib, and content to organize package artifacts, and you can include folders for specific .NET versions as well. More information on creating packages is available online.

The next step is to bundle a package into a feed that Visual Studio accesses to give you installation options as shown in the next section. Right now, there is one official public feed that is the source for everything, but you can create your own local feeds for organizations that want to restrict what their development team uses.

Installing NuGet

NuGet is freely available as a CodePlex open source project. It simplifies the many caveats of utilizing external libraries in your Visual Studio 2010 projects. It also supports Visual Web Developer 2010 and any .NET project type. It does require and extensively uses PowerShell 2.0, and you can create your own PowerShell scripts as well. I have seen references to it working with SharpDevelop with previous NuGet versions, but I have not tried to use it.

NuGet may be utilized via a command line interface as well as point-and-click within the Visual Studio environment. Installation begins by downloading the installation package or via the Visual Studio Extension Manager via the Tools menu (Figure A). You can search for NuGet and then install it. Once it is installed, it is displayed in the Installed Extensions section of the Extension Manager (Figure B).
Figure A

Installing NuGet via Visual Studio Extension Manager (Click the image to enlarge.)

Figure B

Viewing installed extensions (Click the image to enlarge.)

Adding references with NuGet

Once installed, you can simply add references to your projects via the Tools | Library Package Manager option (Figure C) or use the context menu (right-click on project) (Figure D) and select Add Library Package Reference.
Figure C

Accessing Package Manager via Visual Studio Tools menu (Click the image to enlarge.)

Figure D

Accessing Package Manager by right-clicking on project (Click the image to enlarge.)

Adding new references (Figure E) follows the same pattern as adding new extensions. A search box allows you to search for libraries in the feed by name, or you may select the All option to view all available libraries in the feed.
Figure E

Adding references to a Visual Studio project (Click the image to enlarge.)

The list displayed in Figure E shows various options, including the jQuery and log4net libraries. A reference is added by selecting it in the list and clicking the Install button. Once installed, it will be available to the project, and it will appear in the Installed Packages section of the Library Package Manager. Figure F shows a project with the Google.GData.Documents library.
Figure F

Viewing installed references via the Library Package Manager (Click the image to enlarge.)

You can easily remove a package once it has been added to a project. Removal is as easy as clicking the Uninstall button for installed packages as shown in Figure F. Be careful when using this option, because there is no confirmation (aka, are you sure?) window. You can also use the Uninstall-Package command-line directive.

Making it easy to focus on development

I can remember the day when Microsoft and open source was like mixing oil and water, but the recent Microsoft TechEd conference provided plenty examples of Microsoft embracing the open source community. NuGet is one such product.

NuGet exemplifies the power of open source as needs are identified with solutions crafted, whereas a company like Microsoft may never identify such a need or feel it is important. The community recognized a way to streamline a development task with NuGet.

NuGet allows a reference or tool to be easily added, and the developer can move forward with coding or other important tasks. I expect it to be fully integrated with Visual Studio in future releases.