Creating and then using a NuGet local repository

In this blog post I am going to cover the creation and usage of a local NuGet repository.
In this example I am going to add the Microsoft Bits (Background Intelligence Transfer Service) dll to a web application.

1 – First of all I created a folder on my hard drive simply called NuGetPackages. (c:\NuGetPackages)

2 – Fire up VS 2010 and then create a new web application or open an existing one so that we can use this to test the new package were creating.

3 – Once you have the web application open, you’ll want to change where Visual Studio 2010 looks for the packages locally, to do this go to the Tools menu, and select Library Package Manager, from there then select Package Manager Settings (as seen below)

Screen shot of the menu to change the package settings

Once selected we need to change where Visual Studio and more specifically where NuGet is looking to try to find our local packages. Type a name and then paste in the location in the source textbox and press Add. You should see something like this:-

Change the location of where to find your packages

4 – Ok at this point we have only created a location to put the packages, and told NuGet where to locate them. Now we need to create a package.

5 – Time to create a package, first of all you need to create a file with the extension .nuspec and this file will be an xml file which will have the following structure:-

id The identifier for the package. It used as a partial name of the package.
version The package version. Also used as a partial name of the package.
authors Author(s) name. Use a comma to separate multiple authors.
description Describe your package for others to understand what the package is used for.

I created a file called Bits.nuspec and put this into the folder created in step 1) (c:\NuGetPackages) you can see my file below:-

Example content for a .nuspec file

6 – Now we need to build the package and to do this we need to have downloaded the Nuget command line tool which you can get from here:- Nuget command line tool

7 – To make life easier I copied this file (Nuget.exe) to the folder in step 1 (c:\NuGetPackages), now open a command prompt and then change to this directory c:\NuGetPackages, now to create the package just type:-

Nuget.exe pack Bits.nuspec

This should create a file with the extension .nupkg for me this was Bits. as it uses the version information as part of the name. (The version info comes from the .nuspec file)

8 – So far we have created a package and if you look at the last part of the code in Step 5, you’ll see a reference to files, this has *.dll and in this instance will add any dll’s found in this folder to the package. This would not be what you want if your going to be adding lots of different dll’s for other packages, you can change this line to read:-

<file src=”\bin\Release\*.dll” target=”lib” />

Where would be changed to relate to the package you want to add – the line above is just used as an example.

9 – To Install the package we will use the Package Manager Console from within VS 2010 which is installed when you install NuGet firstly you can type List-Package to see all the packages you have and then type Install-Package Bits to install the package called Bits.

10 – Now we should be ready to try it out – go back to VS 2010 and then this time select Tools, Library Package manager and then Add Library Package Reference as below:-

Add a library package

Select the package we installed, in this case Bits and click Install as below:-

Install a package

That’s all there is to it, hope someone out there found this of use.