How to install NumPy on Ubuntu Linux?

NumPy means Numerical Python it adds support for multidimensional arrays, matrices and provides a large number of high-level mathematical functions. It is one of the most popular machine learning libraries for Python.

Many other libraries like TensorFlow uses NumPy internally for performing various operations. It is interactive and easy to use which makes complex mathematical problems simple.

In this article, I will discuss how to install and use NumPy on a Ubuntu system.

Prerequisites

To follow this guide you should have the following –

  • Access to a user account that has superuser privileges
  • Python 3 should be installed on your Ubuntu system

How to install NumPy on Ubuntu

There are multiple ways in which you can install NumPy a Ubuntu system. You can follow one of the given methods to install NumPy on your system.

Installing Python, Python pip, and Python venv packages

Before you install NumPy, first you need to install Python, Python pip, and Python venv packages on your Ubuntu system.

Use the following command to install the latest version of Python available in the Ubuntu repository –

sudo apt update
sudo apt install python3 -y

Next, use the following command to install Python pip –

sudo apt install python3-pip -y

You also need to install the python-venv package to create Python virtual environment.

The advantage of using virtual environments is that they provide isolation which means no environment interacts with another each of them works as an independent unit and its unusual changes do not damage other projects.

Use the following command to install python-venv package –

sudo apt install python3-venv -y

Method 1: Installing NumPy in Python virtual environment

First, create a project directory let’s say it is numpy-demo, make the directory with a meaningful name.

mkdir numpy-demo

Move to the created directory by using –

cd numpy-demo

Next, use the given command to create a python virtual environment –

python3 -m venv numpy-dev

Activate the virtual environment by using –

source numpy-dev/bin/activate

Now your prompt will change to a virtual environment as you can see in the image below.

numpy

Use the given command to install NumPy –

pip install numpy

You can verify the installation by using the given command –

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

If NumPy is successfully installed then it will display the given output in your terminal.

numpy version

If you want you can deactivate the virtual environment by using the given command –

deactivate

Method 2: Installing Numpy in a Ubuntu system using pip

If you want to install it on your Ubuntu system, not on the virtual environment that you have created then use the following command in your terminal –

pip3 install numpy

Once it gets installed, you can verify the installation by using the given command –

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

Method 3: Installing NumPy from Ubuntu repository

NumPy library is available in the official Ubuntu repository you can install it by using the given command in your terminal –

sudo apt install python3-numpy -y

Again you can check if it gets correctly installed or not by using –

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

If it displays no error that means NumPy has been set up successfully on your system.

Now if you have a query then write us in the comments below.

Leave a Comment