# NuGet CLI Usage

Use NuGet-compatible command-line clients to search for, install, restore, and publish packages through Nexus Repository. Configure the Nexus Repository source before running these commands. For instructions, see [Configure NuGet With Nexus](https://help.sonatype.com/en/configure-nuget-with-nexus.html "Configure NuGet With Nexus").

## Install a NuGet Package

Use the following .NET CLI syntax to add a package to a project:

```
dotnet add package <package-id> \

--version <version> \

--source <repository-url>
```

Where,

- `<package-id>` - Your NuGet package ID

- `<version>` - Package version.

- `<repository-url>` - Your Nexus Repository URL

The following example installs `Newtonsoft.Json` from a NuGet group:

```
dotnet add package Newtonsoft.Json \

--version 13.0.3 \

--source https://example.nexus.com/repository/nuget-group/index.json
```

## Restore NuGet Packages

Use the following syntax to restore the packages required by a project:

```
dotnet restore --source <repository-url>
```

Where `<repository-url>` is the NuGet v3 service-index URL or another configured NuGet source.

The following example restores packages through a NuGet group:

```
dotnet restore --source https://nexus.example.com/repository/nuget-group/index.json
```

## Publish NuGet Packages

Publish packages directly to a NuGet hosted repository:

```
dotnet nuget push <package-file> \

--source <hosted-repository-url> \

--api-key <api-key>
```

Where,

- `<package-file>` - Path to the `.nupkg` file

- `<hosted-repository-url>` - NuGet hosted repository URL

- `<api-key>` - Your NuGet API key

The following example publishes a package to a hosted repository:

```
dotnet nuget push ./Example.Package.1.0.0.nupkg \

--source https://nexus.example.com/repository/nuget-hosted/index.json \
--api-key "$NUGET_API_KEY"
```

**Note**

Nexus Repository does not support the `--skip-duplicate` option. Nexus Repository returns an HTTP `400` response when the package already exists, while the option expects an HTTP `409` response.

## Search for Chocolatey Packages

Use the following syntax to search a configured Chocolatey source:

```
choco search <search-term> --source="<source-name-or-url>"
```

Where,

- `<search-term>` - The full or partial package name

- `<source-name-or-url>` - Configured Chocolatey source name or a Nexus Repository URL

The following example searches the `NexusChocolatey` source:

```
choco search git --source="NexusChocolatey"
```

**Note**

Nexus Repository caps the total search count at 10,000 unique packages to prevent unbounded iteration in very large repositories. Administrators can adjust this cap in `nexus.properties` by setting `nexus.nuget.v3.search.maxTotalHits`.

## Install a Chocolatey Package

Use the following syntax to install a package:

```
choco install <package-id> \
--version <version> \
--source="<source-name-or-url>"
```

Where,

- `<package-id>` - Chocolatey package ID

- `<version>` - Package version

- `<source-name-or-url>` - Configured source name or Nexus Repository URL

The following example installs a package from a configured Nexus Repository source:

```
choco install git \
--version 2.47.1 \
--source="NexusChocolatey"
```

**Note**

When dealing with mixed v2 and v3 group repositories, Chocolatey search works across the full group. But `choco install` only succeeds for packages reachable via v2 members. A package found via `choco search` that is exclusively cached by a v3 proxy member will fail to install.

## Publish a Package With Chocolatey

API Key needs to be associated with the hosted repository before publishing `choco` packages. Use the following syntax to publish a Chocolatey package:

```
choco push <package-file> --source="<hosted-repository-url>"
```

Where,

- `<package-file>` - Path to the `.nupkg` file

- `<hosted-repository-url>` - NuGet hosted repository URL associated with the configured API key.

Example:

```
choco push .\Example.Package.1.0.0.nupkg \
--source="https://nexus.example.com/repository/nuget-hosted/"
```

## Upload Symbol Packages

**Register Nexus Hosted Repository**

Before pushing packages, register the Nexus hosted repository as a named source in your `NuGet.Config` using dotnet nuget add source.

For NuGet V3 hosted repository:

```
dotnet nuget add source "http://<your-nexus-server>:8081/repository/nuget-hosted/index.json" \
  --name NexusHosted \
  --username admin \
  --password admin123 \
  --store-password-in-clear-text \
  --configfile nuget.config
```

For NuGet V2 hosted repository:

```
dotnet nuget add source "http://<your-nexus-server>:8081/repository/nuget-hosted/" \
  --name NexusHosted \
  --username admin \
  --password admin123 \
  --store-password-in-clear-text \
  --configfile nuget.config
```

**Note**

`--store-password-in-clear-text` is required on non-Windows platforms and in CI environments where the NuGet credential store is not available. The `--configfile` flag writes the source to the specified file; omit it to update the global `NuGet.Config` at `~/.nuget/NuGet/NuGet.Config`.

**Upload Symbol Packages**

NuGet V3 hosted repositories accepts `.snupkg` uploads alongside regular `.nupkg` uploads. The upload path is identical. `dotnet nuget push` uploads both `.nupkg` and `.snupkg` files if they exist side by side.

```
dotnet nuget push MyLibrary.1.0.0.nupkg -s NexusHosted
dotnet nuget push MyLibrary.1.0.0.snupkg -s NexusHosted
```

Both `.snupkg` downloads and SymSrv-style symbol fetches are served from a hosted repository. The `.snupkg` download URL requires standard repository `read` permissions. The SymSrv URL (`/symbols/...`) is served anonymously by default, since debuggers do not send authentication.

NuGet V2 hosted repositories use the legacy `.symbols.nupkg` format. Specify the symbol destination explicitly with the `--symbol-source` (`-ss`) flag. Without the `-ss` flag, the NuGet client does not know where to send the symbol package on a V2 feed.

For V2, NuGet hosted repository accepts `.symbols.nupkg` uploads alongside regular `.nupkg` uploads. The upload path is identical. `dotnet nuget push` uploads both `.nupkg` and `.symbols.nupkg` files if they exist side by side.

```
dotnet nuget push MyLibrary.1.0.0.nupkg -s NexusHosted  -ss NexusHosted
dotnet nuget push MyLibrary.1.0.0.symbols.nupkg -s NexusHosted -ss NexusHosted
```

**Note**

- `.symbols.nupkg` is the legacy V2 symbol package format. For V3 hosted repositories, use `.snupkg` instead.

- If you upload a `.snupkg` without a corresponding `.nupkg` in the same repository, Nexus Repository still stores and indexes it, but the resulting assets are not linked to a parent component. Such components are stored and downloadable but are not indexed for keyword or checksum search. To search for a symbol package by MD5/SHA-1/etc., ensure the matching `.nupkg` is present in the same repository.
