
Installing Python 3 and Setting Up a Programming Environment on Ubuntu 24
Ubuntu 24 LTS ships with Python 3.12 by default, but getting a proper development environment set up involves more than just using the pre-installed version. Whether you’re setting up a development server, preparing a production environment, or just getting your local Ubuntu machine ready for Python development, you’ll need to understand package management, virtual environments, and the various Python installation methods available. This guide walks you through the complete process of installing Python 3, setting up development tools, and configuring a robust programming environment that won’t break when system updates roll around.
Understanding Python Installation Options on Ubuntu 24
Ubuntu 24 comes with several ways to install and manage Python, each with distinct advantages and trade-offs. The system Python is located at /usr/bin/python3
and serves system tools, while development work typically requires additional flexibility.
Installation Method | Pros | Cons | Best For |
---|---|---|---|
System Package (apt) | Stable, integrated with system, security updates | Limited versions, dependency conflicts | Production servers, system scripts |
deadsnakes PPA | Multiple Python versions, Ubuntu-optimized | Third-party repository dependency | Development, testing multiple versions |
pyenv | Version switching, user-level control | Compilation time, build dependencies | Development environments, version testing |
Docker/Containers | Complete isolation, reproducible | Resource overhead, complexity | Microservices, deployment consistency |
Installing Python 3 and Essential Development Tools
Start by updating your package lists and installing the core Python development packages:
sudo apt update
sudo apt install python3 python3-pip python3-venv python3-dev build-essential
Verify the installation and check your Python version:
python3 --version
pip3 --version
which python3
For development work, you’ll also want these additional packages:
sudo apt install python3-setuptools python3-wheel python3-distutils
sudo apt install git curl wget software-properties-common apt-transport-https
If you need multiple Python versions, add the deadsnakes PPA:
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
sudo apt install python3.11 python3.11-venv python3.11-dev
sudo apt install python3.13 python3.13-venv python3.13-dev
Setting Up Virtual Environments
Virtual environments prevent dependency conflicts and keep your system Python clean. Ubuntu’s python3-venv module is the most reliable approach:
# Create a new virtual environment
python3 -m venv myproject-env
# Activate the environment
source myproject-env/bin/activate
# Verify you're in the virtual environment
which python
python --version
pip --version
Inside the virtual environment, you can install packages without sudo:
# Install packages in the virtual environment
pip install requests flask numpy pandas
pip install --upgrade pip
# Generate requirements file
pip freeze > requirements.txt
# Deactivate when done
deactivate
For more advanced virtual environment management, consider installing pipenv or poetry:
# Using pipenv
pip3 install --user pipenv
echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc
# Create and activate environment with pipenv
pipenv install requests
pipenv shell
Installing and Configuring pyenv for Version Management
pyenv allows you to install and switch between multiple Python versions easily. First, install the build dependencies:
sudo apt install make build-essential libssl-dev zlib1g-dev \
libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm \
libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev \
libffi-dev liblzma-dev
Install pyenv using the official installer:
curl https://pyenv.run | bash
Add pyenv to your shell configuration:
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc
source ~/.bashrc
Now you can install and manage multiple Python versions:
# List available Python versions
pyenv install --list
# Install specific versions
pyenv install 3.11.7
pyenv install 3.12.1
# List installed versions
pyenv versions
# Set global Python version
pyenv global 3.12.1
# Set local version for a project
cd /path/to/project
pyenv local 3.11.7
Development Environment Configuration
Configure pip to use a better package index and enable user installs by default. Create ~/.pip/pip.conf
:
mkdir -p ~/.pip
cat > ~/.pip/pip.conf << EOF
[global]
index-url = https://pypi.org/simple/
trusted-host = pypi.org
timeout = 60
user = true
[install]
user = true
EOF
Set up useful shell aliases for Python development in ~/.bashrc
:
echo 'alias python=python3' >> ~/.bashrc
echo 'alias pip=pip3' >> ~/.bashrc
echo 'alias py="python3"' >> ~/.bashrc
echo 'alias serve="python3 -m http.server"' >> ~/.bashrc
echo 'alias jsonpp="python3 -m json.tool"' >> ~/.bashrc
source ~/.bashrc
Install commonly used development tools:
pip3 install --user virtualenv virtualenvwrapper
pip3 install --user ipython jupyter notebook
pip3 install --user black flake8 pylint mypy
pip3 install --user pytest pytest-cov tox
IDE and Editor Setup
For VS Code, install the Python extension and configure it to work with virtual environments:
# Install VS Code if not already installed
wget -qO- https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > packages.microsoft.gpg
sudo install -o root -g root -m 644 packages.microsoft.gpg /etc/apt/trusted.gpg.d/
sudo sh -c 'echo "deb [arch=amd64,arm64,armhf signed-by=/etc/apt/trusted.gpg.d/packages.microsoft.gpg] https://packages.microsoft.com/repos/code stable main" > /etc/apt/sources.list.d/vscode.list'
sudo apt update
sudo apt install code
Create a VS Code settings file for Python projects (.vscode/settings.json
):
{
"python.defaultInterpreterPath": "./venv/bin/python",
"python.linting.enabled": true,
"python.linting.flake8Enabled": true,
"python.formatting.provider": "black",
"python.formatting.blackArgs": ["--line-length", "88"],
"python.testing.pytestEnabled": true,
"files.exclude": {
"**/__pycache__": true,
"**/*.pyc": true
}
}
Common Issues and Troubleshooting
Here are the most frequent problems you'll encounter and their solutions:
- pip install fails with permission errors: Use
pip install --user package_name
or work inside a virtual environment - python command not found: Ubuntu uses
python3
by default; create an alias or usepython3
explicitly - Virtual environment activation fails: Check that you're using
source venv/bin/activate
notbash venv/bin/activate
- Package conflicts: Use separate virtual environments for different projects
- SSL certificate errors with pip: Update certificates with
sudo apt update && sudo apt install ca-certificates
If you encounter build failures when installing packages, install additional development headers:
sudo apt install python3-dev libpq-dev libxml2-dev libxslt1-dev libldap2-dev libsasl2-dev libffi-dev
Performance Optimization and Best Practices
Configure pip to use parallel downloads and caching:
pip config set global.cache-dir ~/.cache/pip
pip config set install.parallel-downloads 10
Use pip-tools for better dependency management:
pip install pip-tools
echo "requests>=2.25.0" > requirements.in
pip-compile requirements.in
pip-sync requirements.txt
Set up pre-commit hooks for code quality:
pip install pre-commit
cat > .pre-commit-config.yaml << EOF
repos:
- repo: https://github.com/psf/black
rev: 22.3.0
hooks:
- id: black
- repo: https://github.com/pycqa/flake8
rev: 4.0.1
hooks:
- id: flake8
EOF
pre-commit install
Real-World Use Cases and Examples
Here's a complete setup script for a Django development environment:
#!/bin/bash
# Django development environment setup
# Create project directory
mkdir mydjango-project && cd mydjango-project
# Create virtual environment
python3 -m venv venv
source venv/bin/activate
# Install Django and development tools
pip install django==4.2 psycopg2-binary python-decouple
pip install black flake8 django-debug-toolbar
# Create Django project
django-admin startproject myproject .
python manage.py migrate
# Create requirements file
pip freeze > requirements.txt
echo "Django development environment ready!"
echo "Activate with: source venv/bin/activate"
echo "Run server with: python manage.py runserver"
For data science work, create a specialized environment:
# Data science environment
python3 -m venv datascience-env
source datascience-env/bin/activate
pip install numpy pandas matplotlib seaborn
pip install jupyter scikit-learn scipy
pip install plotly dash streamlit
# Start Jupyter notebook
jupyter notebook --ip=0.0.0.0 --port=8888 --no-browser
The Python development environment on Ubuntu 24 provides excellent stability and performance. With proper virtual environment usage and package management, you'll avoid the common pitfalls that plague Python installations. The combination of system packages for stability and user-level tools for flexibility gives you the best of both worlds.
For additional resources, check the official Python virtual environment documentation and the Python Packaging Authority guide for more advanced package management techniques.

This article incorporates information and material from various online sources. We acknowledge and appreciate the work of all original authors, publishers, and websites. While every effort has been made to appropriately credit the source material, any unintentional oversight or omission does not constitute a copyright infringement. All trademarks, logos, and images mentioned are the property of their respective owners. If you believe that any content used in this article infringes upon your copyright, please contact us immediately for review and prompt action.
This article is intended for informational and educational purposes only and does not infringe on the rights of the copyright owners. If any copyrighted material has been used without proper credit or in violation of copyright laws, it is unintentional and we will rectify it promptly upon notification. Please note that the republishing, redistribution, or reproduction of part or all of the contents in any form is prohibited without express written permission from the author and website owner. For permissions or further inquiries, please contact us.