How to use NuGet Packages
Overview
NuGet is a Package management system for Visual Studio. It makes easy to add, update and remove external libraries in our application. Using NuGet, we can create our own packages easily and make it available for others.
Life before NuGet
Before NuGet, if we want to use some external packages in our application then we need to face many challenges lik
- First of all we need to search a package over internet that fulfills our requirement and then download the correct version of it.
- Install and configure it correctly by following the instruction given.
- And the most difficult task is to track the update of that package.
NuGet makes our life easy and free from all these challenges. NuGet automates all these tedious tasks from discovering and installing package to updating packages.
Installing NuGet
If you have Visual Studio 2012 or higher then you don’t need to install NuGet because it comes already installed. And if you are using Visual Studio 2010 then you can install it by opening Visual Studio Extension Manager which is under Tools menu. Then search for NuGet Package Manager under Online Gallery tab and install it.
Using NuGet GUI
Adding a Package to your Project
For adding a package, right click the References node in the Solution Explorer and click on Manage NuGet Packages… option.
It will open a dialog box, here type the desired package name in search text box at the top right side.
When you select the package, it shows package information in right side pane like Created By, Id, Version, Downloads, Description, Dependencies and etc.
Now click the Install button, it will download the package as well as its dependencies if any and install the package in your application.
Once installed, it makes few changes in your project like if you are adding a package first time then it will create a file named packages.config. This file keeps a list of all packages that are installed in your project.
It also creates a folder named packages in the directory where your solution (.sln) file resides. Packages folder contains a subfolder for each installed package with version number.
NuGet automatically add the reference of library and makes the necessary changes in config file. So you don’t need to do anything and now you are ready to use the package in your application.
Updating Existing Packages in your Project
Updating NuGet package in your project is very easy. Open the Manage NuGet Packages dialog box and click on Updates node in the left pane. It will display a list of packages that have new version.
Click on Update button next to package name. It will update the latest package for you and also update the dependencies of that package if any.
Uninstalling a Package from your Project
Open the “Manage NuGet Packages” dialog box and select “Installed Packages” from left section. It will show the entire installed packages list.
Click the package name you want to uninstall and it will show a button “Manage” like in below screen shot:
Click on “Manage” button. It will show a “Select Project” dialog box.
Here uncheck the check box of your project from which you want to uninstall the package and click on OK button.
It will remove the package from your project.
Using Command Line
Visual Studio also provides the command line interface to manage NuGet packages. It provides Power Shell based console named “Package Manager Console”.
To open the console Go to Tools -> Library Package Manager -> Package Manager Console.
Installing a Package
To install a new package use the following command:
Install-Package
For example:
Install-Package elmah
This command will install the Elmah package in your current project.
Updating a Package
To update a existing package use the following command:
Update-Package
This command will update to the latest version of package. If you want to update a certain package then use -version switch like below
Update-Package Elmah -version 1.1
To update all NuGet packages in your project you can simply execute Update-Package command without any parameter.
Uninstalling a Package
To Uninstall the NuGet package use
Uninstall-Package
For Example:
Uninstall-Package Elmah
This command will uninstall the Elmah from your current project.