DEV Community

Cover image for NumPy on Ubuntu: From Zero to First Arrays
Anas Hussain
Anas Hussain

Posted on

NumPy on Ubuntu: From Zero to First Arrays

Why this post?

Today I set up a clean Python workspace on Ubuntu and ran my first NumPy code. I wrote down every step so you (or future‑me) can repeat it in minutes.


Table of Contents

  1. Prerequisites
  2. Install Python and pip
  3. Create a Virtual Environment
  4. Install NumPy and Jupyter
  5. Launch Jupyter Notebook
  6. Your First NumPy Operations
  7. Shut Down & Deactivate
  8. Common Pitfalls & Fixes
  9. What’s Next

Prerequisites

  • Ubuntu (works on 20.04/22.04/24.04+)
  • Terminal access
  • Basic comfort running shell commands

Tip: Commands with $ go in your terminal. Don’t copy the $ itself.


Install Python and pip

Ubuntu ships with Python 3, but let’s make sure the essentials are installed and up‑to‑date.

$ sudo apt update && sudo apt install -y python3 python3-pip python3-venv
$ python3 --version
$ pip3 --version
Enter fullscreen mode Exit fullscreen mode

Create a Virtual Environment

Make a dedicated folder for your NumPy learning so packages don’t leak into system Python.

$ mkdir -p ~/numpy-learning && cd ~/numpy-learning
$ python3 -m venv numpy-venv
$ source numpy-venv/bin/activate
(numpy-venv) $ which python
Enter fullscreen mode Exit fullscreen mode

You should see something like /home/you/numpy-learning/numpy-venv/bin/python. The (numpy-venv) prefix means the environment is active.

Why venv? Each project gets its own clean set of packages. No conflicts.


Install NumPy and Jupyter

First, bring pip up to date, then install the tools we need:

(numpy-venv) $ python -m pip install --upgrade pip
(numpy-venv) $ pip install numpy jupyter
Enter fullscreen mode Exit fullscreen mode

Optional but handy for data work:

(numpy-venv) $ pip install matplotlib pandas
Enter fullscreen mode Exit fullscreen mode

Verify installs:

(numpy-venv) $ python -c "import numpy as np; print(np.__version__)"
(numpy-venv) $ jupyter --version
Enter fullscreen mode Exit fullscreen mode

Launch Jupyter Notebook

Start the notebook server. Your browser will open automatically (or you’ll get a URL with a token to paste into your browser):

(numpy-venv) $ jupyter notebook
Enter fullscreen mode Exit fullscreen mode

In the Jupyter UI:

  1. Click New → Python 3 (ipykernel).
  2. Rename it to numpy_basics.ipynb (click the title at the top).

Take a look into my Jupyter UI


Your First NumPy Operations

Create a few cells and run the following. (Click a cell and press Shift+Enter to run.)

Cell 1 — Import & version

import numpy as np
print("NumPy version:", np.__version__)
Enter fullscreen mode Exit fullscreen mode

Cell 2 — Create arrays

# 1D and 2D arrays
a = np.array([1, 2, 3, 4, 5])
b = np.array([[1, 2, 3],
              [4, 5, 6]])
print("a:", a)
print("b:\n", b)
Enter fullscreen mode Exit fullscreen mode

Cell 3 — Properties

print("a.shape:", a.shape, "a.dtype:", a.dtype)
print("b.shape:", b.shape, "b.ndim:", b.ndim, "b.size:", b.size)
Enter fullscreen mode Exit fullscreen mode

Cell 4 — Indexing & slicing

print("First of a:", a[0])
print("Last two of a:", a[-2:])
print("b second column:", b[:, 1])
Enter fullscreen mode Exit fullscreen mode

Cell 5 — Vectorized operations

print("a * 2:", a * 2)
print("a + 10:", a + 10)
print("a ** 2:", a ** 2)
Enter fullscreen mode Exit fullscreen mode

Cell 6 — Broadcasting

row = np.array([10, 20, 30])
print("b + row:\n", b + row)
Enter fullscreen mode Exit fullscreen mode

Cell 7 — Aggregations

arr = np.array([10, 20, 30, 40, 50])
print("sum:", np.sum(arr))
print("mean:", np.mean(arr))
print("std:", np.std(arr))
print("min:", np.min(arr), "max:", np.max(arr))
Enter fullscreen mode Exit fullscreen mode

Cell 8 — Reshape

reshaped = b.reshape(3, 2)
print("reshaped b (3x2):\n", reshaped)
Enter fullscreen mode Exit fullscreen mode

That’s it — your first NumPy session is done!


Shut Down & Deactivate

When you’re finished for the day:

  • In the Jupyter tab: File → Save and Checkpoint.
  • In the terminal where Jupyter is running: press Ctrl + C and confirm to stop the server.
  • Deactivate your virtual environment:
(numpy-venv) $ deactivate
Enter fullscreen mode Exit fullscreen mode

Next time, come back with:

$ cd ~/numpy-learning
$ source numpy-venv/bin/activate
(numpy-venv) $ jupyter notebook
Enter fullscreen mode Exit fullscreen mode

Common Pitfalls & Fixes

pip installs to the wrong place

Always use python -m pip ... inside your venv to be explicit.

(numpy-venv) $ python -m pip install numpy jupyter
Enter fullscreen mode Exit fullscreen mode

jupyter: command not found after install

Make sure your venv is active (look for (numpy-venv)), then reinstall:

$ source numpy-venv/bin/activate
(numpy-venv) $ python -m pip install --upgrade pip jupyter
Enter fullscreen mode Exit fullscreen mode

❌ Mixing system Python and venv Python

Check which Python is running:

(numpy-venv) $ which python
Enter fullscreen mode Exit fullscreen mode

It should point to your project’s numpy-venv folder.

❌ Permission errors

Avoid sudo pip. Use a venv. If needed, upgrade pip:

(numpy-venv) $ python -m pip install --upgrade pip
Enter fullscreen mode Exit fullscreen mode

What’s Next

  • Try JupyterLab for a modern UI:
  (numpy-venv) $ pip install jupyterlab
  (numpy-venv) $ jupyter lab
Enter fullscreen mode Exit fullscreen mode
  • Explore: np.arange, np.linspace, np.random.rand, np.vstack, np.hstack.
  • Add matplotlib and plot your arrays.
  • Day 2 topics: boolean masking, fancy indexing, broadcasting tricks, and small exercises.

If this helped, drop a comment or share what you built with NumPy today! 💡🚀

Top comments (0)