This content originally appeared on DEV Community and was authored by Anas Hussain
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
- Prerequisites
- Install Python and pip
- Create a Virtual Environment
- Install NumPy and Jupyter
- Launch Jupyter Notebook
- Your First NumPy Operations
- Shut Down & Deactivate
- Common Pitfalls & Fixes
- 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
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
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
Optional but handy for data work:
(numpy-venv) $ pip install matplotlib pandas
Verify installs:
(numpy-venv) $ python -c "import numpy as np; print(np.__version__)"
(numpy-venv) $ jupyter --version
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
In the Jupyter UI:
- Click New → Python 3 (ipykernel).
- Rename it to
numpy_basics.ipynb
(click the title at the top).
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__)
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)
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)
Cell 4 — Indexing & slicing
print("First of a:", a[0])
print("Last two of a:", a[-2:])
print("b second column:", b[:, 1])
Cell 5 — Vectorized operations
print("a * 2:", a * 2)
print("a + 10:", a + 10)
print("a ** 2:", a ** 2)
Cell 6 — Broadcasting
row = np.array([10, 20, 30])
print("b + row:\n", b + row)
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))
Cell 8 — Reshape
reshaped = b.reshape(3, 2)
print("reshaped b (3x2):\n", reshaped)
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
Next time, come back with:
$ cd ~/numpy-learning
$ source numpy-venv/bin/activate
(numpy-venv) $ jupyter notebook
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
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
Mixing system Python and venv Python
Check which Python is running:
(numpy-venv) $ which python
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
What’s Next
- Try JupyterLab for a modern UI:
(numpy-venv) $ pip install jupyterlab
(numpy-venv) $ jupyter lab
- 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!
This content originally appeared on DEV Community and was authored by Anas Hussain