How to Install NumPy

Introduction

NumPy (Numerical Python) is a powerful Python library for numerical computing. The library supports working with arrays, matrices, and various mathematical functions. NumPy is used for scientific computing, engineering, data science, and machine learning projects.

This guide shows how to install NumPy on various systems using different methods.

Tutorial on how to install NumPy.

Prerequisites

  • Access to the terminal.
  • Python 3 installed and added to PATH.
  • Either Conda or PIP installed for package management.

Note: This guide uses Ubuntu 22.04. The installation steps are similar accross different operating systems (Linux, macOS, and Windows). Windows users use the command prompt instead of the terminal and require administrator privileges to perform any installation.

How to Install NumPy via Terminal

The simplest way to install NumPy is via the terminal. Depending on the system requirements, you can use PIP or Conda to install the library.

The sections below show how to install it using both methods.

Install NumPy with PIP

To install NumPy using PIP, follow the steps below:

1. Open the terminal window.

2. Check if PIP is installed. Run the following command:

pip --version
pip --version terminal output

The command shows the PIP version if the package manager is installed. If the version number does not show, run the following command to install PIP:

python -m ensurepip

The command automatically runs a PIP installation script. Alternatively, see one of our OS-specific installation guides:

3. Use PIP to install NumPy. Run the following command:

pip install numpy

Wait for the installation to complete.

Note: This method installs NumPy globally. You can also use PIP to install NumPy inside a virtual environment or a Conda environment.

4. Test the installation with the following example code:

python3 -c "import numpy; print(numpy.__version__)"
python import numpy version terminal output

The command runs the Python code in quotation marks. If the installation succeeds, the code imports the library and prints the NumPy version.

Install NumPy Using Conda

When using Conda to manage Python libraries, follow the steps below to install NumPy:

1. Open the terminal.

2. Check that Conda is installed:

conda --version
conda --version terminal output

The command prints the Conda version. If not, download and install Anaconda or Miniconda.

3. Create a Conda environment:

conda create -n [environment_name]
conda create numpy_demo terminal output

Replace [environment_name] with the actual environment name. Enter y when prompted to proceed.

Note: The installation does not require creating an evnironment, but it's considered best practice to isolate Python projects for simpler dependency management.

4. Activate the environment with:

conda activate [environment_name]
conda activate numpy_demo terminal output

Use the environment name from the previous step. The terminal window changes to the environment.

5. Install NumPy with the following command:

conda install numpy
conda install numpy terminal output

Enter y to confirm the installation when prompted.

6. Test the installation with:

python3 -c "import numpy; print(numpy.__version__)"
conda numpy version terminal output

The command runs Python code that imports the NumPy library and prints the version number.

Build NumPy From Source

An advanced way to install NumPy is to build it from source. The method is meant for users with specific requirements and developers looking to contribute to the project.

Follow the steps below to build NumPy from source:

1. Clone the NumPy Git repository locally:

git clone https://github.com/numpy/numpy.git
git clone NumPy repository terminal output

The command clones the project into the numpy directory.

2. Navigate to the project directory:

cd numpy

3. Initialize and update submodules with git submodule update:

git submodule update --init
git submodule init NumPy terminal output

After this step, the submodule directories are fully functional.

4. Install the NumPy package from the current directory with:

pip install .
pip install . numpy terminal output

Wait for the installation to complete.

5. Navigate to the home directory, as running Python code from the installation directory will not work.

cd ..

Alternatively, navigate to your project directory or any other location.

6. Test the NumPy installation:

python3 -c "import numpy; print(numpy.__version__)"
Python import NumPy dev version terminal output

The command prints the NumPy dev version, indicating a successful installation.

How to Update NumPy

Update NumPy with PIP or Conda depending on the Python environment setup. See the examples below:

  • PIP:
pip install --upgrade numpy
  • Conda:
conda update numpy

Choose a method that best aligns with your current environment and NumPy installation method.

Conclusion

This guide showed how to install NumPy on your system using two different methods: PIP and Conda.

Next, check out our introduction to Python Pandas. It is an open-source Python library built on top of NumPy and primarily used for data analysis.

Was this article helpful?
YesNo
Milica Dancuk
Milica Dancuk is a technical writer at phoenixNAP with a passion for programming. With a background in Electrical Engineering and Computing, coupled with her teaching experience, she excels at simplifying complex technical concepts in her writing.
Next you should read
How To Install PIP to Manage Python Packages On Windows
November 30, 2023

PIP for Python is a utility to manage PyPI package installations from the command line. This tutorial will...
Read more
How to Install Pip on Ubuntu 18.04
August 20, 2024

Pip is a software utility that downloads packages from PyPI, the Python Package Index. Using PIP is like...
Read more
How to Install Latest Version Of Python 3 on CentOS 7
March 12, 2019

Python is a popular, stable, and well-performing programming language. CentOS 7 uses Python 2.7.5, but as...
Read more
How to Check Python Version in Linux, Mac, & Windows
December 15, 2023

Python is a popular programming language with different versions organized by release date. Certain...
Read more