In this article we'll examine setting up a NuGet/Chocolatey repository in your enterprise to distribute software. This will allow you to easily distribute development and software packages throughout your network.
NuGet? I don't need any more candy.NuGet started life NuPack (not to be confused with NuPack), an open source solution for managing .NET packages. Since then it has evolved into a mature platform with numerous interfaces including a Visual Studio plugin, command line, and Mono support. Chocolatey and PowerShellGet are built on that framework. Speaking of Chocolate...
Chocolatey? I told you already, no more candy.
Where NuGet was meant for .NET packages, Chocolatey,
which is built on the same infrastructure, is meant for machine (Windows)
packages. Think of it like apt
or yum for
windows. PowerShell has also already shipped a preview of PowerShell
OneGet which can use Chocolatey repositories.
But There are Already Repositories for These and I'm Hungry! Why Would I
Make My Own?
Yup! There are great public repositories for Chocolatey
and NuGet, but those are
geared at freely available public software that wouldn't be built for an
explicit purpose. By hosting your own you can host custom .NET packages
specific to your business unit or even package commercially available
software for distribution with Chocolatey provided your licensing is up to
There are several options to get going that vary in terms of hosting location, ease of installation, and scalability. Some of the more popular options include:
- NuGet Server : A basic server that runs on-premise and is easy to setup. Doesn't have granular security features and will only scale so far before it slows down.
- NuGet Gallery: More complex NuGet server package that includes advanced security features and will scale for larger implementations (this is what the main public NuGet repo uses).
- MyGet: A commercial NuGet repo service hosted in Azure. Has a limited free tier and reasonably priced paid tiers. Worth consideration if you don't want to host your own infrastructure.
The assumption with this article is that you're hosting your first NuGet/Chocolately repo for the purposes of your enterprise or team use. Since the NuGet server includes most needed functionality for that purpose and can be replaced with the NuGet Gallery as you grow, we'll set up a NuGet Server(the first option) in this article.Let's Get Started!
- We'll be setting up on a Windows Server 2012 R2. This will work on Windows Server 2008 and up. I'm assuming you have one set up and ready to go.
- You will need Visual Studio Express 2010 or newer on your workstation. (Preferably not on the server)
- You will need Admin rights on both the server and your workstation.
- In enterprise environments you often have to make due with the resources you have available, so we'll be setting up the repo as a virtual application in IIS rather than its own site so that it can share port 80 for the sake of simplicity.
IIS Setup on Server
We'll walk through installing the minimum IIS requirements to run the NuGet Server package. Everything here could be very easily done with PowerShell but we'll use the GUI to make for a more visual tutorial.
- On the server where you will host the application, start the "Add Roles and Features Wizard"
- Click "Next" until you advance to the "Server Roles" section. If you're executing remotely make sure you select the correct server.
- Of the Roles listed, select "Web Server (IIS)" and select "Add Features" when prompted. Click "Next".
- On the "Features" page, expand ".NET Framework 4.5 Features" and ensure ".NET Framework 4.5" and "ASP.NET 4.5" are checked. Click "Next".
- Click "Next" to advance to the "Role Services" section under "Web Server Role (IIS)" and select the following (only the most granular required, not the headings):
- Web Server
- Common HTTP Features
- Default Document
- Static Content
- Health and Diagnostics
- HTTP Logging
- Static Content Compression
- Request Filtering
- Application Development
- .NET Extensibility 4.5
- ASP.NET 4.5
- ISAPI Extensions
- ISAPI Filters
- Management Tools
- IIS Management Console
IIS Setup on ServerNow we'll set up the site that the NuGet Server will be served from. As mentioned earlier, I will walk you through setting it up as a virtual application off of the default web site. This configuration would allow you to share port 80 with an existing site as well as show you how to configure this application below the root, which does need a bit of special consideration worth mentioning.
- Create the directory structure for your site. I always put my IIS sites on a non-system drive with the permissions locked down. In this example, I'll be using D:\Sites\NuGetRepo .
- Create the directory for the NuGet/Chocolately package repo. We'll configure this below. This directory uses a different permissions structure and could potentially be shared out over your LAN, so it may be beneficial to place this separate from the site. In my example I'll be using D:\NuGetRepo .
- (Optional/Best Practice) At the D:\Sites and D:\NuGetRepo levels disable inheritance ensure only Administrators and SYSTEM have write access. Do not allow any other access at this time, we'll get to that below.
- On the server to host NuGet Server, open the IIS management tool.
- Right click the Default Web Site (note this could be any web site) and select "Add Application..." (A virtual directory will not work!)
- Set the alias to "NuGet", leave the "Application Pool" on "DefaultApplicationPool" (again, you could change this if desired) and set the physical path to what you created for the site. For our example we're using "D:\Sites\NuGetRepo" .
- The default out-of-box settings should work for the site, but in case the Default Web Site settings have been changed you may want to refresh your view and ensure IIS authentication is set to "Anonymous". If desired, change the logging location as well (D:\logfiles\IIS\NuGetRepo for example).
Setting File System Permissions on IIS Server
For clients to access the site and repo successfully we need to set file system level permissions. If you have used different directory names above substitute them here accordingly.
Note: We're assuming you have administrative access to this server from your Workstation as well to deploy the code in the steps below. If not, you'll need to grant whoever will be deploying the site access to the site folder. If you are admin, don't worry about it.
- The directory containing the website needs to be read by the AppPool account and the Anon user account. Right click D:\Sites\NuGetRepo and select "Properties".
- Click "Security", "Edit", and then "Add". Change the "Location" to the local system name.
- Give the default web site application pool virtual service account and anonymous account permissions by typing "IIS APPPOOL\DefaultAppPool;IUSR", clicking "Check Names" and then "OK". Again, if you have elected to use a different site/pool/acct you will need to take that into account. This should resolve to two accounts, "DefaultAppPool" and IUSR".
- Give each of the added users "Read & Execute", "List folder contents", and "Read" permissions and then click "OK".
- The directory containing the actual repo only needs to be read by the AppPool account. Right click D:\NuGetRepo and select "Properties".
- Click "Security", "Edit", and then "Add".
Change the "Location" to the local system
- Give the default web site application pool virtual service account
permissions by typing "IIS APPPOOL\DefaultAppPool",
clicking "Check Names" and then "OK".
Again, if you have elected to use a different site/pool/acct you will
need to take that into account. This should resolve to one account,
- Grant it "Read & Execute", "List folder contents", and "Read" permissions and then click "OK".
NuGet Server Config on Workstation
Now we'll grab the NuGet Server package and configure it accordingly. Note some of these options will vary slightly depending on which version of Visual Studio you are using. I'm using 2012 Premium but everything is possible in 2010 Express and up.
Note: We are assuming your IIS server is accessible to you and has file sharing turned on to push the site. If you are unable to get to the filesystem of the server from this machine you will need to use a different deployment mechanism when we get to that step.
- On your workstation, open Visual Studio and start a new Project by selecting "File"->"New"->"Project"
- Navigate to "Installed"->"Templates"->"Visual C#"->"Web" and select "ASP.NET Empty Web Application"
- Right click on your newly created application under the solution and select "Manage NuGet Packages"
- Assuming the defaults of the nuget.org feed and "Stable Only" are selected, type "nuget.server" in the search box and hit Enter.
- Select the "NuGet.Server" package and click "Install". This will install the NuGet server package and any dependencies. Accept license agreements associated with the other packages to continue and then close the package management window.
- The only thing we need to customize is the web.config file for our installation. In the Solution Explorer click "Web.config" under the web application. Note: This file is also where you can control API Key behavior, but that is outside the scope of this article.
- Look for the add key="packagesPath" entry in the web.config file under the "<appSettings>" heading. We need to set this to the location of our repository. Change <add key="packagesPath" value=""/> to <add key="packagesPath" value="D:\NuGetRepo"/> (or other directory if appropriate). Note that there is no trailing slash. Save your project.
- Now we need to publish. Click "Build"->"Publish WebApplication..."
- If you already have a working publishing profile for the web server, select it and skip to step 12. Otherwise, select <new profile> from the drop-down box, enter a name, and click "OK".
- Change "Publish method" to "File System" and enter the full path to the web server site location, I.E. "\\<server>\d$\sites\NuGetRepo\" . Click "Next".
- Accept the default publishing settings and click "Next".
- Review the settings and click "Publish".
- Review the Output window to ensure there weren't any errors.
- Test your NuGet Server by navigating to http://<servername>/NuGet/ . If you encounter errors be sure to browse to it locally on the server to get the full error information.
That's it! Now all you need to do is configure the source in your clients, make packages, and enjoy! For instructions on those steps see below, and stay tuned for more. Thanks for reading!
Creating NuGet/Chocolatey PackagesChocolatey Docs: Create Packages Quick Start
NuGet Docs: Nuspec Reference
Chocolatey Docs: Chocolately Templates
NuGet Docs: Creating and Publishing a Package
Scott Hanselman: Creating a NuGet Package in 7 Easy Steps!
Chocolatey Docs: Creating Chocolatey Packages
Hong Xu: Create and Publish Chocolatey Packages
NuGet Docs: Configuration File and Source Code Transformations
Configuring SourcesChocolatey Docs: Source command
NuGet Docs: Visual Studio Package Sources
References!NuGet Docs: Hosting Your Own Feeds
Scott Hanselman: Is the Windows User Ready for Apt-Get?
MBrownNYC: Create Your Own NuGet Server to Serve Packages for Chocolatey
NuGet Docs: An Overview of the NuGet Ecosystem