Click here to Skip to main content
15,892,674 members
Articles / Web Development / ASP.NET
Tip/Trick

How to Use NuGet Packages

Rate me:
Please Sign up or sign in to vote.
3.32/5 (10 votes)
11 May 2015CPOL4 min read 67.7K   11   3
This is the fundamental tip about what NuGet packages are, the benefit of NuGet, how to install, update and remove NuGet packages using GUI and Command line.

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. 

Image 1

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.

Image 2

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.

Image 3

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:

Image 4

Click on “Manage” button. It will show a “Select Project” dialog box.

Image 5

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.

Image 6Installing 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.  

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) 3Pillar Global Pvt. Ltd.
India India
I am Sumit Gupta working in 3Pillar Global Pvt. Ltd as Module Lead. I have 7+ year of experience in .Net technologies. I love to explore new technologies and write technical article.

Comments and Discussions

 
QuestionNo reference for NuGet package? Pin
DJ Dave 23829-Jan-18 13:54
DJ Dave 23829-Jan-18 13:54 
QuestionExcellent article Pin
Paul of the Redwoods14-May-15 7:16
Paul of the Redwoods14-May-15 7:16 
Thank you for this! 5-star!
QuestionMy vote of 5! Pin
jediYL13-May-15 15:26
professionaljediYL13-May-15 15:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.