What is NuGet and how to install its packages into Visual Studio 2010

It's very easy to install the Newtonsoft.Json into the project with NuGet!

Today I'm gonna talk about a Visual Studio extension which makes things very easy for us developers: NuGet.

What is Nuget?

NuGet (don't mistake with nugget) is a Visual Studio extension which helps the library management and on Visual Studio. It helps a lot to add references to a project and also to keep them up to date.

Example: You are working on a project which needs to use Json and just found an amazing library (Json.NET) which can help you to serialize all List<> as you want to return them in json format. You have 2 choices: 1) Add and update the reference manually always when there is a new version available; 2) Install via Nuget and let it take care of the updates.

Where do I download?

Nuget is normally installed when you download and install ASP.NET MVC 3 but, in case you don't have Nuget yet or it had some problems, you can download it directly from the Visual Studio Galery.

How do I install a library via NuGet?

  1. Using Visual Studio go to the Solution Explorer, right click in References and select Manage NuGet Packages

  2. When the window open you can notice 4 items on the left side:
    1. Installed packages: List of libraries (packages) already installed;
    2. Online: Packages available for download;
    3. Updates: Updates available for your installed packages;
    4. Recent packages: List of packages which you recently used.

  3. Select the Online item (normally pre selected) and use the search field at the right side (Search Online) to look for the desired package. In our case, type Json.NET and wait. The results will appear after few seconds (according to your connection)

  4. Click in Install at the first item. Nuget will download the package automatically and also install it in your project

  5. After finished you can close the Nuget window. Notice that, besides the reference to Json.NET (Newtonsoft.Json) have been added, a file named packages.config was created in the root of the project

  6. This files has the names and versions of all Nuget packages which were added to your project. I would recommend you to not touch it. :)

There is another way to install the packages, which is via command line. If you want to do so just go to  menu View -> Other Windows -> Package Manager Console. When the window open you can just type the command which is written at the page of the project you want to install. For Json.NET the command is Install-Package Newtonsoft.Json.

I honestly prefer to install the packages using the Nuget Manager. It's more centralized and just few clicks far away. Besides, I don't need to open the Nuget website for that. ;)