Linux Ubuntu, a cornerstone of the open-source world and a popular choice for developers, relies heavily on Python for a vast array of system tasks and applications. From scripting administrative functions to powering complex machine learning algorithms, Python’s versatility makes its installation and management a critical skill for any Ubuntu user. This guide will walk you through the process of installing and managing Python on your Ubuntu system, ensuring you have the right tools for your projects.
Understanding Python Versions on Ubuntu
Ubuntu, by default, often comes with a pre-installed version of Python. However, this default version might not always be the latest or the one required for your specific development needs. Managing multiple Python versions is a common requirement, especially when working on projects that depend on different Python interpreter versions or specific libraries that are not compatible across versions.

Checking for Pre-installed Python
Before proceeding with any installation, it’s prudent to check which versions of Python are already present on your system. Open your terminal and execute the following commands:
python --version
python3 --version
These commands will display the default python (often Python 2, though increasingly Python 3) and python3 versions installed. If these commands return an error, it indicates that Python is not currently installed, or at least not configured in your system’s PATH.
The Importance of Python 3
While Python 2 was widely used for many years, it reached its end-of-life in January 2020. Modern development exclusively favors Python 3. Ubuntu systems increasingly default to Python 3, and it’s the recommended version for all new projects. Ensure you are working with Python 3 for optimal compatibility and access to the latest features and security updates.
Managing Multiple Python Versions
For developers needing to switch between different Python versions (e.g., for testing compatibility or working with legacy code), tools like pyenv are invaluable. However, for most users, installing a specific Python 3 version directly from the Ubuntu repositories or compiling from source is sufficient. This guide will focus on the most common and straightforward methods.
Installing Python 3 from Ubuntu Repositories
The simplest and most recommended method for installing Python 3 on Ubuntu is by leveraging its Advanced Packaging Tool (APT). APT is Ubuntu’s package manager, which simplifies the process of installing, updating, and removing software.
Updating Package Lists
Before installing any new software, it’s good practice to update your local package index. This ensures that you have access to the latest available versions of packages. Open your terminal and run:
sudo apt update
This command fetches the latest package information from the configured software repositories.
Installing the Latest Python 3
To install the latest stable version of Python 3 available in the Ubuntu repositories, use the following command:
sudo apt install python3
This command will download and install Python 3 along with its core modules and standard libraries. If you wish to install development headers and libraries (which are often necessary for compiling Python modules from source or for certain development tools), you can install the python3-dev package:
sudo apt install python3-dev
To install pip, the Python package installer, which is essential for managing third-party Python libraries, run:
sudo apt install python3-pip
pip allows you to easily install and manage packages from the Python Package Index (PyPI). For instance, to install a package like requests, you would later use pip install requests.
Verifying the Installation
After the installation is complete, verify that Python 3 and pip have been installed correctly by checking their versions:
python3 --version
pip3 --version
You should see the version numbers printed in the terminal.
Installing Specific Python 3 Versions with PPA
Sometimes, the version of Python 3 available in the default Ubuntu repositories might not be the very latest release or a specific version you require. In such cases, you can use a Personal Package Archive (PPA). PPAs are repositories maintained by individuals or groups that can provide newer or alternative versions of software not found in the official Ubuntu repositories.
Adding a PPA for Python
A popular and reliable PPA for Python versions is maintained by the “deadsnakes” team. To add this PPA to your system, open your terminal and execute:
sudo add-apt-repository ppa:deadsnakes/ppa
You will be prompted to confirm adding the PPA. Press Enter to proceed.
Updating Package Lists After Adding PPA
After adding a new repository, it’s crucial to update your package lists again to include the packages from the newly added PPA:
sudo apt update
Installing a Specific Python 3 Version
Now you can install a specific version of Python 3. For example, to install Python 3.11, you would use:
sudo apt install python3.11
To install the corresponding development headers and pip for this version:
sudo apt install python3.11-dev python3.11-venv python3.11-distutils
Note: python3.11-distutils might be required for pip to function correctly with some older package installations. python3.11-venv is for creating virtual environments, which is highly recommended.
Setting the Default Python 3 Version (Optional)
If you’ve installed multiple Python 3 versions and wish to set a specific one as the default python3 command, you can use the update-alternatives command.
First, list the available Python 3 alternatives:
update-alternatives --list python3
If the version you want to set as default is not listed, you’ll need to add it:
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.11 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 2
The last number (e.g., 1, 2) is the priority. Higher numbers indicate higher priority.
Then, configure which version to use:

