Skip to content

dotnet

close up photo of matrix background

Creating and publishing a NuGet package on Linux

Suppose you have a couple of .dll files that were built on a TeamCity server and you want to bundle them into a NuGet package and publish them on nuget.org, how would you do that if you were a Linux user? Is that even possible??? Let’s find out!

  1. Preparation

    First things first, lets create a clean working environment:

    mkdir -p ~/repos/qa-nugetlinux
    cd qa-nugetlinux
    git init
    gi linux,vagrant >> .gitignore
    git add .gitignore
    git commit -m ".gitignore created by https://www.gitignore.io/api/linux,vagrant"
    vagrant init --minimal ubuntu/yakkety64
    git add Vagrantfile
    git commit -m "Add Vagrantfile"
    vagrant up --provider virtualbox

    This creates a Vagrant box where I will conduct my experiments. Let’s dive in and make sure that everything is up-to-date inside:

    vagrant ssh
    sudo apt-get update
    sudo apt-get -y dist-upgrade
    sudo apt-get -y autoremove
     
     
     
  2. Installing NuGet

    Now let’s get this party going!

    cd ~/vagrant
    wget https://dist.nuget.org/win-x86-commandline/latest/nuget.exe
    chmod +x nuget.exe
    ./nuget.exe
    -bash: ./nuget.exe: cannot execute binary file: Exec format error

    Computer says no…
    Why not?

    file nuget.exe
    nuget.exe: PE32 executable (console) Intel 80386 Mono/.Net assembly, for MS Windows

    Oops, silly me. It’s a Mono executable.

    mono nuget.exe
    The program 'mono' is currently not installed. You can install it by typing:
    sudo apt install mono-runtime

    Thank you for that helpful message, Ubuntu!

    sudo apt-get -y install mono-runtime

    16 MiB later, I try again:

    mono nuget.exe
    Unhandled Exception:
    System.IO.FileNotFoundException: Could not load file or assembly 'System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies.
    File name: 'System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
      at NuGet.CommandLine.Program.Main (System.String[] args)  in :0 
    [ERROR] FATAL UNHANDLED EXCEPTION: System.IO.FileNotFoundException: Could not load file or assembly 'System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies.
    File name: 'System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
      at NuGet.CommandLine.Program.Main (System.String[] args)  in :0

    System.Core is missing? OK let’s install that.

    sudo apt-get -y install libmono-system-*

    And try again:

    mono nuget.exe
    Could not load file or assembly or one of its dependencies.

    Sigh. Ok, let’s use a cannon to shoot a mosquito:

    sudo apt-get -y install mono-complete

    Does it work now?

    mono nuget.exe
    NuGet Version: 3.4.4.1321
    usage: NuGet  [args] [options] 
    Type 'NuGet help ' for help on a specific command.
    Available commands:
     add         Adds the given package to a hierarchical source. http sources are not supported. For more info, goto https://docs.nuget.org/consume/command-line-reference#add-command.
     config      Gets or sets NuGet config values.
     delete      Deletes a package from the server.
     help (?)    Displays general help information and help information about other commands.
     init        Adds all the packages from the  to the hierarchical . http feeds are not supported. For more info, goto https://docs.nuget.org/consume/command-line-reference#init-command.
     install     Installs a package using the specified sources. If no sources are specified, all sources defined in the NuGet configuration file are used. If the configuration file specifies no sources, uses the default NuGet feed.
     list        Displays a list of packages from a given source. If no sources are specified, all sources defined in %AppData%NuGetNuGet.config are used. If NuGet.config specifies no sources, uses the default NuGet feed.
     locals      Clears or lists local NuGet resources such as http requests cache, packages cache or machine-wide global packages folder.
     pack        Creates a NuGet package based on the specified nuspec or project file.
     push        Pushes a package to the server and publishes it.
                 NuGet's default configuration is obtained by loading %AppData%NuGetNuGet.config, then loading any nuget.config or .nugetnuget.config starting from root of drive and ending in current directory.
     restore     Restores NuGet packages.
     setApiKey   Saves an API key for a given server URL. When no URL is provided API key is saved for the NuGet gallery.
     sources     Provides the ability to manage list of sources located in %AppData%NuGetNuGet.config
     spec        Generates a nuspec for a new package. If this command is run in the same folder as a project file (.csproj, .vbproj, .fsproj), it will create a tokenized nuspec file.
     update      Update packages to latest available versions. This command also updates NuGet.exe itself.
    For more information, visit http://docs.nuget.org/docs/reference/command-line-reference

    And there was much rejoicing (Monty Python And The Holy Grail)
  3. Creating the .nuspec file

    1. Trying the easy way, and failing miserably

      According to some Idiot’s Guide to Creating and Publishing a NuGet package I found, I should be able to create a .nuspec file by running NuGet in the same directory as a .csproj file. Let’s try that:

      cd ~/vagrant/itextcore-dotnet/itext/itext.barcodes/
      mono ~/vagrant/nuget.exe pack itext.barcodes.csproj -verbosity detailed
      Attempting to build package from 'itext.barcodes.csproj'.
      MSBuild auto-detection: using msbuild version '4.0' from '/usr/lib/mono/4.5'. Use option -MSBuildVersion to force nuget to use a specific version of MSBuild.
      System.NotImplementedException: The method or operation is not implemented.
        at (wrapper dynamic-method) System.Object:CallSite.Target (System.Runtime.CompilerServices.Closure,System.Runtime.CompilerServices.CallSite,object)
        at System.Dynamic.UpdateDelegates.UpdateAndExecuteVoid1[T0] (System.Runtime.CompilerServices.CallSite site, System.Dynamic.T0 arg0)  in :0 
        at NuGet.CommandLine.ProjectFactory.ResolveTargetPath ()  in :0 
        at NuGet.CommandLine.ProjectFactory.BuildProject ()  in :0 
        at NuGet.CommandLine.ProjectFactory.CreateBuilder (System.String basePath)  in :0 
        at NuGet.CommandLine.PackCommand.BuildFromProjectFile (System.String path)  in :0 
        at NuGet.CommandLine.PackCommand.BuildPackage (System.String path)  in :0 
        at NuGet.CommandLine.PackCommand.ExecuteCommand ()  in :0 
        at NuGet.CommandLine.Command.ExecuteCommandAsync ()  in :0 
        at NuGet.CommandLine.Command.Execute ()  in :0 
        at NuGet.CommandLine.Program.MainCore (System.String workingDirectory, System.String[] args)  in :0

      That seems like a big ball of NOPE to me… According to this GitHub comment from a NuGet member, this is to be expected.

    2. Hand Crank the .nuspec File

      So it’s going to be the hard way.

      <TO BE CONTINUED>
      This blog post was a draft, and I decided to publish whatever I had already, and if anyone is ever interested, I may or may not finish it. ¯_(ツ)_/¯