What is NuGet and how do I use it?
Most of you must have gone through the agony that you sometimes feel when adding libraries or tools or managing resource dependencies to use in your project manually, whether it’s a single DLL file to reference, or a pack of files to reference along with taking care of the versions and also updating your config files for them. Most of these issues come when you are trying to work in an already built system and trying to compile and run it to get it to work before you make any changes. Argh!!!! I was victim of these situations a number of times and really hated this time wasting process. But thanks to a great tool that I am about to explain, this process is so much easier and now it’s like 1…2…3 and BAM!!! There you go sir or madam.
Most of you may already be familiar with this tool and must be wondering why I am demonstrating such a basic tool. Well the reason is that this tool now helps us in almost all of our development projects. It’s important to explain this first so that in the following topics, the use of this tool will be known to all. So without further ado, I give you NuGet!
What is NuGet?
NuGet is an open source free package manager for Microsoft .NET Framework. It was formerly known as NuPack, but the name was changed due to confusion with another software with the same name. The NuGet client tools provide the ability to produce and consume packages in development in .NET Framework. It’s a Visual Studio extension that lets us easily manage libraries and tools in a Visual Studio development environment.
How does it work?
When you add a library or tool, NuGet copies files to your solution and automatically makes whatever changes are needed in your project, such as adding references and changing your app.config or web.config file. When you remove a library, NuGet removes files and reverses whatever changes it made in your project so that no clutter is left.
Everything you need to install a library or a tool is bundled into a package (a .nupkg file). A package includes files to copy to your solution/project, a manifest file that describes the contents of the package and instructions on what goes where and what needs to be done to the solution/project to add or remove the library. Packages are bundled into feeds that Visual Studio accesses in order to present lists of available packages. There is an official feed that is the default source for NuGet, and you can contribute to that feed or create your own feeds.
How do you use it?
You work with packages by using a dialog box in Visual Studio or by using PowerShell cmdlets in a dedicated Visual Studio window. Both options are accessible from the Visual Studio main menu; you can also open the dialog box from a Solution Explorer context menu. I will show how to use both ways in a while. The PowerShell cmdlets require PowerShell 2.0. Therefore, NuGet requires one of the following operating systems:
- Windows 8
- Windows 7
- Windows Vista SP1
- Windows Server 2008
- Windows Server 2008 R2
- Windows Server 2003 SP2
- Windows XP SP3
(**Note that NuGet can be used on Visual Studios 2010 or above only.)
Now let me demonstrate how you can use this tool to help you easily manage libraries and tools in your projects/solutions.
Well the first step is to get this baby installed. You can check first to see if your Visual Studios already has NuGet installed. Go to the Tools menu and look for the Library Package Manager. If you don’t find it, then to install NuGet you can use the Visual Studios Extension Manager like demonstrated below.
Go to Tools -> Extensions and Updates.
After clicking on the Extensions and Updates link, a popup window will open showing you all currently installed extensions on your Visual Studios. Using the top right search box, type in “nuget” and hit enter and the NuGet package will show up. As you can see my NuGet is already installed, but if you don’t have it installed on your Visual Studios you will be given the option to do so.
Right! Now that we have NuGet extension installed. Let us now use it in an actual development environment. There are 2 ways of using NuGet, as mentioned before. One is using the graphical dialog box in Visual Studio and the second is by using PowerShell cmdlets in a dedicated Visual Studio window. Let me first demonstrate it using the graphical dialog box that is a lot like the Extensions and Updates dialog box way of installing NuGet.
First thing is first; we need a project. In your Visual Studios 2012:
Go to File -> New -> and click Project.
From the templates on the left side, select Web and then select ASP.NET Web Forms Application from the list in the middle.
Rename the project to “MyTestWebApplicaiton” and then click OK.
In the Solutions Explorer, expand the Scripts folder. (Note that Visual Studios will automatically put in basic references to jQuery and related scripts by default.) You will see jquery version 1.7.1 along with jquery-ui version 1.8.20 and modernizr version 2.5.3.
Go to Tools -> Library Package Manager -> click Manager NuGet Packages for Solution…
A dialog box like the Extensions and Updates box we used before will appear with the latest in-use libraries list in the middle.
You can use the top right search box to search for the library you want to reference to in your project/solution.
Search the script libraries that you want and install or update them one by one. To demonstrate, I looked for jquery and clicked the install button shown on it. It will ask you to select the project(s) from your solution that you want the library to be added in before installing.
NuGet will automatically remove the old versions from the solution and put in the latest versions that are included in the selected package. Once this is done, repeat this step on all the script libraries you want to update. In our case, I also installed jquery-ui and modernizr.
I will now search for another script library named “knockoutjs” and install that to also be added into my solution for use. I have demonstrated this below.
Now if you refresh the Scripts folder and look inside it, you will notice that all the previous versions of the scripts in there are updated to newer versions and you will also see an additional script library (i.e. knockout 2.2.1).
So now that you have referenced/added all the libraries, you can use them in your project without any compilation errors or manual web.config entries, etc. Simple, isn’t it?
Let me briefly demonstrate the second way of using NuGet from a PowerShell cmdlets window inside the Visual Studios. Let’s say I want to use ASP.NET Razor 2 in this application. The following is what I would do to reference the required files in my solution.
Go to Tools -> Library Package Manager -> click Package Manager Console
A sub window at the bottom of Visual Studios will open. You will see instructions on how to get a list of commands that you can use in this Powershell cmdlet to use NuGet.
To Install Microsoft ASP.NET Razor 2 library, you will be using the Install-Package command like in the figure below. Note that you can also tell NuGet what version of the library you want. Neat, isn’t it?
If everything went smoothly during the installation then you will see a line that reads “successfully added 'Microsoft.AspNet.Razor 2.0.30506.0' to MyTestWebApplication.” That means that the installation went perfectly. To verify, you can expand the References section on the Solutions Explorer and see that the project now has a reference to “System.Web.Razor” and you can start using that library now in your project/solution.
So now I have demonstrated both ways of using this awesome tool to easily manage your libraries and references in your project. An additional advantage of using NuGet is that if a library is dependent on other libraries or tools then NuGet will automatically install them to be with the one you required. So you don’t have to move a finger to handle sub level dependencies of libraries; that is if the package has the necessary instructions to do so.
NuGet is a great open source tool to manage packages (Libraries and tools). You can also use it to create your own custom packages. It helps you by taking away all the work needed to manage libraries and dependencies in your project and also easily manages their versions so that you don’t have to waste time on them in the beginning like I mentioned in the Intro.
This was a fairly simple tool to demonstrate and most of you may be wondering why I started this series of blogs with this. The reason, as I mentioned before, is that this will be used mostly in my coming blogs on the more exciting and big stuff, so I thought it would be best to get it out of the way.
You can find detailed help from http://docs.nuget.org/ about NuGet if you are intrigued to learn its further advantages or go through the package gallery available on http://nuget.org/packages to see the packages available online to you.
With that, I end this first blog and hope that it helps you in your day to day programming tasks. I will be seeing you all in my next blog with the big stuff starting with “ASP.NET Web Forms.” So stay connected and keep a lookout for the next blog. Take care everyone.