Introduction
NuGet is a Package management system for Visual Studio. It makes it 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. NuGet is a Package management system for Visual Studio. It makes it 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 like:
- First of all, we need to search a package over the 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 from 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, 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 for the 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 adds 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 the 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 the 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 the 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 NuGet from the 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 <PackageName>
For example:
Install-Package elmah
This command will install the Elmah
package in your current project.
Updating a Package
To update the NuGet
package, use:
Update-Package <PackageName>
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 <PackageName>
For example:
Uninstall-Package Elmah
Summary
So in this way, you can install, update and remove NuGet packages in your project both from using GUI and command line. This tip does not include all the switches of command. For the complete list, please refer to https://docs.nuget.org/consume/command-line-reference.