NuGet may sounds like something yummy and a package of NuGet can't be obtained from any typical supermarkets out there. It's not a food item hence it's missing one letter G.
It's a way to manage third party libraries in any .Net projects during development using Visual Studio (VS). Typically, libraries can be referenced directly by putting selected dynamic-link library (DLL) files into a project. In VS, this can be done by Add Reference feature. However, some library packages come together with open source code and several dependencies. We might want to add or edit some codes, wherever possible, to suit our demands and preferences.
With NuGet, through its package manager program, those library packages will be copied to the VS solution and automatically adds any references or change any config files to match the requirement of the package that being installed. Uninstallation of the package will revert any changes it made back to original state. Nice, huh.
To take advantage of NuGet, one's machine first need to have PowerShell 2.0 installed. Those with Windows 7 or Windows Server 2008 R2 can directly install NuGet Package Manager since Powershell 2.0 is built-in into these two operating systems. Machines with Windows XP SP3, Server 2003 SP2, Vista SP1, or Server 2008 however, needs Powershell 2.0 to be installed manually. Get it here. Not only that, NuGet will only works on VS2010 or later. Since VS2012 was out a couple of months ago, VS2010 can be considered mature (if not old) by now.
This tutorial is based on my current work environment, which is a Windows 7 machine with VS2010 SP1 installed. NuGet package manager was installed when I installed VS2010 earlier. Although it's quite simple to install a NuGet package into VS2010 project, this post may help those who are new to NuGet, just like myself. Initally, I intended to get a few third-party projects or solutions into my project. I downloaded them and then I realized those solutions were NuGet-packaged since they have .nupkg extensions. How did I manage to get those files into my project then? Here's how.
- I opened my project in VS2010.
- Once the project loaded up, go to Tools --> Options. On Options window, select Package Manager from the listbox on the left. By default, there's only 1 link of available package source, which is the official link from Microsoft. I added another location for the package source by clicking on the "..." button. I browsed to the folder where I put those NuGet packages inside. Name for the source should as well be provided. Click on Add button. Once added to the list of Available package sources, click OK.
- Inside Solution Explorer, I right-clicked on the project where I want to unload those packages to. Clicking on Add Library Package reference will open up another window. On the left pane, I saw the name of the package source I just created in step 2 and chose that.
- A list of available packages will be displayed and all I did was clicked on Install button on any packages I want to unpack.
Once installed, the package will appear inside the solution together with a newly created packages.config file that keeps some information on the installed package(s). From there, the package(s) can be manipulated accordingly. To uninstall any of the unpacked packages, select All under the Installed packages. The list on the right of it will show all packages just got installed. Click on Uninstall button and VS2010 will do the rest, reverting back the project to its initial state before packages were installed.
There is another way to achieve similar results but a little slower and requires some knowledge of the commands to be executed. It's by using a command-line program called Package Manager Console.
Select Package Source and Default Project from two comboboxes available on top of the console. Since this console requires input from users, feel free to learn some commands from here.
To see a list of all available packages inside the source folder, type in Get-Package -ListAvailable and press Enter. Be reminded, if you select All from the Package Source combobox, thousands of packages will be listed.