sudo update-alternatives --config python3
This will present you with a menu to choose the default Python 3 version. Select the number corresponding to your desired version and press Enter.
Remember that changing the system’s default python3 can have implications for system scripts that rely on a specific version. It’s often safer to use virtual environments or explicitly call the desired Python version (e.g., python3.11 your_script.py).
Using Virtual Environments
Virtual environments are a fundamental best practice in Python development. They allow you to create isolated Python environments for different projects, each with its own set of installed packages and Python version. This prevents package conflicts and ensures that your project dependencies don’t interfere with each other or with the system’s global Python installation.
What are Virtual Environments?
A virtual environment is a self-contained directory tree that includes a Python installation for a particular version of Python, plus a number of additional packages.
Creating a Virtual Environment
Python 3 comes with the venv module built-in, which makes creating virtual environments straightforward.
Navigate to your project directory in the terminal:
cd /path/to/your/project
Then, create a virtual environment. It’s common practice to name the environment directory .venv or env:
python3 -m venv .venv
This command will create a .venv directory within your project folder, containing a copy of the Python interpreter and the necessary files to manage packages.
Activating a Virtual Environment
To start using the virtual environment, you need to activate it. The activation command differs slightly between Linux/macOS and Windows. For Ubuntu:
source .venv/bin/activate
Once activated, your terminal prompt will typically change to indicate that you are within the virtual environment (e.g., (.venv) user@hostname:~/your/project$).
While the environment is active:
- The
pythoncommand will refer to the Python interpreter within your virtual environment. - The
pipcommand will install packages only into this virtual environment.
Deactivating a Virtual Environment
When you’re finished working in your virtual environment, you can deactivate it by simply typing:
deactivate
Your terminal prompt will return to its normal state, and the python and pip commands will revert to using the system’s default versions.
Managing Packages within a Virtual Environment
With your virtual environment activated, you can now use pip to install packages specific to your project.
To install a package:
pip install package_name
To install packages from a requirements.txt file:
pip install -r requirements.txt
To freeze the current environment’s packages into a requirements.txt file:
pip freeze > requirements.txt
This requirements.txt file is essential for reproducibility, allowing others (or yourself on a different machine) to easily set up the same development environment.
Installing Python from Source (Advanced)
While generally not recommended for most users due to its complexity and potential for system instability if not done carefully, compiling Python from source offers the ultimate control over the installation process, allowing you to configure specific build options or install bleeding-edge versions not yet available through any repository.
Downloading the Source Code
First, you need to download the Python source code. Visit the official Python website (python.org) and navigate to the downloads section to find the latest or desired version. You can also use wget directly in the terminal if you know the download URL. For example, for Python 3.12.0:
wget https://www.python.org/ftp/python/3.12.0/Python-3.12.0.tgz
Extracting the Archive
Once downloaded, extract the archive:
tar -xf Python-3.12.0.tgz
cd Python-3.12.0
Configuring and Compiling
Before compiling, you need to install essential build dependencies:
sudo apt update
sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev wget
Now, configure the build. The --enable-optimizations flag enables profile-guided optimizations, which can improve performance but will increase compilation time. The --prefix option specifies the installation directory; installing to /usr/local is a common choice for locally compiled software.
./configure --enable-optimizations --prefix=/usr/local
After configuration, compile the source code. The make -j $(nproc) command uses all available CPU cores (nproc) to speed up the compilation process.
make -j $(nproc)
Finally, install the compiled Python. Using altinstall is crucial to avoid overwriting the system’s default python3 executable. altinstall installs the executable as python3.12 (or the version number), preventing conflicts.
sudo make altinstall

Verifying the Source Installation
Check the version of the newly installed Python:
/usr/local/bin/python3.12 --version
This method provides a highly customized Python installation, but it requires more effort to manage and update compared to using APT or PPAs. It’s a path best taken when specific build configurations are absolutely necessary.
