Creating Local Packages with NuGet

NuGet allows us to download and install packages to our solutions using the NuGet manager in Visual Studio (or from the command line using nuget.exe) from a remote server – for example, nuget.org. But what if you want to host your own packages locally or remotely (outside of nuget.org) for example due to workplace policies or simply not wanting to publish your own libraries to a public server.

All the information contained in this post was gleaned from the references below. These are fantastic posts and I would urge anyone visiting this site to go and read those resources. What I want to do here is just distill things down to some basics to remind myself how to quickly and easily reproduce packages and spec for packages

Getting Started

Let’s start of by creating a local (i.e. on your development machine) NuGet package folder.

  1. Simply create a folder on your machine which will be used to store your packages, let’s call it LocalPackages
  2. In Visual Studio 2012, select Tools | NuGet Package Manager | Package Manager Settings
  3. In Package Sources you’ll see the nuget.org source, click the + button, give your source a name such as “Local Feed” and set the Source to the location of your LocalPackages folder
  4. Press OK and we’ve now added a new package source

At the point we now have Visual Studio set up to read from our packages folder – now we need to create some packages.

Creating a package

So we need to create a nupkg file (which is basically a compressed file which includes binaries and configuration – change the extension to .zip and open with your preferred zip editor/viewer to see the layout). This can be created using the Package Explorer GUI or via a nuspec file and nugget.exe.

If you don’t already have it, download and install the command line version of NuGet which can be found here see Command Line Utility. I placed mine in the folder I created previously, LocalPackages, as I will run it from here to work with the nupkg files etc.

As I prefer to let tools do the heavy lifting for me, I’ve downloaded the clickonce application, Package Explorer GUI.

With the package explorer we can easily create nupkg files without a care in the world for what’s inside them :)

So let’s create a package very quickly using the Package Explorer GUI

  1. If you haven’t already done so, download/run the Package Explorer Gui
  2. Create a new package
  3. We can now edit the meta data or meta data source from the UI – the metadata editor gives us, a simple GUI with input fields and the meta data source is basically an text editor (well it’s a little better than that but you get the idea). For now let’s stick with the GUI meta data editor with it’s nice input fields
  4. Enter an Id – this is the package name, it’s the name used when installing the package
  5. Supply a version. NuGet allows us to download specific package versions. The version should be in the format 1.2.3
  6. Now supply a human friendly title, if none is supplied the Id is used instead
  7. Now enter a comma separated list of authors
  8. I’m not going to step through every UI element here – check out the Nuspec Reference for a complete list of fields and what’s expected
  9. We can add any framework assemblies and both dependencies and assmebly references via the GUI, but also we need to actually add our assemblies, i.e. what the package has been created to deploy. We drag and drop those into the right hand pane of the GUI tool
  10. We can right click on the right pane to add files and folders and more, all of which are a little beyond this first post
  11. For now just drag and drop an assembly you wish to package and accept the defaults
  12. Save the nupkg file to the LocalPackages folder

If you now attempt to open this nupkg file, you’ll find it’s a binary file. If you prefer you can save the file as a nuspec (An XML file) via the GUI using the File | Save Meta data as… option and then when you are ready you can use

NuGet Pack MyPackageName.1.0.0.nuspec

However you may well find that this fails because the assemblies added may need updating to the actual locations on your hard drive, so open the nuspc file and check the src paths. See the GUI made things easy.

Also when running the NuGet command line tool with Pack you may get a warning about the location of your assmebly, for example mine was by default stored in lib, but running the command line I find it is warning that I should move the file to a framework-specific folder.

For now we’ll not worry about this. Instead let’s see our nuget package being used.

Is our nuget package available

We can now open Visual Studio (if not already opened) and assuming you’ve added your local package as outlined previously.

  1. Go to Tools | NuGet Package Manager | Package Manager Console
  2. Your should see “Package source” in the console, select “Local Feed” or whatever name you gave for your local package location
  3. Type
    Get-Package -ListAvailable
    

    and you should see the package you created

Adding a local nuget package to our application

  1. Create yourself a new application to test your nuget package in
  2. Now we can add our package using the Package Manager Console, for example
    Install-Package MyPackage
    

    or from the References folder on your solution, right mouse click and select Manage NuGet Packages, from Online, select your Local Feed and select your packages as normal and install

Remote Packages

Check out the excellent post Hosting Your Own NuGet Feeds. The section on “Creating Remote Feeds” get’s you up and running in no time at all.

But let’s list the steps anyway

  1. Creating a empty web application
  2. Add the NuGet.Server package using NuGet
  3. Change the WebConfig packagesPath value to match the path to your packages
  4. Run the app – yes it’s that simple
  5. In Visual Studio 2012, select Tools | NuGet Package Manager | Package Manager Settings
  6. In Package Sources click the + button, give your remote source a name such as “My Package Feed” and set the Source to the URL of your server which you just created and ran

That’s is for now.

References

Nuspec Reference
Hosting Your Own NuGet Feeds
Using A GUI (Package Explorer) to build packages
Creating and Publishing a Package
Package Manager Console Powershell Reference