Install and manage NuGet packages in Visual Studio for Mac
Visual Studio for Mac is scheduled for retirement on August 31, 2024 in accordance with Microsoft’s Modern Lifecycle Policy. While you can continue to work with Visual Studio for Mac, there are several other options for developers on Mac such as the preview version of the new C# Dev Kit extension for VS Code.
The NuGet Package Manager UI in Visual Studio for Mac allows you to easily install, uninstall, and update NuGet packages in projects and solutions. You can search for and add packages to your .NET Core, ASP.NET Core, and Xamarin projects.
This article describes how to include a NuGet package in a project and demonstrates the tool chain that makes the process seamless.
For an intro to using NuGet in Visual Studio for Mac, see Quickstart: Install and use a package in Visual Studio for Mac
Find and install a Package
With a project open in Visual Studio for Mac, right-click on the Dependencies folder (Packages folder if using a Xamarin project) in the Solution Window and select Manage NuGet Packages....
The Manage NuGet Packages dialog comes up. Ensure that the Package source drop-down in the bottom left corner of the dialog is set to
nuget.org, so that you're searching the central NuGet package repository.
Use the Search box in the top-right corner to find a specific package, for example
EntityFramework. When you have found a package that you wish to use, select it and select the Add Package button to begin installation.
Once the package has been downloaded, it'll be added to your project. The solution will change depending on the type of project you're editing:
- The References node will contain a list of all the assemblies that are part of a NuGet package.
- The Packages node displays each NuGet package that you've downloaded. You can update or remove a package from this list.
.NET Core Projects
- The Dependencies > NuGet node displays each NuGet package that you've downloaded. You can update or remove a package from this list.
Using NuGet Packages
Once the NuGet package has been added and the project references updated, you can program against the APIs as you would with any project reference.
Ensure that you add any required
using directives to the top of your file:
Package updates can be done either all at once, by right-clicking on the Dependencies node (Packages node for Xamarin projects), or individually on each package. When a new version of a NuGet package is available, an update icon is shown .
Right-click on Dependencies to access the context menu and choose Update to update all packages:
- Manage NuGet Packages - Opens the window to add more packages to the project.
- Update - Checks the source server for each package and downloads any newer versions.
- Restore - Downloads any missing packages (without updating existing packages to newer versions).
Update and Restore options are also available at the Solution level, and affect all the projects in the solution.
Updating to pre-release versions of packages
To update to a newer pre-release version of a package, you can right-click on Dependencies to open the context menu and choose the Manage NuGet Packages... menu.
Check the Include prereleases checkbox at the bottom of the dialog.
Finally, from the Updates tab of the dialog, select the package you wish to update and choose the new pre-release version from the New Version drop-down and select Update Package.
Locating outdated packages
From the Solution Window, you can view what version of a package is currently installed. Right-click on the package to update.
You'll also see a notification next to the package name when a new version of a package is available. You can decide if you may want to update it.
In the menu shown, you have two options:
- Update - Checks the source server and downloads a newer version (if it exists).
- Remove - Removes the package from this project and removes the relevant assemblies from the project's References.
Manage packages for the solution
Managing packages for a solution is a convenient means to work with multiple projects simultaneously.
Right-click the solution and select Manage NuGet Packages...:
When you manage packages for the solution, the UI lets you select the projects that are affected by the operations:
When you work in a solution with multiple projects, ensure that anywhere you use the same NuGet package in each project, you're also using the same version number of that package. Visual Studio for Mac helps make it easier by providing a Consolidate tab in the Package Manager UI when you choose to manage packages for a solution. Using the Consolidate tab, you can easily see where packages with distinct version numbers are used by different projects in the solution:
In this example, the NuGetDemo project is using Microsoft.EntityFrameworkCore 3.1.23, whereas NuGetDemo.Shared is using Microsoft.EntityFrameworkCore 5.0.2. To consolidate package versions, follow these steps:
- Select the projects to update in the project list.
- Select the version to use in all those projects in the New Version list, such as Microsoft.EntityFrameworkCore 6.0.3.
- Select the Consolidate Package button.
The Package Manager installs the selected package version into all selected projects, after which the package no longer appears on the Consolidate tab.
Adding Package Sources
Packages available for installation are initially retrieved from nuget.org. However, you can add other package locations to Visual Studio for Mac. It's useful for testing your own NuGet packages under development, or to use a private NuGet server inside your company or organization.
In Visual Studio for Mac, navigate to Visual Studio > Preferences > NuGet > Sources to view and edit the list of package sources. Sources can be a remote server (specified by a URL) or a local directory.
Select Add to set up a new source. Enter a friendly Name and Location (the URL or file path) to the package source. If the source is a secure web server, enter the Username and Password as well, otherwise leave these entries blank:
Different sources can then be selected when searching for packages:
The NuGet documentation discusses using NuGet without committing packages to source control. If you prefer not to store binaries and unused information in source control, you can configure Visual Studio for Mac to automatically restore packages from the server. When a developer retrieves the project from source control for the first time, Visual Studio for Mac will automatically download and install the required packages.
Refer to your particular source control documentation for details on how to exclude the
packages directory from being tracked.