Skip to main content
Redhat Developers  Logo
  • Products

    Platforms

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat AI
      Red Hat AI
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • View All Red Hat Products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat Developer Hub
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat OpenShift Local
    • Red Hat Developer Sandbox

      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Secure Development & Architectures

      • Security
      • Secure coding
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • Product Documentation
    • API Catalog
    • Legacy Documentation
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

Introduction to NuGet with .NET Core on RHEL

August 16, 2017
Dave Mulford
Related topics:
Linux.NET
Related products:
Red Hat Enterprise Linux

Share:

    Introduction to NuGet with .NET Core

    NuGet is an open source package manager for the .NET Core ecosystem. For those familiar with Red Hat Enterprise Linux (RHEL), you can think of it as the “yum” for pulling libraries into your .NET Core project. Working with NuGet packages in .NET Core applications is accomplished primarily through your project’s .csproj file and the dotnet command-line interface.

    Repositories

    Just like RHEL, NuGet has its own repositories to get packages. By default, when the .NET Core runtime is installed, the nuget.org repository is added to your system. You can see this by viewing ~/.nuget/NuGet/NuGet.Config.

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <packageSources>
        <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
      </packageSources>
    </configuration>
    

    More information about the various NuGet.Config files and how they’re applied can be found in Microsoft’s documentation for Configuring NuGet Behavior.

    Finding a package

    Finding a package is an organic process that will undoubtedly change for you over time. In other words, you’ll find your own methods for doing this, so I’ll point you in the right direction and assume you’ll take it from there.

    I searched two places first for packages. Which one I use depends upon the information I know about the package already, i.e., whether I know the package name or just the name of a class within the package.

    If I already know the package’s name and just want to know the current version and which framework version it targets, I’ll use nuget.org to search by name. For example, the Newtonsoft.Json package page shows the current version is 10.0.3 and it targets .NETStandard 1.3.

    Sometimes I only know the name of a class or method. For example, I may have seen a code snippet on Stack Overflow that references the JsonConvert.DeserializeObject method. The nuget.org search won’t work very well here, so Microsoft created their Reverse Package Search tool. Here you can enter any class or method name, and the search results will link to the project on nuget.org.

    Be aware that any developer can create an account and upload packages to NuGet. This means that packages need to be vetted before being used in production systems. You can generally trust a package if the download count is high and the package is in active development. When in doubt, be sure to visit the project’s home page, if it exists.

    Adding packages to your project

    There are two ways of adding a package to your project:

    1. Using the dotnet add command
    2. Manually editing your .csproj file

    Using dotnet add is easiest because it will automatically run a package restore and download the package. To use the example above, let’s add the Newtonsoft.Json package to a project. In the project’s root directory (the directory containing your .csproj file), execute the following command.

    $ dotnet add package Newtonsoft.Json --version 10.0.3
    Microsoft (R) Build Engine version 15.1.1012.6693
    Copyright (C) Microsoft Corporation. All rights reserved.
    
      Writing /tmp/tmpgud8AG.tmp
    info : Adding PackageReference for package 'Newtonsoft.Json' into project '/home/redhat/myproject/myproject.csproj'.
    log  : Restoring packages for /home/redhat/myproject/myproject.csproj...
    info : Package 'Newtonsoft.Json' is compatible with all the specified frameworks in project '/home/redhat/myproject/myproject.csproj'.
    info : PackageReference for package 'Newtonsoft.Json' version '10.0.3' updated in file '/home/redhat/myproject/myproject.csproj'.
    

    To add the project manually, you’ll need to edit your project’s .csproj file and then run dotnet restore. Here is what the project file looks like after adding the Newtonsoft.Json package.

    <Project Sdk="Microsoft.NET.Sdk">
      <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>netcoreapp1.1</TargetFramework>
      </PropertyGroup>
      <ItemGroup>
        <PackageReference Include="Newtonsoft.Json" Version="10.0.3"/>
      </ItemGroup>
    </Project>
    

    Save the file and then run dotnet restore while in your project folder. This will check the package for compatibility and download the package for use. I find it much easier to use the first method.

    Local packages

    If you’re developing your own libraries for use across multiple projects, you’ll eventually want to reference those libraries. Since NuGet is the only way to add references to a .NET Core project, you have two options:

    1. You can create an account and upload your package to nuget.org.
    2. You can host an internal NuGet repository.

    At the time of writing this post, there is no NuGet hosting software that runs on Linux. So the only option is to use a directory to hold your NuGet packages. Note that this can be a directory on your local machine or a networked file share like NFS. Below I’ll focus on a local directory to keep things simple.

    First, create a directory for your packages. This can be anywhere your user has access, but I’ve chosen /usr/share/nuget.

    $ sudo mkdir -p /usr/share/nuget
    $ sudo chmod 644 /usr/share/nuget
    

    This post doesn’t cover how to create a NuGet package, but copy them into the directory you created above. Now you have to tell .NET about this directory. In your project folder, create a new NuGet.Config with the following content.

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <packageSources>
        <add key="mynuget" value="/usr/share/nuget" />
      </packageSources>
    </configuration>
    

    Now add a package to your project using your chosen method from the previous section.

    Conclusion

    I hope this post helps you in getting started with using NuGet for your .NET Core projects. Please comment below with any tips or suggestions!

    Last updated: August 14, 2017

    Recent Posts

    • A deep dive into Apache Kafka's KRaft protocol

    • Staying ahead of artificial intelligence threats

    • Strengthen privacy and security with encrypted DNS in RHEL

    • How to enable Ansible Lightspeed intelligent assistant

    • Why some agentic AI developers are moving code from Python to Rust

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2025 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue