How to Set Up a Python Research Environment on Ubuntu for Cosmology and Astrophysics (Step-by-Step Guide)
If you're a physics student interested in cosmology or astrophysics, installing Python is only the beginning. A proper research environment should be clean, organized, and easy to maintain as your projects grow more advanced — and getting this right early will save you hours of frustration later.
When I started preparing my Ubuntu laptop for scientific computing, I quickly realized that simply installing Python wasn't enough. I needed tools for numerical computation, symbolic mathematics, data analysis, visualization, and interactive notebooks — all while keeping project dependencies isolated.
This guide documents the exact setup I used to build my Python research environment for cosmology and astrophysics work. If you're just starting out, you can follow along step by step.
Why Ubuntu for Scientific Computing?
Ubuntu is one of the most widely used operating systems in research and scientific computing. It offers a stable environment, powerful terminal tools, excellent package management, and broad compatibility with scientific libraries.
Combined with Python, Ubuntu becomes an excellent platform for learning and conducting research in physics, astronomy, and cosmology.
Why Python for Cosmology and Astrophysics?
Python has become the standard language in scientific research because of its simplicity and extensive ecosystem. Some of the libraries every physics student should know include:
- NumPy — Numerical computing and array operations
- SciPy — Scientific algorithms and numerical methods
- Matplotlib — Data visualization and plotting
- Pandas — Working with structured datasets
- SymPy — Symbolic mathematics and equation solving
- Astropy — Astronomy and astrophysics tools
- Jupyter Notebook — Interactive coding, documentation, and experimentation
Together, these libraries provide everything you need to begin computational work in cosmology and astrophysics.
Step 1: Check Your Python Installation
Ubuntu usually comes with Python pre-installed. Open the Terminal and run:
python3 --version
If Python is installed, you'll see the version number. Next, check whether pip is available:
pip3 --version
If pip isn't installed, install it before proceeding.
Step 2: Understanding the "Externally Managed Environment" Error
When I tried installing packages globally using pip, Ubuntu displayed an externally-managed-environment error.
This happens because newer Ubuntu versions protect the system Python installation. Installing packages globally with pip can interfere with operating system packages.
The recommended solution is to use a virtual environment, where each project has its own isolated Python installation.
Step 3: Install Virtual Environment Support
Install the required package:
sudo apt install python3-venv
This allows Python to create isolated environments for different projects.
Step 4: Create a Project Workspace
Create a folder for your Python projects:
mkdir ~/python-projects
cd ~/python-projects
Keeping all your projects inside one directory makes them much easier to organize as they grow.
Step 5: Create a Virtual Environment
Create a virtual environment:
python3 -m venv myenv
Activate it:
source myenv/bin/activate
Once activated, your terminal prompt changes to show the environment name. This confirms that all packages installed from this point onward remain isolated from the system Python.
Step 6: Install Scientific Python Libraries
Inside the activated environment, install the required scientific packages:
pip install numpy scipy matplotlib pandas jupyter sympy astropy
These libraries form the foundation of most scientific Python workflows in physics and astronomy.
Step 7: Verify the Installation
Check that NumPy was installed correctly:
python -c "import numpy; print(numpy.__version__)"
If a version number appears, the installation was successful.
Step 8: Set Up Visual Studio Code
Visual Studio Code provides an excellent development environment for Python. Open your project folder:
code .
Install the following extensions:
- Python
- Pylance
- Jupyter
Then select the Python interpreter from your virtual environment. Using the correct interpreter ensures VS Code can access all your installed scientific libraries.
Step 9: Working with Jupyter Notebook
Jupyter Notebook is ideal for scientific research because it combines code, equations, plots, and notes in one place.
Create your first notebook, for example first_notebook.ipynb, and select the kernel from your virtual environment before running any code. This ensures your notebook uses the same packages you installed earlier.
Step 10: Test the Environment
Run the following code inside Jupyter Notebook:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 10, 100)
y = np.sin(x)
plt.plot(x, y)
plt.xlabel("x")
plt.ylabel("sin(x)")
plt.title("My First Scientific Plot")
plt.show()
If a graph appears, your scientific Python environment is working correctly.
My Recommended Project Structure
As your projects grow, keeping them organized becomes essential:
python-projects/
│
├── myenv/
├── notebooks/
├── scripts/
├── data/
├── figures/
└── README.md
A clean project structure makes research easier to manage and reproduce — a habit that pays off enormously once you start working with real astronomical datasets.
Common Problems and Fixes
pip command not found
Install pip first before installing packages.
ModuleNotFoundError
Make sure your virtual environment is activated before running your scripts.
Packages work in Terminal but not VS Code
Check that VS Code is using the Python interpreter from your virtual environment, not the system one.
Jupyter cannot find the libraries
Select the notebook kernel from the virtual environment instead of the system Python.
What I Learned
The biggest lesson from this setup was that the best research environment isn't the most complicated one. It's the one that is:
- Clean
- Organized
- Reproducible
- Easy to maintain
Using virtual environments prevents dependency conflicts, VS Code improves productivity, and Jupyter Notebook makes experimentation far easier. Together, these tools form a solid foundation for anyone interested in computational physics, cosmology, or astrophysics.
What's Next?
Now that my development environment is ready, my next goals are:
- Learn NumPy for numerical computing
- Explore scientific visualization with Matplotlib
- Practice symbolic mathematics using SymPy
- Study Astropy for astronomy-specific applications
- Build small computational physics projects
- Prepare for more advanced cosmology research
This setup marks the beginning of my journey into scientific computing. I hope it helps other students preparing for research in physics, astronomy, and cosmology.
Have you set up your own Python environment for physics or astronomy work? Let me know in the comments what tools you're using — and what you'd like me to cover next in this series.
Lets's Decode The Cosmos Together 💜

Comments
Post a Comment
Kindly keep comments respectful and related to the topic