How to Get Started with Python Development on Ubuntu

How to Get Started with Python Development on Ubuntu


Ubuntu provides an excellent development environment for working with Python. With its simplified package management and availability of libraries, developing Python applications on Ubuntu is quick and easy.

In this detailed guide, we’ll walk through setting up a Python development on Ubuntu from scratch. We’ll cover:

  • Installing Python and creating virtual environments
  • Setting up a code editor like VS Code
  • Managing packages with pip
  • Setting up Flask and Django web frameworks
  • Testing and debugging tools like pytest and pdb
  • Resources for learning Python and its ecosystems

Follow along as we equip your Ubuntu system into an ideal Python coding workspace. Let’s get started!

Let’s get start Python Development on Ubuntu

Installing Python on Ubuntu

Since Ubuntu includes Python by default, you likely already have a working Python interpreter.

Check the version:

python3 --version
# Python 3.8.10

If you need a specific Python version, use the pyenv tool to install and manage multiple versions.

First install pyenv:

sudo apt install pyenv

Then install the desired Python version, say 3.9.6:

pyenv install 3.9.6

Set this as the global version:

pyenv global 3.9.6

Pyenv allows seamlessly switching between Python versions on Ubuntu for testing compatibility.

Setting up a Virtual Environment

It’s best practice to isolate projects into virtual environments instead of installing packages globally. We’ll use the built-in venv module.

Create a project folder and a venv:

mkdir myproject
cd myproject
python3 -m venv .venv

This will create the .venv environment. Next, activate it:

source .venv/bin/activate

Installed packages will now reside within the project instead of globally. Be sure to add the .venv folder to your .gitignore file.

Virtual environments keep dependencies organized and portable.

Choosing a Code Editor

Any basic text editor works for writing Python. But using an editor with Python support provides a much better experience.

VS Code is an excellent free code editor for Python with syntax highlighting, auto-completion, linting, debugging, and support for extensions:

sudo snap install --classic code

Install the Python extension for enhanced functionality including code formatting with black and pylint linting.

Other great editors include PyCharm and Atom.

Managing Packages with Pip

The pip package manager allows installing and managing Python packages from the extensive Python Package Index (PyPI).

Ensure pip is up to date:

pip install --upgrade pip

Install packages as needed:

pip install pandas numpy requests

List installed packages:

pip list

Create requirements.txt to track dependencies:

pip freeze > requirements.txt

Pip simplifies acquiring and managing packages for your projects.

Using Flask for Web Development

Flask is a lightweight Python web framework great for building web APIs, sites, and REST services.

Install Flask:

pip install Flask

A simple Flask app:

from flask import Flask
app = Flask(__name__)

def home():
    return "Hello, Flask!"

if __name__ == "__main__":

Run the server:


Flask has minimal boilerplate, offers great flexibility, and encourages clean code. It’s a pleasure to use.

Using Django Web Framework

For more robust web apps, Django provides an all-in-one framework with an ORM, admin interface, authentication, and more out of the box.


pip install Django

Bootstrap a project:

django-admin startproject myproject

This generates the initial Django project structure including for running commands.

Start the development server:

python runserver

Django is ideal for quickly building full-featured, secure sites backed by SQL databases like PostgreSQL.

Testing Code with Pytest

Pytest is the most popular Python testing framework with its simple API and ability to run unit and functional tests.


pip install pytest

Tests are written as functions starting with test_:

import app

def test_add():
  assert app.add(1, 2) == 3

def test_subtract():
  assert app.subtract(4, 2) == 2

Run tests:


Pytest makes writing tests easy and is highly extensible via plugins.

Debugging with pdb

pdb is Python’s built-in interactive debugger.

Place import pdb; pdb.set_trace() where you want execution to pause:

def divide(a, b):
  import pdb; pdb.set_trace()
  return a / b

divide(5, 0)

This will open an interactive pdb session allowing inspection of variables and step-by-step execution.

pdb is fantastic for quickly diagnosing runtime issues during development.

Learning Python Programming

Some great resources for learning Python:

Start easy and work your way up to advanced topics like concurrency, metaprogramming, and package development. Always try out concepts by coding!


Ubuntu provides an excellent starting point for Python development. By following this guide, you’ll be able to:

  • Install and manage multiple Python versions
  • Set up clean virtual environments for projects
  • Install and use popular packages from PyPI
  • Build Python web apps with Flask and Django
  • Write tests with pytest and debug with pdb
  • Use Git and Github for version control and collaboration

You’ll also have an array of resources to continuously build your Python skills.

Ubuntu’s vast package index and ease of use makes it the perfect OS for developing in Python. You’re now ready to code the next big thing in Python on Ubuntu!

Frequently Asked Questions

Here are some common questions about Python development on Ubuntu:

Q: Is Python preinstalled on Ubuntu?

A: Yes, Ubuntu comes preinstalled with Python 3. You can check the version by running python3 --version.

Q: Should I use apt or pip to install Python packages?

A: Prefer pip over apt. Pip installs packages from PyPI for the specific version of Python in your environment.

Q: How do I install multiple versions of Python on Ubuntu?

A: Use the pyenv tool to install and manage multiple Python versions. Then switch between them as needed.

Q: Is Django or Flask better for web development?

A: Django offers more batteries included with its ORM, admin etc. Flask is more minimal – pick based on the scope of your app.

Q: How can I ensure repeatable installations for Python projects?

A: Use virtual environments to isolate dependencies. Also, create a requirements.txt with pinned versions for each project.

Q: What IDEs are recommended for Python on Ubuntu?

A: Great options include VS Code, PyCharm, Atom, and Spyder. Pick one with Python integration.

Leave a Reply

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