How to Install numpy on Ubuntu 24.04

NumPy is a powerful library for users working in data science or numerical computing with Python. Installing NumPy on Ubuntu serves a significant purpose for users involved in data analysis, scientific computing, or engineering applications. This combination allows for efficient and sophisticated data manipulation and analysis, which is essential for handling the complex computations required in these fields.

This article will teach different methods to install numpy on Ubuntu 24.04 based on supported content:

How to Install numpy on Ubuntu 24.04  

NumPy is a tool that helps with big, complex lists and tables, and it comes with many math tools to work on these lists. For users of Ubuntu 24.04, installing NumPy can be done in these methods:

  • Method 1: Installing NumPy Using the Ubuntu Repository
  • Method 2: Installing NumPy Using pip
  • Method 3: Installing numpy Using a Virtual Environment

Prerequisites

Before installing NumPy, ensure that Python3 and pip (Python’s package installer) are installed on the system. Python3 comes pre-installed on Ubuntu 24.04, but users can authenticate its presence via the below command:

python3 --version

To install or verify pip, use the “python3-pip” package as below:

sudo apt install python3-pip

Let’s start with the easiest method.

Method 1: Installing NumPy from the Ubuntu Repository

For Ubuntu users, installing NumPy can be done quickly and easily from the Ubuntu repository. It is the easiest way to install NumPy directly from the Ubuntu repository. 

Here’s a step-by-step guide to installing NumPy on Ubuntu:

Step 1: Update Package List 

First of all, update the package list to make sure that you have the latest version of the repository listings:

sudo apt update

Step 2: Install NumPy

If you’re using Python 3 (which is highly recommended as Python 2 has reached end-of-life), users can install NumPy with the “python3-numpy” package name:

sudo apt install python3-numpy

Step 3: Check NumPy Version 

After installation, users can authenticate the installed version of NumPy by executing the below command:

python3 -c "import numpy; print(numpy.__version__)"

That is all from the Ubuntu Repository method.

Method 2: Installing NumPy Using pip

NumPy is an important library for users working in data science or using Python for computations. If you’re using Ubuntu, installing NumPy with pip is a simple process. It might provide a more recent version.

To install NumPy on Ubuntu 24.04 using pip, follow the below steps: 

Step 1: Install pip 

First, ensure that pip is installed, if not install it with the “python3-pip” package name:

sudo apt install python3-pip

Step 2: Install NumPy 

Then, install NumPy with the “pip3” command by mentioning the “numpy” module name. Here, myenv is the virtual environment that is set for the Python package. For setting, follow this Method 3:

pip3 install numpy

If the user finds the “ModuleNotFoundError: No module named ‘numpy’”  error, ensure that you have pip installed and that it is added to your system’s PATH. To add to the system’s PATH follow the below method.

Method 3: Installing NumPy Using a Virtual Environment

When installing Python packages, It’s good practice to utilize a virtual environment. This allows users to manage dependencies more effectively. 

To install numpy using a virtual environment, follow the below steps:

Step 1: Install Python3 Virtual Environment

To create and activate a virtual environment, users first need to install this environment as below:

sudo apt install python3.12-venv

Step 2: Create and Activate Python3 Virtual Environment

After successfully installation, users need to create and activate a virtual environment for installation of the numpy module:

python3 -m venv myenvsource myenv/bin/activate

Step 3: Install NumPy 

Now, install NumPy with the “pip3” command by mentioning the “numpy” module name:

pip3 install numpy

Step 4: Check NumPy Version

Finally, check the installation with the version utility below:

python3 -c "import numpy; print(numpy.__version__)"

That is all from the installation of numpy from setting a virtual environment.

How to Upgrade NumPy on Ubuntu 24.04

For upgrading NumPy to the latest version on Ubuntu, users can use the Ubuntu Repository as well as the pip installer. Let’s follow the below methods for upgrading:

Method 1: Upgrading NumPy Using Ubuntu Repository

If users already have NumPy installed and wish to upgrade to the latest version, they can do so via the repository with the below command: 

sudo apt install --only-upgrade python3-numpy

Method 2: Upgrading NumPy Using pip

If users already have NumPy installed and want to upgrade to the latest version, they can do so with the pip installer:

source myenv/bin/activate
pip3 install --upgrade numpy

Note: If you need to upgrade NumPy to the latest version, you can use pip, the Python package installer. First, ensure that pip is installed via the “sudo apt install python3-pip” command.

How to Remove NumPy from Ubuntu 24.04

Removing NumPy from Ubuntu 24.04 can be accomplished through various methods, depending on how the package is installed.

Method 1: Using apt or Ubuntu Repository

If numpy was installed using the “apt” manager, users can remove it via the below command:

sudo apt remove numpysudo apt autoremove python3-numpy

To also remove unused dependencies and configuration files, utilize the “autoremove” as below:

sudo apt autoremove numpy --purgesudo apt remove python3.12-venv # For Removing virtual environment

Method 2: Using pip

If numpy was installed via “pip”, users can uninstall it using the below commands for different Python versions: 

source myenv/bin/activatepip3 uninstall numpy
pip uninstall numpy

Method 3: Using dpkg

For packages installed with “dpkg”, the command “dpkg -r” removes the package:

sudo dpkg -r numpy

while the -p option also purges configuration files: 

sudo dpkg -P numpy

Remember to check which method corresponds to how numpy was installed on your system to ensure proper removal.

FAQ

Keeping the NumPy library up to date is important for getting to the recent features. For detailed instructions, refer to the guides available on Linux configuration websites, or the below answers: 

How to Fix: No module named NumPy?

The error “No module named NumPy” typically occurs when the NumPy package is not installed in the Python environment. To resolve this, install NumPy using the package manager pip. Simply run the “pip install numpy” command in the terminal interface. 

If users are using Python 3, use the “pip3 install numpy” command instead. Also, make sure that you have pip installed and that it is added to the system’s PATH. If the issue remains, it may be due to several Python versions installed on the system. Then, users might need to specify the Python version when installing NumPy or check their PATH environment variable.

How to Create and Activate Python Virtual Environment on Ubuntu?

To create and activate a Python virtual environment on Linux, use the “venv” module. First, ensure you have Python installed. Then, create a virtual environment by executing “python3 -m venv virtual_env”. Here, “virtual_env” is the environment name. 

To activate the Python Virtual Environment, use the “source myenv/bin/activate” command. It indicates users are working within the virtual environment. To deactivate it, simply run “deactivate”.

Fix: “Import Error: No module named numpy”

The error “Import Error: No module named numpy” typically occurs when Python cannot find the NumPy library in your environment. It may occur if the library is not installed or if it’s installed in a location (not included in Python’s search path). 

To resolve this issue, install NumPy using the “pip” manager via the “pip install numpy” in the terminal. If you have several versions of Python installed, make sure to use the version-specific pip command, e.g. “pip3” for Python 3. 

Additionally, verify that NumPy is installed in the correct environment where your Python script is running, as installing it in a different environment can lead to this error.

Conclusion

To install NumPy on Ubuntu, use either the “apt” package manager or “pip”, the Python package installer. For a quick installation, execute “sudo apt install python3-numpy” in the terminal. It installs NumPy for Python 3. If you prefer to use “pip”, you can run “pip3 install numpy” after ensuring that “pip” is installed on the system. It’s recommended to use a virtual environment when installing packages with “pip” to avoid conflicts with system packages and other Python projects.

Whether you choose to install NumPy from the Ubuntu repository or via pip, the process is straightforward. After installation, you can begin using NumPy in your Python programs on Ubuntu 24.04.