This website may use cookies. More info. That's Fine x
Welcome Login

Nuget package manager (and package manager console)


Nuget is a free and open source package manager for the .net framework.

Nuget visual studio extension makes it easy to add, remove, and update libraries and tools in visual studio projects that use the .net Framework.

Nuget is distributed as a visual studio extension, and included in the popular c# code snippet tool linqpad.

 

 

Nuget package manager:

To install a package using nuget package manager ui:

Steps:

1. Open nuget package manager:

Method 1:

Tools > Nuget Package Manager > Manage NuGet Packages for Solution ...

 

Method 2:

Right-click on the References folder of your project > select 'Manage NuGet Packages...'

vs-nuget1

 

2. In Manage NuGet Packages, enter required package in Search Online, select package and click 'Install':

vs-nuget4

 

 

To uninstall a package using nuget package manager ui:

Steps:

1. Tools > Nuget Package Manager > Manage Nuget Packages for Solution.

2. Goto 'Installed packages' (left column) > select 'All'.

3. Select package from list of installed packages, and click Manage button.

4. Uncheck package and click OK.

vs-nuget8

 

 

 

Package manager console:

Alternatively, you can use the 'Package Manager Console' to install package:

Steps:

1. Goto: Tools > Library Package Manager > Package Manager Console:

vs-nuget2

 

2. Select a project from dropdownlist in Default project.

vs-nuget10

 

3. To install a package, enter package manager console command: 'install-package':

Eg: installing package: entityframework

PM > install-package entityframework

 

Eg2: installing a package

PM > install-package mvcscaffolding

 

Eg3: uninstalling a package and installing a package:

vs-nuget3

 

Note: Package Manager Console requires: PowerShell 2.0.

 

Note2: packages manager console is used in mvc migrations.

 

Note3: To fix broken package reference, try using following command:

update-package Owin -reinstall


-Reinstall flag: will cause nuget to re-apply the same version of the package that you already have installed. 
                 It's like a Repair operation. Generally, it will already be in the packages folder and hence won't need to download. 
                 Without this flag, the operation will update the package to the latest version, 
                 but only if there is a newer version, which may impact other packages that depend on it.

 

 

 

 

To update nuget package manager (vs2013):

Tools > Extensions and Updates... > Select Updates > Visual Studio Gallery > select 'Nuget Package Manager' and update.

 

vs-nuget7

 

vs-updatenuget1

 

For more information on update nuget, see here.

And here.

 

 

Nuget version:

To find nuget version:

VS > Help menu item > About Microsoft Visual Studio > look at the version displayed next to NuGet Package Manager.

Eg:

vs-nuget5

 

Alternatively, launch the Package Manager Console and enter $host:

Eg:

vs-nuget6

 

For nuget example to install entity framework, see here.

 

 

Package restore:

To reduce repository size, 'nuget package restore' installs all of a project's dependencies as listed in either the project file or packages.config.

Visual studio can restore packages automatically when a project is built.

The dotnet build and dotnet run commands (.net core 2.0+) also perform an automatic restore.

You can also restore packages at any time through visual studio, nuget restore, dotnet restore, and xbuild on Mono.

Package restore can be triggered in different ways:

To restore manually:

VS > right-click the solution in Solution Explorer > select Restore NuGet Packages.

 

vs-nuget9

 

If one or more individual packages are still not installed properly (meaning that solution explorer shows an error icon), then use the VS nuget package manager to uninstall and reinstall the affected packages.

For more information, see here.

 

 

To reinstall nuget package:

In package manager console:

update-package -Id <package_name> –reinstall

 

Using this command is much easier than removing a package and then trying to locate the same package in the nuget gallery with the same version.

Note that the -Id switch is optional.

 

 

To update nuget package:

The same command without -reinstall updates a package to a newer version, if applicable.

The command gives an error if the package in question is not already installed in a project, ie: update-package does not install packages directly.

update-package <package_name>

 

To reinstall and update nuget packages, see here.

 

 

packages.config file:

packages.config is a file used in projects that utilize nuget package manager.

packages.config is used to keep track of all the nuget packages that are installed in a project.

It lists the name and version of each package, along with any dependencies they may have.

This file is typically located in the root directory of the project.

When you install a nuget package into your project using the nuget package manager in vs, it automatically updates the packages.config file to reflect the newly added package and its version.

This allows vs to manage dependencies and ensure that the correct versions of packages are referenced in your project.

 

packages directory:

Nuget packages are typically installed into a directory called packages within your visual studio solution directory.

This packages directory is created automatically when you install the first nuget package into your project.

Inside the packages directory, you'll find subdirectories for each installed package.

These subdirectories contain the actual package files, including binaries, metadata, and any other files necessary for the package to function.

Each package subdirectory is named according to the package's ID and version, making it easy to identify which package is stored where.

This structure allows visual studio to manage packages efficiently and resolve dependencies correctly when building or running your project.

 

Eg: packages.config file and packages directory:

vs-nuget11


Created on: Monday, March 11, 2013 by Andrew Sin