401

I want to install an older version of a package (Newtonsoft.Json). But NuGet rolls back:

PM> Install-Package Newtonsoft.Json -Version 4.0.5
Successfully installed 'Newtonsoft.Json 4.0.5'.
Install failed. Rolling back...
Install-Package : Already referencing a newer version of 'Newtonsoft.Json'.

How can I do it?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nebide Yildiz
  • 4,031
  • 2
  • 15
  • 12
  • 3
    possible duplicate of [Download old version of package with nuget](http://stackoverflow.com/questions/5628689/download-old-version-of-package-with-nuget) – Dirk Vollmar Oct 22 '13 at 09:10

5 Answers5

597

Try the following:

Uninstall-Package Newtonsoft.Json -Force

Followed by:

Install-Package Newtonsoft.Json -Version <press tab key for autocomplete>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Xavier Decoster
  • 14,580
  • 5
  • 35
  • 46
  • twitterizer uses Newtonsoft.Json, I have to install older without uninstall newer. PM> Uninstall-Package Newtonsoft.Json Uninstall-Package : Unable to uninstall 'Newtonsoft.Json 4.0.8' because 'twitterizer 2.4.0.26532' depends on it. – Nebide Yildiz Apr 19 '12 at 05:41
  • 9
    You didn't mention existing dependencies to the package so I was unware of that: try adding the -Force switch to the uninstall-package command (as edited above) – Xavier Decoster Apr 19 '12 at 08:16
  • Sorry for my missing. -Force worked and I installed the older one. Thank you so much. – Nebide Yildiz Apr 19 '12 at 08:25
  • when uninstalling EntityFramework 6 beta to downgrade to version 5 I kept getting messages telling me to restart VS to complete uninstall but doing so didn't remove the message. I just went into packages folder and deleted the remaining empty tree structure from there and it was then successful – Simon_Weaver Apr 26 '13 at 03:31
  • @Simon_Weaver I suspect the EF 6 pkg is doing something that causes this (noticed some AppDomain code for instance in the PowerShell scripts, so likely VS is holding on to some of the dll's) – Xavier Decoster Apr 26 '13 at 07:28
  • I don't see the specific version I need in the dropdown list (7.0.0). And when I specify the version number it gives an error. Any ideas? – CorribView Jul 27 '16 at 19:10
  • This may be an unlisted version? Unlisted packages are filtered out of search results and autocomplete. – Xavier Decoster Sep 05 '16 at 12:21
273

As of NuGet 2.8, there is a feature to downgrade a package.

NuGet 2.8 Release Notes

Example:

The following command entered into the Package Manager Console will downgrade the Couchbase client to version 1.3.1.0.

Update-Package CouchbaseNetClient -Version 1.3.1.0

Result:

Updating 'CouchbaseNetClient' from version '1.3.3' to '1.3.1.0' in project [project name].
Removing 'CouchbaseNetClient 1.3.3' from [project name].
Successfully removed 'CouchbaseNetClient 1.3.3' from [project name].

Something to note as per crimbo below:

This approach doesn't work for downgrading from one prerelease version to other prerelease version - it only works for downgrading to a release version

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
James Roland
  • 8,183
  • 4
  • 30
  • 16
  • 17
    This should now be the accepted answer as its the best solution with minimal effort. – Peter Oct 24 '14 at 07:53
  • Yes, works like a charm, including downgrading all dependencies - perfect – peter Dec 03 '14 at 22:20
  • 8
    Unfortunately this approach doesn't work for downgrading from one prerelease version to another prerelease version - it only works for downgrading to a release version. – crimbo Dec 11 '14 at 19:58
  • @James Roland it'd great if you can highlight the prerelease warning by crimbo on the answer – eglasius Dec 18 '15 at 10:23
51

I've used Xavier's answer quite a bit. I want to add that restricting the package version to a specified range is easy and useful in the latest versions of NuGet.

For example, if you never want Newtonsoft.Json to be updated past version 3.x.x in your project, change the corresponding package element in your packages.config file to look like this:

<package id="Newtonsoft.Json" version="3.5.8" allowedVersions="[3.0, 4.0)" targetFramework="net40" />

Notice the allowedVersions attribute. This will limit the version of that package to versions between 3.0 (inclusive) and 4.0 (exclusive). Then, when you do an Update-Package on the whole solution, you don't need to worry about that particular package being updated past version 3.x.x.

The documentation for this functionality is here.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
qxn
  • 17,162
  • 3
  • 49
  • 72
25

Now, it's very much simplified in Visual Studio 2015 and later. You can do downgrade / upgrade within the User interface itself, without executing commands in the Package Manager Console.

  1. Right click on your project and *go to Manage NuGet Packages.

  2. Look at the below image.

    • Select your Package and Choose the Version, which you wanted to install.

NuGet Package Manager window of Project

Very very simple, isn't it? :)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
RajeshKdev
  • 6,365
  • 6
  • 58
  • 80
1

Another more manual option to get it:

.nuget\nuget.exe install Newtonsoft.Json -Version 4.0.5
knocte
  • 16,941
  • 11
  • 79
  • 125