A Gentle Introduction to NuGet
- by Joe Mayo
Not too long ago, Microsoft released, NuGet, an automated package manager for Visual Studio. NuGet makes it easy to download and install assemblies, and their references, into a Visual Studio project. These assemblies, which I loosely refer to as packages, are often open source, and include projects such as LINQ to Twitter. In this post, I'll explain how to get started in using NuGet with your projects to include: installng NuGet, installing/uninstalling LINQ to Twitter via console command, and installing/uninstalling LINQ to Twitter via graphical reference menu.
Installing NuGet
The first step you'll need to take is to install NuGet. Visit the NuGet site, at http://nuget.org/, click on the Install NuGet button, and download the NuGet.Tools.vsix installation file, shown below.
Each browser is different (i.e. FireFox, Chrome, IE, etc), so you might see options to run right away, save to a location, or access to the file through the browser's download manager. Regardless of how you receive the NuGet installer, execute the downloaded NuGet.Tools.vsix to install Nuget into visual Studio.
The NuGet Footprint
When you open visual Studio, observe that there is a new menu option on the Tools menu, titled Library Package Manager; This is where you use NuGet. There are two menu options, from the Library Package Manager Menu that you can use: Package Manager Console and Package Manager Settings.
I won't discuss Package Manager Settings in this post, except to give you a general idea that, as one of a set of capabilities, it manages the path to the NuGet server, which is already set for you.
Another menu, added by the NuGet installer, is Add Library Package Reference, found by opening the context menu for either a Solution Explorer project or a project's References folder or via the Project menu. I'll discuss how to use this later in the post.
The following discussion is concerned with the other menu option, Package Manager Console, which allows you to manage NuGet packages.
Gettng a NuGet Package
Selecting Tools -> Library Package Manager -> Package Manager Console opens the Package Manager Console. As you can see, below, the Package Manager Console is text-based and you'll need to type in commands to work with packages.
In this post, I'll explain how to use the Package Manager Console to install LINQ to Twitter, but there are many more commands, explained in the NuGet Package Manager Console Commands documentation. To install LINQ to Twitter, open your current project where you want LINQ to Twitter installed, and type the following at the PM> prompt:
Install-Package linqtotwitter
If all works well, you'll receive a confirmation message, similar to the following, after a brief pause:
Successfully installed 'linqtotwitter 2.0.20'.
Successfully added 'linqtotwitter 2.0.20' to NuGetInstall.
Also, observe that a reference to the LinqToTwitter.dll assembly was added to your current project.
Uninstalling a NuGet Package
I won't be so bold as to assume that you would only want to use LINQ to Twitter because there are other Twitter libraries available; I recommend Twitterizer if you don't care for LINQ to Twitter. So, you might want to use the following command at the PM> prompt to remove LINQ to Twitter from your project:
Uninstall-Package linqtotwitter
After a brief pause, you'll see a confirmation message similar to the following:
Successfully removed 'linqtotwitter 2.0.20' from NuGetInstall.
Also, observe that the LinqToTwitter.dll assembly no longer appears in your project references list.
Sometimes using the Package Manager Console is required for more sophisticated scenarios. However, LINQ to Twitter doesn't have any dependencies and is a very simple install, so you can use another method of installing graphically, which I'll show you next.
Graphical Installations
As explained earlier, clicking Add Library Package Reference, from the context menu for either a Solution Explorer project or a project's References folder or via the Project menu opens the Add Library Package Reference window. This window will allow you to add a reference a NuGet package in your project.
To the left of the window are a few accordian folders to help you find packages that are either on-line or already installed. Just like the previous section, I'll assume you are installing LINQ to Twitter for the first time, so you would select the Online folder and click All.
After waiting for package descriptions to download, you'll notice that there are too many to scroll through in a short period of time, over 900 as I write this. Therefore, use the search box located at the top right corner of the window and type LINQ to Twitter as I've done in the previous figure. You'll see LINQ to Twitter appear in the list.
Click the Install button on the LINQ to Twitter entry. If the installation was successful, you'll see a message box display and disappear quickly (or maybe not if your machine is very fast or you blink at that moment). Then you'll see a reference to the LinqToTwitter.dll assembly in your project's references list.
Note: While running this demo, I ran into an issue where VS had created a file lock on an installation folder without releasing it, causing an error with "packagename already exists. Skipping..." and then an error describing that it couldn't write to a destination folder. I resolved the problem by closing and reopening VS.
If you open the Add a Library Package Reference window again, you'll see LINQ to Twitter listed in the Recent packages folder.
Summary
You can install NuGet via the on-line home page with a click of a button. Nuget provides two ways to work with packages, via console or graphical window. While the graphical window is easiest, the console window is more powerful. You can now quickly add project references to many available packages via the NuGet service.
Joe