Feb 23, 2013

Using NuGet standalone command-line

In case you are running Visual Studio Express (prior to 2012), MonoDevelop, SharpDevelop or anything other than the paid version of Visual Studio, you cannot use the integrated NuGet Package Manager. There is a way to use NuGet command-line version to still achieve what you need, and it also works on Linux with Mono. This way you can use it in the manner of traditional command-line package managers. At the time this article was written, current NuGet command-line version was 2.2.

Downloading and using NuGet command-line

Download the "NuGet.exe Command Line" (not the bootstrapper) from the NuGet releases page. Run it once and it will update itself. If you are on Linux, check running NuGet command-line on Linux.

Note: In order to use NuGet from the command-line in any directory, it needs to be on one of the paths defined by your PATH system variable.
NuGet Basic Usage
# Install the specified package (latest, if version is not specified)
# Use '-version' to specify version
nuget install nunit
nuget install nunit -version 2.6.1

# Install all packages defined in the 'packages.config' file
# Use '-o' to specify output directory (relative to the working directory)
nuget install
nuget install -o "vendor"

# Display all available packages
nuget list

# Update all packages and NuGet to the latest version.
# Use '-self' to only update NuGet
nuget update
nuget update -self

NuGet Configuration Files

In order to automate and simplify the installation of your dependencies, you can create two configuration files for NuGet inside your project directory. Create nuget.config (optional), to specify the output directory. NuGet will search for this file from the root of your drive to the current working directory. If this file is missing, packages will be installed in the current working directory. Visit NuGet configuration file page for more info.

nuget.config (optional)
<?xml version="1.0" encoding="utf-8"?>
    <!-- install packages to 'vendor' subdirectory -->
    <add key="repositoryPath" value="vendor" />

Create packages.config to specify the list of packages your project requires. With this file in your repository, everyone will be able to checkout the project and get the required dependencies.

<?xml version="1.0" encoding="utf-8"?>
  <package id="NUnit" version="2.6.2" />
  <package id="NSubstitute" version="" />
  <package id="log4net" version="2.0.0" />

Proposed project folder structure:

  • FooProject\
    • FooProject.sln
    • Program.cs
    • nuget.config
    • packages.config
    • vendor\
    • ...

With nuget.config and packages.config in the project directory, it is enough to run nuget install, and all dependencies will be downloaded and installed.

comments powered by Disqus