877

I've come across situations where a current version of a package seems not to be working and requires reinstallation. But pip install -U won't touch a package that is already up-to-date. I see how to force a reinstallation by first uninstalling (with pip uninstall) and then installing, but is there a way to simply force an "update" to a nominally current version in a single step?

1
  • 4
    for those looking to re-install pip it self (if it stopped working for some reason ;) ), the answer can be found in this SO q&a
    – nsof
    Commented Nov 16, 2019 at 17:18

10 Answers 10

1296
pip install --upgrade --force-reinstall <package>

When upgrading, reinstall all packages even if they are already up-to-date.

pip install -I <package>
pip install --ignore-installed <package>

Ignore the installed packages (reinstalling instead).

12
  • 2
    Any way to force an overwrite when using --target= flag? none of these worked for me. I get the destination path already exists error.
    – radtek
    Commented Aug 5, 2014 at 20:09
  • 3
    Including --upgrade when --force-reinstall is being used shouldn't be needed as of pip 10.0, FYI: github.com/pypa/pip/issues/1139
    – cjerdonek
    Commented Feb 1, 2019 at 16:45
  • 3
    @mrgloom The using cachedjust means it uses source files that where cached on the last install. To force re-download use the --no-cache-dir flag.
    – lcnittl
    Commented Jul 25, 2019 at 7:03
  • 8
    Note that this command also reinstalls all dependencies. Add --no-deps to avoid that, as suggested in Finn���s answer below. Commented Jan 11, 2021 at 13:03
  • 2
    This does not work for updating pip itself
    – Hektor
    Commented Feb 28, 2021 at 10:31
311

You might want to have all three options: --upgrade and --force-reinstall ensures reinstallation, while --no-deps avoids reinstalling dependencies.

$ sudo pip install --upgrade --no-deps --force-reinstall <packagename>

Otherwise you might run into the problem that pip starts to recompile Numpy or other large packages.

9
  • 2
    This also works for offline installs, while the excepted answer doesn't.
    – orodbhen
    Commented Jun 1, 2018 at 14:24
  • 8
    This is a better solution for packages with a large number of dependencies that do not need to be reinstalled.
    – Assil
    Commented Nov 15, 2018 at 15:43
  • 1
    sudo was crucial in my case.
    – mrgloom
    Commented Aug 19, 2019 at 12:12
  • 3
    Why we need --upgrade when we use --force-reinstall?
    – mrgloom
    Commented Aug 19, 2019 at 12:13
  • 1
    macOS: You shouldn't run sudo with pip on a mac . Run as admin rights user but without sudo . On Linux (Ubuntu): it makes sense to run with sudo to install for all users. Don't run sudo with --user as that will install packages under root user only.
    – wesinat0r
    Commented Jul 21, 2020 at 12:46
62

If you want to reinstall packages specified in a requirements.txt file, without upgrading, so just reinstall the specific versions specified in the requirements.txt file:

pip install -r requirements.txt --ignore-installed
3
  • And if you want to avoid using the local cache, add the option --no-cache-dir
    – Davy
    Commented Feb 15, 2022 at 14:31
  • This may still upgrade packages, though, if the version restrictions in the requirements.txt allow for it.
    – Alperino
    Commented Dec 2, 2022 at 14:22
  • Indeed, but if you force them to a fixed version with '==' then no upgrades will happen
    – Davy
    Commented Dec 3, 2022 at 13:52
48
--upgrade --force-reinstall

doesn't appear to force reinstall using python2.7 with pip-1.5

I've had to use

--no-deps --ignore-installed
1
  • 29
    You must specify --upgrade in addition to --force-reinstall, or it won't have any effect. Commented Feb 12, 2014 at 4:32
24
sudo pip3 install --upgrade --force-reinstall --no-deps --no-cache-dir <package-name>==<package-version>

Some relevant answers:

Difference between pip install options "ignore-installed" and "force-reinstall"

1
  • 1
    --no-cache-dir is exactly what I was looking for, though the question isn't posed as such. Thanks. Commented Sep 30, 2022 at 3:22
21

In the case you need to force the reinstallation of pip itself you can do:

python -m pip install --upgrade --force-reinstall pip
11

If you have a text file with loads of packages you need to add the -r flag

pip install --upgrade --no-deps --force-reinstall -r requirements.txt
1

I had a Jupyter notebook open using the python kernel that has the package loaded already. I closed that notebook and tried again and it worked.

0
1
pip install --force-reinstall <package name>

this can be used

1
  • Thank you for your interest in contributing to the Stack Overflow community. This question already has quite a few answers—including one that has been extensively validated by the community. Are you certain your approach hasn’t been given previously? If so, it would be useful to explain how your approach is different, under what circumstances your approach might be preferred, and/or why you think the previous answers aren’t sufficient. Can you kindly edit your answer to offer an explanation? Commented Sep 13, 2023 at 0:18
0

If you work on MacOS and are using Homebrew, run:

/opt/homebrew/opt/[email protected]/bin/python3.11 -m pip install --upgrade pip

of course use the appropriate path for your version

Not the answer you're looking for? Browse other questions tagged or ask your own question.