Installing NumPy on Ubuntu – A No‑Nonsense Guide
If you’re trying to get your data‑science stack up and running, the first thing most people do is install NumPy. This article shows you how to drop that library into your Ubuntu machine without getting lost in a maze of dependencies.
Make Sure Your System Is Ready
Before you start pulling packages from anywhere else, let’s tidy up. An old, half‑broken python3 installation is a common culprit for headaches later on.
sudo apt update && sudo apt upgrade -y
Why run the upgrade? Because it pulls in the latest security patches and keeps the core Python libraries in sync with what Ubuntu ships. I’ve seen this happen after a bad driver update that left libssl out of date, causing pip to choke on SSL errors.
Install the Base Python Development Tools
NumPy needs to compile C extensions if you’re not using wheels that match your exact distro version. That means you need GCC and the header files for Python.
sudo apt install python3-dev build-essential
The python3-dev package provides the necessary headers (Python.h) for building native modules, while build-essential gives you a working compiler set. Skipping this step often results in an error like “error: Python.h file not found”.
Pick Your Installation Path
You have three common options: system‑wide pip install, user‑site installation, or a virtual environment. I’ve seen people fall into the trap of installing with sudo pip and then running into permission headaches later.
- System‑wide (sudo pip install numpy) – Quick, but messes with Ubuntu’s package manager.
- User‑site (pip install --user numpy) – Keeps files in your home directory; no root needed.
- Virtualenv/Conda – Isolated and the cleanest for projects that have specific dependency versions.
If you’re just learning or building a script, use the user‑site method. For projects, I always prefer virtualenv:
python3 -m venv myenv source myenv/bin/activate pip install numpy
Install NumPy with pip
Assuming you’re in your chosen environment (user or venv), the command is straightforward:
pip install --upgrade numpy
The --upgrade flag ensures you get the latest wheel for Ubuntu 22.04, which already contains the compiled C extensions. No more waiting a few minutes for compilation unless you’re on an older release that lacks pre‑built wheels.
Verify the Installation
Open a Python shell and try importing NumPy:
import numpy as np print(np.__version__)
If you see something like 1.26.x printed, congratulations—you’ve got a working installation. If you hit an import error, double‑check that you’re running the same Python interpreter where you installed NumPy.
Common Pitfalls and Quick Fixes
- Missing GCC – “error: ‘Python.h’ file not found” => sudo apt install python3-dev build-essential.
- SSL errors during pip install – Usually a broken openssl library; run sudo apt reinstall libssl-dev.
- Permission denied on /usr/local/lib/python3.*/dist-packages – Don’t use sudo pip; switch to user‑site or venv.
Clean Up (Optional)
If you installed NumPy system‑wide and want to revert, remove it with:
sudo apt purge python3-numpy
Or if you used pip, uninstall:
pip uninstall numpy
That’s it. You’re now ready to drop NumPy into your scripts or notebooks without wrestling with dependencies.