How to fix error: legacy-install-failure in Python

featured Image

If you are a Python developer, you might come across the “error:legacy-install-failure” error while installing or upgrading packages using the pip install command.

note: This error originates from a subprocess, and is likely not a problem with pip.
error: legacy-install-failure

In this article, we will discuss the cause of the error and provide different solutions that can resolve the Legacy install failure error in python.

What is “legacy-install-failure” error in python?

This error occurs because of compatibility issues when you try to install a python package using pip install command. To fix it you need to update your Package Manager or the Python version in your system.

The error means that the package you are trying to install or upgrade requires a newer version of Python. This error is common for users that are using Python2 or an older version of Python 3.

Causes of the “legacy-install-failure” error

The main reason for this error is that you are using an older version of python that is not compatible with the package you want to install or upgrade.

You can check in Python Package Index (PyPI) that all packages have a minimum version of Python to function properly.

If the Python version that is installed in your system does not match the minimum requirements of the specific package then you will get the “error: legacy-install-failure” error in your terminal.

Solutions to the error

Now, we know the reason for the error. So let’s see some of the solutions that can help you resolve the error in your project.

Solution 1: Upgrade pip and other built tools

One of the main reasons for the “legacy install failure” error is that the build tools you are using are not updated.

So, you have to upgrade pip , setuptools and wheel versions before installing or upgrading any Python packages.

The build tools depend on the Python version and the operating system you are currently using on your computer.

To upgrade the build tools use the following commands given below:

#for pip
pip install --upgrade pip

#for pip3 (Python 3)
pip3 install --upgrade pip

#for you get any permission error
pip install --upgrade pip --user

If you do not have pip in PATH environment variable, then use the following command:

python -m pip install --upgrade pip

#for python 3
python3 -m pip install --upgrade pip

For windows, use this command to update pip:

py -m pip install --upgrade pip

To upgrade setuptools and wheel, use the following code:

pip install --upgrade setuptools wheel

#for pip3 (Python 3)
pip3 install --upgrade setuptools wheel

#With no PATH
python -m pip install --upgrade setuptools wheel

python3 -m pip install --upgrade setuptools wheel

#for windows
py -m pip install --upgrade setuptools wheel

Now that you have successfully upgraded pip and other build tools, you can now try to install packages using pip install <package name>.

Solution 2: Check if the package name has been changed

Another reason that can cause the “legacy install failure” error is that the package you are trying to install has its name changed.

So double check the package name before installing a package in your project.

You can go to Python Package Index (PyPI) and check if the package name has been renamed or just google search “pypi package-name”.

For example, if you try to install the package – “Locust” using the old name (locustio), it will throw an error. So, use the new name (locust) when installing the package.

#old package name (will throw an error)
pip install locustio 

#use the new package name
pip install locust

#or
pip3 install locust

Solution 3: Install packages with –no-dependencies option

Another reason that might cause the “legacy-install-failure” error is conflicts between the package you are installing and the packages that are already installed in your computer.

To resolve this we have to use the --no-dependencies options with the pip install while installing a package.

pip install <package-name> --no-dependencies

This command will prevent pip from installing any dependencies and will resolve the error.

Solution 4: Use a Virtual Environment

Another solution to fix the “legacy install failure” error is to use a virtual environment.

A virtual environment is a tool that allows the creation of an isolated python environment that is separated from your system’s Python installation.

Using a virtual environment we can install or upgrade packages without affecting the system’s Python installation.

To create a virtual environment, you can follow the steps given below.

Step 1: Navigate to your project folder and open your command prompt or terminal.

Step 2: Run the following command to create a virtual environment:

python -m venv venv
#or
python3 -m venv venv
#or
py -m venv venv

This command will create a virtual environment in a folder named as “venv” in your project directory.

Step 3: Next, we have to activate the virtual environment. To activate, run the following command:

For Linux or MacOS,

source venv/bin/activate

For windows,

#for command prompt
venvScriptsactivate.bat

#for powershell command
venvScriptsActivate.ps1

Step 4: Once the environment is created and activated, you can now try to install the package in your virtual environment using pip install command.

pip install <package-name>

Solution 5: Activate developer path in MacOS

If you are on MacOS, you might get the following error while installing packages:

xcrun: error: invalid active developer path..
missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun.

This means you have to install the Xcode Command Line Tools to fix the error.

So before installing any packages, first install the xcode using the following command:

xcode-select --install

Once successfully installed you can install Python packages without any issues.

Solution 6: Download .whl file for Windows

You can also install the wheel distribution of the specific package directly on your computer if you are a Windows user.

For example, let’s say you want to install numpy package, so to do that go to pypi.org and search for your package name.

There you can see all the built distributions available for all operating systems.

legacy-install-failure error whi install

For Windows users use the win_ packages. You will two variants: amd64 for 64-bit and win32 for 32-bit devices.

For MacOS users, use the macosx_ packages. The x86_64 is for Intel Mac and universal for apple silicone.

For Linux users, use the manylinux_ packages. The x86_64 is for Intel and aarch64 is for ARM devices.

First, you have to download the Windows 64-bit version of the package in your system.

Next, open your powershell or cmd in the same folder where you have downloaded the file and use pip install command to install the package.

You can press Shift + right-click inside the folder to get the option “Open Powershell window here“.

pip install numpy-1.24.2-cp311-cp311-win_amd64.whl

#or use pip3
pip3 install numpy-1.24.2-cp311-cp311-win_amd64.whl

cp311 is the Python version 3.11 and amd64 means 64-bit.

This command will install the numpy wheel package in your windows machine without any “legacy install failure” error message.

Conclusion:

In this article, we have learned about the causes of the “legacy-install-failure” python error. And also discussed the different solutions to resolve the issue.

The main reason for this error is the compatibility issue between the package with the version of pip or python version installed on our computer.

And the easy way to solve this error is to upgrade the pip and other build tools in our system.

You can also install the package directly in your system using the wheel distribution package (.whl) from pypi.org.

Related Posts

merge json files in python

How to merge JSON files in python

JSON (JavaScript Object Notation) is a data format used for storing, transferring, and visualizing data. Its simple structure of key:value pairs and array data types makes JSON an easy and…

Read more
Install python pip in ubuntu

How to Install Pip(pip3) in Ubuntu Linux 22.04

Pip is a package manager for Python that allows you to install and manage additional Python packages that are not part of the Python standard library. Pip makes it easy…

Read more
featured Image

How to Write If-Else Statements in One Line in Python

If-else statements are a fundamental concept in Python and other programming languages. They allow you to execute different blocks of code based on whether a condition is true or false….

Read more

Leave a Reply

Your email address will not be published. Required fields are marked *