65

I need to install NuGet on Linux based machine.When am using the following command in Windows machine it works fine.

nuget install packages.config

But I am unable to do this with linux machine, how to achieve this?

user3610920
  • 1,582
  • 2
  • 13
  • 24

6 Answers6

92

Once you've followed the (somewhat annoying) install steps to get .Net core installed and the apt repo setup from https://www.microsoft.com/net/core, you can just do this:

sudo apt install nuget

and you'll have a working nuget on your local machine:

$ cat /etc/issue
Ubuntu 16.04.1 LTS \n \l

$ nuget
NuGet Version: 2.8.7.0
usage: NuGet <command> [args] [options] 
Type 'NuGet help <command>' for help on a specific command.

Notice that as of the time of writing do not run nuget update -self, as although it will successfully install a more recent version of nuget, that version won't actually run.

If you do break it though, you can always just blow it away and reinstall:

sudo apt remove nuget
sudo apt install nuget
Doug
  • 32,844
  • 38
  • 166
  • 222
  • I tried this answer on openSuse 15.0 with no luck. Gave me this output ```Retrieving repository 'openSUSE-Leap-15.0-Update' metadata ...............[done] Building repository 'openSUSE-Leap-15.0-Update' cache ....................[done] Loading repository data... Reading installed packages... 'nuget' not found in package names. Trying capabilities. No provider of 'nuget' found. Resolving package dependencies... ``` – mdzeko Oct 29 '19 at 09:57
  • @mdzeko On openSuse you may need to use "mono-nuget" instead of nuget as the package name; I don't know why they chose to package it under that, but you can see the package here --> https://software.opensuse.org/ymp/openSUSE:Leap:15.1/standard/mono-nuget.ymp?base=openSUSE%3ALeap%3A15.1&query=mono-nuget – Doug Oct 30 '19 at 08:14
  • Thanks, I've seen that already and tried as well, but to no avail. I have a container that runs Debian so your original answer was helpful after all. Thanks again. – mdzeko Oct 30 '19 at 13:43
  • This does not work anymore. Newer versions requide dotnet-sdk or mono-complete – kyb Aug 07 '21 at 15:32
  • I can confirm, that in 2023, debian bookworm still installs nuget 2.8.7 and `nuget update -self` still does not work :( – kuga Aug 24 '23 at 14:47
28

Install mono, then download nuget:

sudo apt-get install mono-complete
wget https://dist.nuget.org/win-x86-commandline/latest/nuget.exe

After then run it using mono nuget.exe.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
olegz
  • 1,189
  • 10
  • 20
  • 3
    You might want to copy it to `/usr/lib/nuget/NuGet.exe` also, so that the `nuget` command will work. That one looks like this on Ubuntu 16.4: ``` #!/bin/sh exec /usr/bin/cli /usr/lib/nuget/NuGet.exe "$@" ``` – Erik A. Brandstadmoen Oct 17 '17 at 14:08
  • nuget has some weird finicky compatibility issues that are affected by the version of mono, so make sure you're actually pulling the latest version. I don't believe apt will always get the latest one: https://www.mono-project.com/download/stable/#download-lin-ubuntu – Sinaesthetic May 24 '18 at 18:08
  • on Ubuntu 16.04, the latest (https://dist.nuget.org/win-x86-commandline/latest/nuget.exe) version of nuget use a newer framework than the system mono supports. – Tom Nov 01 '18 at 21:17
  • For 16.04 4.5.1 is the latest that works. https://dist.nuget.org/win-x86- commandline/v4.5.1/nuget.exe (4.6.2 doesn't work) – Tom Nov 01 '18 at 21:23
  • Just adding this here for anyone getting System.Net.Http errors while trying to run nuget on linux / wsl, try installing mono-complete even if mono seems to be installed. – Dustin Jun 29 '20 at 17:02
17

nuget apt package doesn't really work on linux, and exe's are for windows. If you want to run nuget the easiest thing is to use mono wrapper.

sudo apt-get install mono-complete
//download nuget.exe
mono nuget.exe install
Warren Parad
  • 3,910
  • 1
  • 20
  • 29
3

Follow the Microsoft instructions for installing Nuget on Linux:

  1. Install Mono 4.4.2 or later.

  2. Execute the following command at a shell prompt (Bash):

    # Download the latest stable `nuget.exe` to `/usr/local/bin` 
    sudo curl -o /usr/local/bin/nuget.exe https://dist.nuget.org/win-x86-commandline/latest/nuget.exe
    
  3. Create an alias by adding the following script to the appropriate file for your OS (typically ~/.bash_aliases or ~/.bash_profile)(Bash):

    # Create as alias for nuget
    alias nuget="mono /usr/local/bin/nuget.exe"
    
  4. Reload the shell. Test the installation by entering nuget with no parameters. NuGet CLI help should display.

Gary Barrett
  • 1,764
  • 5
  • 21
  • 33
2

In case you want to use nuget with WSL2, the steps are as follows.

  1. Download nuget.exe through wget https://dist.nuget.org/win-x86-commandline/latest/nuget.exe

  2. Create a bash file called nuget:

> nuget
# Or
vi nuget
  1. Edit the file with the content below (vim nuget, then i):
# Edit the file with - make sure to add the correct path to nuget.exe file
'nuget.exe' $@ &
  1. Make it executable.
# Make it executable
chmod +x nuget
  1. Add to the $PATH environment variable
# Edit .bashrc
vi .bashrc
  1. Insert export PATH=/path/to/nuget-folder:$PATH in the .bashrc file.
Alexz S.
  • 2,366
  • 4
  • 21
  • 34
2

A quick and dirty way if you just need the package might be to use curl or wget and unzip. The script may look like:

curl -L -o package.zip https://www.nuget.org/api/v2/package/<target_package>

unzip package.zip -d <target_folder>

Pay attention to the -L curl flag that tells curl to follow any redirects. It's common that nuget would redirect you to a CDN.

Now, you have the latest nuget package in your desired folder. Optionally, you can target a specific version by appending the url with /<version>.


Examples:

curl -L -o package.zip https://www.nuget.org/api/v2/package/Microsoft.Azure.Kusto.Language
...

unzip package.zip -d test
Archive:  package.zip
  inflating: test/_rels/.rels
  inflating: test/Microsoft.Azure.Kusto.Language.nuspec
  inflating: test/lib/net472/Kusto.Language.dll
  inflating: test/lib/net472/Kusto.Language.xml
  inflating: test/lib/net5.0/Kusto.Language.dll
  inflating: test/lib/net5.0/Kusto.Language.xml
  inflating: test/lib/netcoreapp2.1/Kusto.Language.dll
  inflating: test/lib/netcoreapp2.1/Kusto.Language.xml
  inflating: test/lib/netstandard2.0/Kusto.Language.dll
  inflating: test/lib/netstandard2.0/Kusto.Language.xml
  inflating: test/EULA-agreement.txt
  inflating: test/MSFT.png
  inflating: test/[Content_Types].xml
  inflating: test/package/services/metadata/core-properties/0878db782482414487821399a0d09728.psmdcp
 extracting: test/.signature.p7s

cat test/Microsoft.Azure.Kusto.Language.nuspec | grep "<version>"
    <version>11.1.0</version>
curl -L -o package.zip https://www.nuget.org/api/v2/package/Newtonsoft.Json/12.0.2
...

unzip package.zip -d test
Archive:  package.zip
  inflating: test/LICENSE.md
  inflating: test/Newtonsoft.Json.nuspec
  inflating: test/packageIcon.png
  inflating: test/README.md
  inflating: test/[Content_Types].xml
  inflating: test/_rels/.rels
  inflating: test/lib/net20/Newtonsoft.Json.dll
  inflating: test/lib/net20/Newtonsoft.Json.xml
  inflating: test/lib/net35/Newtonsoft.Json.dll
  inflating: test/lib/net35/Newtonsoft.Json.xml
  inflating: test/lib/net40/Newtonsoft.Json.dll
  inflating: test/lib/net40/Newtonsoft.Json.xml
  inflating: test/lib/net45/Newtonsoft.Json.dll
  inflating: test/lib/net45/Newtonsoft.Json.xml
  inflating: test/lib/net6.0/Newtonsoft.Json.dll
  inflating: test/lib/net6.0/Newtonsoft.Json.xml
  inflating: test/lib/netstandard1.0/Newtonsoft.Json.dll
  inflating: test/lib/netstandard1.0/Newtonsoft.Json.xml
  inflating: test/lib/netstandard1.3/Newtonsoft.Json.dll
  inflating: test/lib/netstandard1.3/Newtonsoft.Json.xml
  inflating: test/lib/netstandard2.0/Newtonsoft.Json.dll
  inflating: test/lib/netstandard2.0/Newtonsoft.Json.xml
  inflating: test/package/services/metadata/core-properties/66867671be6046c2a70df93bfa9634b8.psmdcp
 extracting: test/.signature.p7s

cat test/Newtonsoft.Json.nuspec | grep "<version>"
    <version>12.0.2</version>
Santhos
  • 3,348
  • 5
  • 30
  • 48