How to Install NumPy on Windows, Linux and Mac: step-by-step guide

We can install NumPy on all major operating systems such as Windows, Linux and macOS. NumPy installation process is very simple and can be done using pip, which is the Python package installer.

Here are the steps to install NumPy on Windows, Linux and Mac operating systems.

Install NumPy on Windows, Linux and Mac

How to Install NumPy on Windows, Linux and macOS
How to Install NumPy on Windows, Linux and macOS

Prerequisites to Install NumPy

Install Python

NumPy is a Python library. Before we start to install NumPy with any operating system, we need to have Python installed on that machine. We can download and install the latest version of Python from Python’s official website.

  • Hardware Requirements
    • RAM: At least 4GB RAM for smooth performance.
    • Processor: Modern multi-core processor (like Intel i5).
  • Operating System – NumPy installation can be done with Windows, MacOS, or Linux.
  • Python VersionPython 3.7 or higher should be installed and added to the system PATH on your machine.
  • Follow the comprehensive guide to install Python 3.
  • Verification for Python 3 – Run the command from terminal => python --version
  • Ensure either Either Conda or PIP installed for package management. Follow the guide to install PIP on your machine – Installation of PIP

During Python installation, ensure to include Python in your System PATH.

Verify Python and pip

python --version
pip --version

Install NumPy on Windows

Install NumPy using pip on Windows

Once Python is installed on Windows operating system, we can install NumPy on Windows by running the following command in the Command Prompt (cmd) or PowerShell with administrator access.

pip install numpy

If above command does not work with Python 3 installation, try with following command.

pip3 install numpy

This command will download and install the latest version of NumPy from the Python Package Index (PyPI).

Verify NumPy Installation

After installation, you can verify that NumPy was installed correctly by opening the Python interpreter and importing NumPy.

  • Open Command Prompt or PowerShell and start the Python interpreter using command
python

# Output
# Python 3.10.3 (tags/v3.10.3:a342a49, Mar 16 2022, 13:07:40) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
  • Run following command, on Command Prompt Python interpreter
import numpy as np
print(np.version)

# Output
# <module 'numpy.version' from 'C:\\Users\\Lenovo\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\numpy\\version.py'>

This will print the installed version of NumPy, which confirmed that NumPy has been installed successfully.

Install NumPy on Linux (Ubuntu/Debian)

Install NumPy using pip on Linux

Once Python is installed on Linux operating system, we can install NumPy on Linux by running the following command in the Linux terminal.

pip3 install numpy

If you need to install NumPy for a specific version of Python, replace pip3 with the relevant version’s pip, for example, pip3.8 if you are using Python 3.8.

Verify NumPy Installation on Linux

After installation, you can check if NumPy is installed by typing the following in the terminal:

python3      # This will start the Python interpreter

import numpy as np      # import NumPy library
print(np.__version__)    # check for NumPy version

This will print the installed version of NumPy, confirming the installation of NumPy.

Install NumPy on macOS

Install Python on macOS

macOS generally comes with Python pre-installed, but it’s good practice to install the latest version of Python via Homebrew (a package manager for macOS).

  • Install Homebrew by running the following command in the Terminal
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

After Homebrew is installed, we can use it to install the latest version of Python.

brew install python

Install NumPy using pip on macOS

pip3 install numpy

For Python 2.x, replace pip3 with pip.

Verify NumPy Installation on macOS

python3      # This will start the Python interpreter

import numpy as np      # import NumPy library
print(np.__version__)    # check for NumPy version

This should print the installed version of NumPy, confirming that the NumPy installation was successful.

Alternative Installation Methods (Optional)

Install NumPy using Anaconda (Cross-platform)

Anaconda is a popular, open-source distribution of the Python and R programming languages. It simplifies package management, deployment, and environment management for these languages. Some users prefer to manage Python environments and packages via Anaconda. We can install NumPy using Anaconda:

Install Anaconda by downloading the installer from: https://www.anaconda.com/download

After Anaconda is installed, open the Anaconda Prompt (Windows) or your terminal (macOS/Linux). Run the following command to install NumPy in your active environment:

conda install numpy

Verify the NumPy installation as shown in the previous section.

Install NumPy using Virtual Environment (Cross-platform)

Creating a virtual environment allows you to isolate your Python environment and avoid version conflicts with system-wide packages. It’s a good practice to create project specific virtual environment and we can install only packages required for that project. Here’s how we can setup and Install NumPy using Virtual Environment:

  • Create a virtual environment
