|
1 | 1 | --- |
2 | | -title: Install and use a NuGet package with the dotnet CLI |
3 | | -description: Get a quick tutorial on how to use the dotnet CLI to install and use a NuGet package in a .NET project. |
| 2 | +title: Install and Use a NuGet Package with the dotnet CLI |
| 3 | +description: In this quickstart, find out how to use the dotnet command-line interface (CLI) to install and use a NuGet package in a .NET project. |
4 | 4 | author: JonDouglas |
5 | 5 | ms.author: jodou |
6 | | -ms.date: 03/03/2025 |
| 6 | +ms.date: 04/17/2026 |
7 | 7 | ms.topic: quickstart |
| 8 | +# customer intent: As a developer, I want to find out how to use the dotnet CLI to install and use a NuGet package in a .NET project so that I can take advantage of available code. |
8 | 9 | --- |
9 | 10 |
|
10 | 11 | # Quickstart: Install and use a package with the dotnet CLI |
11 | 12 |
|
12 | | -NuGet packages contain compiled binary code that developers make available for other developers to use in their projects. For more information, see [What is NuGet](../What-is-NuGet.md). This quickstart describes how to install the popular [Newtonsoft.Json](https://www.nuget.org/packages/Newtonsoft.Json) NuGet package into a .NET project by using the [dotnet add package](/dotnet/core/tools/dotnet-add-package) command. |
| 13 | +In this quickstart, you install the popular [`Newtonsoft.Json`](https://www.nuget.org/packages/Newtonsoft.Json) NuGet package into a .NET project. NuGet packages contain compiled binary code that developers make available for other developers to use in their projects. For more information, see [An introduction to NuGet](../what-is-nuget.md). |
13 | 14 |
|
14 | | -You refer to installed packages in code with a `using <namespace>` directive, where `<namespace>` is often the package name. You can then use the package's API in your project. |
| 15 | +To install the package, you use the [dotnet package add](/dotnet/core/tools/dotnet-package-add) command, which is part of the dotnet command-line interface (CLI). |
15 | 16 |
|
16 | 17 | > [!Tip] |
17 | | -> Browse [nuget.org/packages](https://nuget.org/packages) to find packages you can reuse in your own applications. You can search directly at [https://nuget.org](https://nuget.org/packages), or find and install packages from within Visual Studio. For more information, see [Find and evaluate NuGet packages for your project](../consume-packages/finding-and-choosing-packages.md). |
| 18 | +> Browse [nuget.org/packages](https://www.nuget.org/packages) to find packages you can reuse in your own applications. You can search directly at [https://nuget.org/packages](https://www.nuget.org/packages), or you can find and install packages from within Visual Studio. For more information, see [Find and evaluate NuGet packages for your project](../consume-packages/Finding-and-Choosing-Packages.md). |
18 | 19 |
|
19 | 20 | ## Prerequisites |
20 | 21 |
|
21 | | -- The [.NET SDK](https://www.microsoft.com/net/download), which provides the `dotnet` command-line tool. Starting in Visual Studio 2017, the dotnet CLI automatically installs with any .NET or .NET Core related workloads. |
| 22 | +The [.NET SDK](https://dotnet.microsoft.com/download), which provides the dotnet CLI. In Visual Studio, the dotnet CLI automatically installs with any .NET-related workloads. |
22 | 23 |
|
23 | 24 | ## Create a project |
24 | 25 |
|
25 | | -You can install NuGet packages into a .NET project. For this walkthrough, create a simple .NET console project by using the dotnet CLI, as follows: |
| 26 | +You can install NuGet packages into a .NET project. For this quickstart, take the following steps to create a basic .NET console project by using the dotnet CLI: |
26 | 27 |
|
27 | 28 | 1. Create a folder named *Nuget.Quickstart* for the project. |
28 | 29 |
|
29 | | -1. Open a command prompt and switch to the new folder. |
| 30 | +1. Open a command prompt window and go to the new folder. |
30 | 31 |
|
31 | 32 | 1. Create the project by using the following command: |
32 | 33 |
|
33 | | - ```dotnetcli |
34 | | - dotnet new console |
35 | | - ``` |
| 34 | + ```dotnetcli |
| 35 | + dotnet new console |
| 36 | + ``` |
36 | 37 |
|
37 | | -1. Use `dotnet run` to test the app. You should see the output `Hello, World!`. |
| 38 | +1. Use `dotnet run` to test the app. The command writes the following output to the screen: `Hello, World!`. |
38 | 39 |
|
39 | 40 | ## Add the Newtonsoft.Json NuGet package |
40 | 41 |
|
41 | | -1. Use the following command to install the `Newtonsoft.json` package: |
| 42 | +1. Use the following command to install the `Newtonsoft.Json` package: |
42 | 43 |
|
43 | | - ```dotnetcli |
44 | | - dotnet add package Newtonsoft.Json |
45 | | - ``` |
| 44 | + ```dotnetcli |
| 45 | + dotnet package add Newtonsoft.Json |
| 46 | + ``` |
46 | 47 |
|
47 | | -2. After the command completes, open the *Nuget.Quickstart.csproj* file in Visual Studio to see the added NuGet package reference: |
| 48 | + If you're using .NET 9 or earlier, use the verb-first form instead: |
48 | 49 |
|
49 | | - ```xml |
50 | | - <ItemGroup> |
51 | | - <PackageReference Include="Newtonsoft.Json" Version="13.0.1" /> |
52 | | - </ItemGroup> |
53 | | - ``` |
| 50 | + ```dotnetcli |
| 51 | + dotnet add package Newtonsoft.Json |
| 52 | + ``` |
| 53 | + |
| 54 | +1. After the command finishes, open the *Nuget.Quickstart.csproj* file in Visual Studio or in a text editor. Check for the added NuGet package reference: |
| 55 | + |
| 56 | + ```xml |
| 57 | + <ItemGroup> |
| 58 | + <PackageReference Include="Newtonsoft.Json" Version="13.0.4" /> |
| 59 | + </ItemGroup> |
| 60 | + ``` |
54 | 61 |
|
55 | 62 | ## Use the Newtonsoft.Json API in the app |
56 | 63 |
|
57 | | -1. In Visual Studio, open the *Program.cs* file and add the following line at the top of the file: |
| 64 | +In code, you refer to installed packages by using a `using <namespace>` directive, where `<namespace>` is often the package name. You can then use the package's API in your project. |
58 | 65 |
|
59 | | - ```cs |
60 | | - using Newtonsoft.Json; |
61 | | - ``` |
| 66 | +1. In Visual Studio or in a text editor, open the *Program.cs* file. Add the following line to the top of the file: |
| 67 | + |
| 68 | + ```cs |
| 69 | + using Newtonsoft.Json; |
| 70 | + ``` |
62 | 71 |
|
63 | 72 | 1. Add the following code to replace the `Console.WriteLine("Hello, World!");` statement: |
64 | 73 |
|
65 | | - ```cs |
66 | | - namespace Nuget.Quickstart |
67 | | - { |
68 | | - public class Account |
69 | | - { |
70 | | - public string? Name { get; set; } |
71 | | - public string? Email { get; set; } |
72 | | - public DateTime DOB { get; set; } |
73 | | - } |
74 | | - internal class Program |
75 | | - { |
76 | | - static void Main(string[] args) |
77 | | - { |
78 | | - Account account = new Account |
79 | | - { |
80 | | - Name = "John Doe", |
81 | | - |
82 | | - DOB = new DateTime(1980, 2, 20, 0, 0, 0, DateTimeKind.Utc), |
83 | | - }; |
84 | | - |
85 | | - string json = JsonConvert.SerializeObject(account, Formatting.Indented); |
86 | | - Console.WriteLine(json); |
87 | | - } |
88 | | - } |
89 | | - } |
90 | | - ``` |
91 | | -
|
92 | | -1. Save the file, then build and run the app by using the `dotnet run` command. The output is the JSON representation of the `Account` object in the code: |
93 | | -
|
94 | | - ```output |
95 | | - { |
96 | | - "Name": "John Doe", |
97 | | - |
98 | | - "DOB": "1980-02-20T00:00:00Z" |
99 | | - } |
100 | | - ``` |
101 | | -
|
102 | | -Congratulations on installing and using your first NuGet package! |
103 | | -
|
104 | | -## Related video |
| 74 | + ```cs |
| 75 | + namespace Nuget.Quickstart |
| 76 | + { |
| 77 | + public class Account |
| 78 | + { |
| 79 | + public string? Id { get; set; } |
| 80 | + public decimal Balance { get; set; } |
| 81 | + public DateTime Created { get; set; } |
| 82 | + } |
| 83 | + internal class Program |
| 84 | + { |
| 85 | + static void Main(string[] args) |
| 86 | + { |
| 87 | + Account account = new Account |
| 88 | + { |
| 89 | + Id = "A1bC2dE3fH4iJ5kL6mN7oP8qR9sT0u", |
| 90 | + Balance = 4389.21m, |
| 91 | + Created = new DateTime(2026, 4, 16, 0, 0, 0, DateTimeKind.Utc), |
| 92 | + }; |
| 93 | + |
| 94 | + string json = JsonConvert.SerializeObject(account, Formatting.Indented); |
| 95 | + Console.WriteLine(json); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + ``` |
| 100 | + |
| 101 | +1. Save the file, and then build and run the app by using the `dotnet run` command. The output is the JSON representation of the `Account` object in the code: |
| 102 | + |
| 103 | + ```output |
| 104 | + { |
| 105 | + "Id": "A1bC2dE3fH4iJ5kL6mN7oP8qR9sT0u", |
| 106 | + "Balance": 4389.21, |
| 107 | + "Created": "2026-04-16T00:00:00Z" |
| 108 | + } |
| 109 | + ``` |
| 110 | + |
| 111 | +## Related videos |
105 | 112 |
|
106 | 113 | > [!VIDEO https://learn-video.azurefd.net/vod/player?show=dotnet-package-management-with-nuget-for-beginners&ep=installing-a-nuget-package-using-the-dotnet-cli-nuget-for-beginners] |
107 | 114 |
|
108 | | -Find more NuGet videos on [Channel 9](/shows/NuGet-101/) and [YouTube](https://www.youtube.com/playlist?list=PLdo4fOcmZ0oVLvfkFk8O9h6v2Dcdh2bh_). |
109 | | -
|
110 | | -## Next steps |
| 115 | +For videos about using NuGet for package management, see [.NET Package Management with NuGet for Beginners](/shows/dotnet-package-management-with-nuget-for-beginners/) and [NuGet for Beginners](https://www.youtube.com/playlist?list=PLdo4fOcmZ0oVLvfkFk8O9h6v2Dcdh2bh_). |
111 | 116 |
|
112 | | -Learn more about installing and using NuGet packages with the dotnet CLI: |
| 117 | +## Related content |
113 | 118 |
|
114 | | -> [!div class="nextstepaction"] |
115 | | -> [Install and use packages by using the dotnet CLI](../consume-packages/install-use-packages-dotnet-cli.md) |
| 119 | +To find out more about installing and using NuGet packages by using the dotnet CLI, see the following articles: |
116 | 120 |
|
117 | | -- [Overview and workflow of package consumption](../consume-packages/overview-and-workflow.md) |
118 | | -- [Find and choose packages](../consume-packages/finding-and-choosing-packages.md) |
119 | | -- [Package references in project files](../consume-packages/package-references-in-project-files.md) |
| 121 | +- [Install and manage NuGet packages with the dotnet CLI](../consume-packages/install-use-packages-dotnet-cli.md) |
| 122 | +- [Package consumption workflow](../consume-packages/Overview-and-Workflow.md) |
| 123 | +- [Find and evaluate NuGet packages for your project](../consume-packages/Finding-and-Choosing-Packages.md) |
| 124 | +- [`PackageReference` in project files](../consume-packages/Package-References-in-Project-Files.md) |
0 commit comments