---
title: "Centralize your packages in .NET with NuGet"
description: "A feature that isn't really new but still didn't get my attention until recently is: *\"Central Package Management\"*. \nSo let's have a look at this in this blog post."
updated: 2024-10-07
tags: ["NuGet"]
canonical: "https://steven-giesel.com/blogPost/988c047a-914a-446d-bd28-4840643dcfb9/centralize-your-packages-in-net-with-nuget"
---

# Centralize your packages in .NET with NuGet

A feature that isn't really new but still didn't get my attention until recently is: *"Central Package Management"*. 
So let's have a look at this in this blog post.

## Central Package Management - What is it?
Central Package Management is a feature that allows you to define a central location for your NuGet packages.
Normally you will have many different packages in different assemblies.

So imagine this: Project A has the following packages:
```xml
<PackageRefernce Include="Microsoft.EntityFrameworkCore" Version="8.0.8" />
<PackageRefernce Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.8" />
```

And Project B has the following packages:
```xml
<PackageRefernce Include="Another.Awesome.Library" Version="1.0.0" />
<PackageRefernce Include="Microsoft.EntityFrameworkCore" Version="8.0.8" />
```

Now you have the same package in two different projects. If you want to update the package in both projects you have to do this manually in both projects (or use your tooling). But there is a different approach: You can simply put all the packages in a central location and reference them from there. 

That is where the `Directory.Packages.props` comes into play. Kind of like the `Directory.Build.props` file it would be in the root of your project and every project inherits from it. How does it look like? Here is an example:

```xml
<Project>
  <PropertyGroup>
    <ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
  </PropertyGroup>

  <ItemGroup>
    <PackageVersion Include="Another.Awesome.Library" Version="1.0.0" />
    <PackageVersion Include="Microsoft.EntityFrameworkCore" Version="8.0.8" />
    <PackageVersion Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.8" />
  </ItemGroup>
</Project>
```

The `ManagePackageVersionsCentrally` is a property that tells the project to manage the package versions centrally.
Keep in mind that we are using the `PackageVersion` iinstead of the `PackageReference`! Our projects now look like this:

```xml
<PackageReference Include="Microsoft.EntityFrameworkCore" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer"/>
```

And:
```xml
<PackageReference Include="Another.Awesome.Library" />
<PackageReference Include="Microsoft.EntityFrameworkCore"/>
```

So we dropped the version entirely. The version is now managed in the central (hence the name CPM) `Directory.Packages.props` file. If you want to update a package you simply update the version in the `Directory.Packages.props` file and all projects will use the new version.

## `Directory.Build.props` vs `Directory.Packages.props`
Hey, wait a minute! Isn't there already a `Directory.Build.props` file? Yes, there is. But the `Directory.Build.props` file is for build configurations and not for package management. And while you can do reference packages here, **every** project (if not specified otherwise) will inherit from the `Directory.Build.props` and include the listed packages.

So in the `Directory.Packages.props` you also can define your test packages, your production packages, etc. and reference them in your projects. But surely<sup>Don't call me Shirley</sup> you don't want testing reference in your production code.

But, there are some packages you do want to have everywhere! And while `Directory.Build.props` is a good place for that, the `Directory.Packages.props` offers you an alternative:

```xml
<Project>
	<PropertyGroup>
		<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
	</PropertyGroup>

	<ItemGroup Label="Code Analyzers">
		<GlobalPackageReference Include="SonarAnalyzer.CSharp" Version="9.32.0.97167" PrivateAssets="All" IncludeAssets="Runtime;Build;Native;contentFiles;Analyzers"/>
	</ItemGroup>
</Project>
```

Stuff like analyzers can be put in the `Directory.Packages.props` file and be available in all projects (thanks to the `GlobalPackageReference`).

## Not everything is sunny
There are some caveats with Central Package Management. One thing you can't use is floating versions. So stuff like:
```xml
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.*" />
```

That might not be a big dealbreaker for some, but it is something to keep in mind.

## Conclusion
Central Package Management is a great feature that allows you to manage your packages in a central location. If you want to read more, head over to the Microsoft site: https://learn.microsoft.com/en-us/nuget/consume-packages/central-package-management