python3 -m venv myenv
  • Activate virtual environment
myenv\Scripts\activate
source myenv/bin/activate
  • Install NumPy
pip install numpy

Verify the installation by importing NumPy as shown in earlier sections.

Troubleshooting for NumPy Installation

If we face any challenge or encounter any issue while installing NumPy, we can check for following steps.

  • Ensure that pip is updated by running pip install --upgrade pip.
  • On Windows, ensure to run the Command Prompt (cmd) or PowerShell as Administrator to install packages.
  • On Linux/macOS, ensure that you have the required permissions for installation. Use sudo if necessary for system-wide installations.
  • Check for any error messages during installation. Some times we require to install additional dependencies, especially on Linux.

Example Program using NumPy

This is an example program, to create NumPy array and perform basic mathematical operations like Multiplication and Additions.

import numpy as np    # import numpy and refer it with alias np

# Creating a NumPy array
numpy_array = np.array([12, 22, 34, 54, 65])

# Displaying the array
print("NumPy Array:", numpy_array)

# Performing mathematical operations
print("Array multiplied by 2:", numpy_array * 2)  # multiplication operation on numpy array elements
print("Sum of the Array:", np.sum(numpy_array))   # sum of all elements of numpy array

# Output
# NumPy Array: [12 22 34 54 65]
# Array multiplied by 2: [ 24  44  68 108 130]
# Sum of the Array: 187

Explanation of the code:

  • First we imported NumPy library into the program using => import numpy as np
  • np.array([12, 22, 34, 54, 65]) => This creates a NumPy array, which is referenced by a variable numpy_array.
  • Print all the elements of numpy_array.
  • numpy_array * 2 => Multiply all elements of array by 2.
  • np.sum(numpy_array) => Using sum function of numpy library, to calculate sum of all array elements.

Conclusion

In this tutorial, we covered step-by-step guide on how to install NumPy in Windows, Linux (Ubuntu/Debian), and macOS. It covers pre-requisites for the different platforms and actual steps to install NumPy on different platforms. It also covers alternative installation methods like using Anaconda, a cross-platform package distribution manager, and using virtual environment for effective dependency management. This article also addresses troubleshooting steps for resolving installation issues.

By following these steps, you should be able to install NumPy on Windows, Linux, and macOS, and begin using it for scientific computing and data analysis.

Code snippets and programs related to Example Program using NumPy, can be accessed from GitHub Repository. This GitHub repository all contains programs related to other topics in NumPy tutorial.

Related Topics

  • numpy.array(): Create NumPy Arrays using Lists, Tuples and Arrays (with Example Programs)
    NumPy (Numerical Python) is one of the most powerful libraries for numerical computations, data manipulation, and analysis in Python. At the core of NumPy is the ndarray, a multi-dimensional or N-dimensional array that allows to store and manipulate large datasets efficiently. NumPy provides several ways to create arrays, from simple ones to more complex multi-dimensional…
  • numpy.arange(): Create Array of Evenly Spaced Numbers within a Range (with Example Programs)
    NumPy library provides various functions to create arrays with evenly spaced numbers within range. In previous tutorials, we learned about Key Features of NumPy Arrays in Python. In this tutorial, we will learn to create NumPy arrays using numpy.arrange() function. numpy.arange() to Create Array of Evenly Spaced Numbers within a Range numpy.arange() is used to create…
  • NumPy Array in Python: First Step into Numerical Python
    NumPy array in Python are basic entities for numerical computations. They originate from the NumPy library, an essential package in Python for scientific computing. A NumPy array also termed as ndarray, is highly efficient and an object of multi-dimensional array (N-dimensional array), which provides the base functions of numerical operations. Being NumPy Arrays an essential…
  • NumPy Array Attributes | ndarray Attributes (with Example Programs)
    NumPy (Numerical Python) is a powerful library for numerical computing in Python. It provides support for large multidimensional arrays and matrices, and it also provides a collection of mathematical functions to operate on these arrays. Understanding the attributes of NumPy arrays is essential for efficiently working with them. In previous articles, we learned about NumPy…
  • np.logspace(): Create Array of Evenly Spaced Numbers on Logarithmic Scale (with Example Programs)
    NumPy is a powerful Python library for numerical computing, and np.logspace() is one of the powerful function to create array of evenly spaced numbers on logarithmic scale. In previous tutorials, we learned about Key Features of NumPy Arrays in Python. This tutorial will provide a step-by-step guide to understand how to use np.logspace() effectively, with examples.…

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

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