如何在Linux命令行中安装NuGet

65

我需要在基于Linux的机器上安装NuGet。当我在Windows机器上使用以下命令时,它可以正常工作:

nuget install packages.config

但是我无法在Linux机器上执行此操作,该怎么做呢?


请查看这个链接 - Avihoo Mamka
6个回答

94

如果你已经按照(有点烦人的)安装步骤安装了 .Net Core 并从 https://www.microsoft.com/net/core 设置好 apt 软件库,那么你只需要执行以下操作:

sudo apt install nuget

然后您就可以在本地计算机上使用NuGet:

$ 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.

注意,截至撰写本文时,请不要运行nuget update -self,因为虽然它会成功安装更高版本的NuGet,但该版本实际上无法运行。

如果您确实损坏了它,可以随时将其删除并重新安装:

sudo apt remove nuget
sudo apt install nuget

我在openSuse 15.0上尝试了这个答案,但没有成功。 它给了我这个输出 `` `检索存储库'openSUSE-Leap-15.0-Update'元数据...............[完成] 构建存储库'openSUSE-Leap-15.0-Update'高速缓存.................... [完成] 加载存储库数据... 阅读已安装的软件包... 在软件包名称中找不到'nuget'。 尝试功能。 找不到“nuget”的提供程序。 解决软件包依赖关系...``` - mdzeko
@mdzeko 在 openSuse 上,您可能需要使用“mono-nuget”而不是nuget作为软件包名称;我不知道他们为什么选择将其打包在下面,但您可以在此处查看软件包--> https://software.opensuse.org/ymp/openSUSE:Leap:15.1/standard/mono-nuget.ymp?base=openSUSE%3ALeap%3A15.1&query=mono-nuget - Doug
谢谢,我已经看到并尝试过了,但是没有成功。我有一个运行Debian的容器,所以你最初的回答还是有帮助的。再次感谢。 - mdzeko
这个已经不再起作用了。更新的版本需要dotnet-sdk或mono-complete。 - kyb
我可以确认,在2023年,Debian Bookworm仍然安装NuGet 2.8.7,并且nuget update -self仍然无法正常工作 :( - kuga

28

安装Mono,然后下载NuGet:

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

然后使用mono nuget.exe运行它。


3
你可能希望将其复制到/usr/lib/nuget/NuGet.exe,这样nuget命令就会起作用。在Ubuntu 16.4上,它看起来像这样:#!/bin/sh exec /usr/bin/cli /usr/lib/nuget/NuGet.exe "$@" - Erik A. Brandstadmoen
NuGet存在一些奇怪的兼容性问题,这些问题受到Mono版本的影响,因此请确保您正在获取最新版本。我不认为apt总是会获取到最新版本:https://www.mono-project.com/download/stable/#download-lin-ubuntu - Sinaesthetic
在Ubuntu 16.04上,最新版本的NuGet(https://dist.nuget.org/win-x86-commandline/latest/nuget.exe)使用的框架比系统Mono支持的要新。 - Tom
在16.04版本中,4.5.1是最新可用的版本。请从以下链接下载:https://dist.nuget.org/win-x86-commandline/v4.5.1/nuget.exe(4.6.2版本不可用)。 - Tom
如果在Linux / WSL上运行nuget时遇到System.Net.Http错误,请尝试安装mono-complete,即使已经安装了mono。我在这里添加这个提示,希望能对大家有所帮助。 - Dustin

16

nuget apt包在Linux上无法正常工作,而exe文件则是针对Windows的。如果要运行nuget,最简单的方法是使用mono包装器。

sudo apt-get install mono-complete
//download nuget.exe
mono nuget.exe install

选择了这个解决方案。.NET在Linux上不支持32位。 - Chry Cheng

4

按照Microsoft的指南在Linux上安装Nuget

  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.


2
如果您想在WSL2中使用nuget,则需要按照以下步骤操作:
  1. 通过wget https://dist.nuget.org/win-x86-commandline/latest/nuget.exe下载nuget.exe

  2. 创建名为nuget的bash文件:

> nuget
# Or
vi nuget
  1. 使用以下内容编辑文件(vim nuget,然后按下i):
# Edit the file with - make sure to add the correct path to nuget.exe file
'nuget.exe' $@ &

使其可执行。
# Make it executable
chmod +x nuget
  1. 将其添加到$PATH环境变量中
# Edit .bashrc
vi .bashrc

在.bashrc文件中插入export PATH=/path/to/nuget-folder:$PATH

2

如果你只需要软件包,一个快速而简单的方法可能是使用curlwgetunzip。脚本可能如下:

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

unzip package.zip -d <target_folder>

注意 -L curl 标志,它告诉 curl 跟随任何重定向。通常情况下,nuget 会将您重定向到 CDN。

现在,您已经在所需的文件夹中拥有最新的 nuget 包。如果需要,您可以通过在 URL 后附加 /<version> 来针对特定版本。


示例:

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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接