2

My OS is Ubuntu 22.04.4 LTS. The "pip list" command tells that I have the NumPy 1.21.5. But I need a newer version of the NumPy.

I am trying to install the NumPy package whith next command:

pip install --upgrade numpy==2.1.2

But here is an error:

<...>
Run-time dependency python found: NO (tried pkgconfig, pkgconfig and sysconfig)

../meson.bould:41:12: ERROR: python dependency not found

I find that I must install -dev package. So I do that:

sudo apt install python3-dev

...but the NumPy installation error is still there.

What shall I do to upgrade the NumPy?

5
  • 6
    You should be using virtualenvs for your projects. Commented Oct 23, 2024 at 14:07
  • What do virtual variables or "my project" have to do with this? I want to update numpy. I want to type "pip list" and see that the numpy version has become 2.x.x. What exactly do I need to do to achieve the above goal? Commented Oct 23, 2024 at 14:28
  • 3
    Because if your numpy is provided by your OS (e.g. the python3-numpy Ubuntu package), you can't upgrade it with pip. Commented Oct 23, 2024 at 14:33
  • 1
    Furthermore, there are pre-built binary packages of Numpy 2.1.2 for various versions of Python and various CPUs, so you shouldn't need to build it from scratch, as you're evidently trying to. Which version of Python, and which architecture, are you on? Commented Oct 23, 2024 at 14:35
  • 1
    "virtual variables" is not the same as a virtual environment. And "your project" is a generic name for your current task at hand, which is doing something (analysis, coding, whatever) with NumPy 2.x. Commented Oct 23, 2024 at 14:43

4 Answers 4

2

Use a virtual-env first

cd <to-your-working-directory>
python3 -m venv venv
source venv/bin/activate

Your prompt will change accordingly, to indicate you are using the virtual environment. Any next session (either a new terminal, new terminal tab or simply a new login) only requires the source venv/bin/activate in that working directory, not the python -m venv part.

Now install NumPy

python3 -m pip install numpy

and install whatever else you need.

This will install a newer NumPy in that virtual environment; it is shielded from the system Python, so it can't break your system.

Now do your analysis / coding / whatever you want to do.


Note that I have explicitly used Python 3 here. The python command (and similar the pip command) may refer to Python 2, and that won't work with newer packages such as NumPy 2. Using python3 explicitly will ensure you don't accidentally use Python 2.

Usuing python3 -m pip in a virtual environment, is technically not necessary anymore(*), but it's a good enough habit to be clear what is going on, that I'll leave it like that.

(*) within a virtual environment, python and pip will refer to the Python you used to create that v-venv, which was the system Python 3.

Sign up to request clarification or add additional context in comments.

3 Comments

Depending on how python was installed, you make also need to do sudo apt install python3-venv before you can do python3 -m venv venv.
Oof, yes; I've never understood why Ubuntu (and some other distros) have split up Python so much. In particular because venv is part of the built-in packages.
I think it's so that the system python can exist without pip or ensurepip, and so be kept safe from users doing things like sudo python3 -m pip install foo and messing with the stability of the system that might depend on certain packages being installed at specific versions.
2

What's most likely going on here is: You are currently using the system version of both python and numpy. With "system version" I mean a version installed as an apt package (so via Ubuntu's package management) as opposed to being installed as a pip package (so via Python's default package manager).

What is usually not a good idea (and often simply does not work, as you experienced), is trying to update a Python package with pip that has been installed with apt.

The better idea, in my opinion, would usually be: keep the Python environment that you want to work with and the system's Python environment separate. This separation can happen on different levels:

  • You can use the system version of python, but might want to install your own version of numpy. This can be achieved, for example, using a virtual environment, as has been suggested in AKX's comment. The article linked there is a good deep-dive into the subject and also might provide you with further motivation as to why to choose this approach. The answer of 9769953 describes the necessary steps of setting up a virtual environment and installing numpy there.
  • You might even want to keep your python version separate from the system version of python. This can be achieved, for example, using anaconda/miniconda/miniforge. With conda and its ecosystem, you have yet another package management tool and repository available, which is also Python-focused, but includes more than Python packages. The article from AKX's comment has a brief section on conda as well.

There are plenty more options starting from there (obligatory xkcd cartoon, which, though being outdated, still captures the essence of the situation I think), and it may be hard to judge which setup will be best for your needs. For the pros and cons of virtual environments vs. conda in particular, you might also want to have a look at the answers to this related question that compares virtualenv (a superset of venv) to conda.

Comments

0

The command you're using has a syntax error. It should be

pip install --upgrade numpy==2.1.2

Notice the == instead of =. This might fix it.

2 Comments

I made a mistake when writing the question. In the console I write == or do not specify the version at all. The result is the same.
@Arseniy Which is why you should always copy-paste the commands you execute, as well as code you execute.
-2
  1. numpy is a Python module, it has nothing to do with the OS.
  2. pip automatically finds and installs the latest version of the module as supported by your python version when you write pip install <package>. If you see a newer version on pypi but is not being installed by pip, it's because your python version does not support that numpy version. Confirm if it is supported for your python version or upgrade your python version.
  3. default python command on ubuntu points to python 2 and not 3. Check if it is the case and change your config to point to python3 accordingly.

4 Comments

"numpy is a Python module, it has nothing to do with the OS.": except that you don't want to overwrite/upgrade NumPy if it was previously installed through an OS package. Knowing about the OS, and that it has support for NumPy packages through its OS package manager, is useful information.
"upgrade your python version" - On Ubuntu, the system version of Python cannot be upgraded. It's tied into the OS too tightly.
@wjandrea, I agree, but you can install multiple versions of python and set a default distribution which is used when you write python or python3.
@Shacklebolt13 Are you talking about Debian Alternatives? That's not used for Python on Ubuntu.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.