Ubuntu Python Install

You are currently viewing Ubuntu Python Install

In today’s technological landscape, Python stands out as one of the most versatile and widely used programming languages. Whether you’re into web development, data science, automation, or just about any tech-related field, the chances are high that you’ll encounter Python. This post is designed to guide you through the installation of Python on an Ubuntu system. We’ll explore the various methods, nuances, and best practices to ensure you have a smooth experience. By the end, you’ll be well-equipped to start your Python journey on Ubuntu.


Before diving into the installation process, let’s establish some groundwork:

Basic Knowledge:

  • Ubuntu Command Line: Familiarity with basic Ubuntu terminal commands will be beneficial. While we’ll walk you through each step, understanding commands like sudo, apt-get, and navigating directories can be advantageous.
  • Python Basics: While not essential for installation, if you’re new to Python, having a basic understanding of its syntax and purpose will help you test and use it post-installation.

System Requirements:

  • Operating System: This guide is tailored for Ubuntu. Most of the instructions will work for its derivatives as well, but it’s always a good idea to refer to the official documentation if you’re on a different distribution.
  • Disk Space: While Python itself isn’t very large, ensure you have at least 100MB of free space for the standard installation. If you plan to install additional packages or libraries later, consider having more space available.

Understanding Python Versions

Python has evolved significantly since its inception. Presently, two primary versions of Python might come to mind: Python 2 and Python 3.

  • Python 2: Released in 2000, Python 2 became very popular and was widely used for many years. However, as of January 1, 2020, Python 2 reached its end of life, meaning it won’t receive updates, not even security patches. While some legacy systems and projects might still use Python 2, it’s advisable to use Python 3 for new projects.
  • Python 3: Released in 2008, Python 3 is not backward compatible with Python 2. This was a bold step taken to rectify fundamental design flaws. As of now, Python 3 is under active development, receives regular updates, and is the recommended version for all purposes.

It’s crucial to be aware of these versions because they co-exist, and certain tools or libraries might only be compatible with one of them. For the scope of this guide and best practices moving forward, we will focus on installing and working with Python 3.

Checking Existing Python Installation

Before attempting to install Python, it’s always a good practice to check if it’s already present on your system. Ubuntu often comes with Python pre-installed.

Check for Python 3: Open a terminal and type:

python3 --version

If Python 3 is installed, this command will display the version number. For instance, “Python 3.8.5” indicates you have Python 3.8.5 installed.

Check for Python 2 (optional):

python --version

This command will typically indicate the version of Python 2 if installed.

Installing Python via APT (Recommended)

The easiest and most straightforward way to install Python on Ubuntu is by using the Advanced Package Tool (APT), Ubuntu’s package manager.

Update Package List: First, ensure your package list is updated:

sudo apt update

Install Python 3: To install Python 3 and its essentials:

sudo apt install python3 python3-pip python3-venv

This command installs Python 3, pip (Python’s package manager), and venv (tool for creating isolated Python environments).

Once the installation is complete, you can again use python3 --version to verify.

Installing Python from Source

For those who need a specific Python version or want the latest release not available in Ubuntu’s repositories, installing from source might be the way to go. However, this method is more involved.

Get Prerequisites: Before downloading the source, ensure you have the essential build tools:

sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev wget

Download Python Source: Visit Python’s official download page to find the URL of the version you want. Then, use wget to download it. For example, for Python 3.9.0:

wget https://www.python.org/ftp/python/3.9.0/Python-3.9.0.tgz

Extract and Compile:

tar -xf Python-3.9.0.tgz cd Python-3.9.0 ./configure --enable-optimizations make sudo make install

Once installation is completed, you can use python3.9 --version to check the installation.

Setting up a Virtual Environment

Virtual environments in Python allow you to create isolated spaces on your computer for Python projects, ensuring that each project can have its dependencies, irrespective of what dependencies every other project has.

Why Use Virtual Environments?

  • Isolation: Keep your project dependencies separated. If one project needs version 1 of a library and another project needs version 2, they can coexist in separate environments.
  • Cleanliness: If you ever decide to delete a project, you can easily remove its environment and be assured that no leftover dependencies remain.

Creating a Virtual Environment:

python3 -m venv my_project_env

Replace my_project_env with your desired environment name.

Activating the Virtual Environment:

source my_project_env/bin/activate

Once activated, your command prompt will show the name of the activated environment.

To deactivate and return to the global Python environment, simply type:


Testing Your Python Installation

Once Python is installed, it’s essential to ensure everything is working as expected.

Interactive Python Shell: Enter the Python interactive shell by typing:


Here, you can execute Python commands directly. For instance, print("Hello, Ubuntu!") should display the message. To exit, type exit().

Creating and Running a Python Script: Create a new Python file named test.py:

echo 'print("Python is successfully installed!")' > test.py

Run the script with:

python3 test.py

If you see the message “Python is successfully installed!”, you’re good to go!

Upgrading and Managing Python Versions

As you continue your Python journey, you’ll encounter times when you need to upgrade Python or manage multiple versions.

Upgrading Python via APT: To upgrade the Python version available in Ubuntu’s repositories:

sudo apt update sudo apt upgrade python3

Managing Multiple Python Versions with pyenv: If you need to switch between Python versions regularly, consider using pyenv.

Install pyenv:

sudo apt install -y build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev 
curl https://pyenv.run | bash

Add pyenv to $PATH: Add the following lines to your ~/.bashrc, ~/.zshrc, or whichever shell you’re using:

export PATH="$HOME/.pyenv/bin:$PATH" 
eval "$(pyenv init --path)" 
eval "$(pyenv virtualenv-init -)"

Installing a Specific Python Version:bashCopy codepyenv install 3.9.0

Setting the Python Version:

For global setting: pyenv global 3.9.0

For local (per-project) setting: pyenv local 3.9.0

Common Troubleshooting Tips

Even with a straightforward installation process, you might occasionally run into issues. Here are some common problems and their solutions:

  1. Command 'python3' not found:
    • If you encounter this after installation, it’s likely the path to the Python executable isn’t in your system’s PATH variable. Double-check the installation steps and ensure that the necessary symlinks are established.
  2. pip not working or module not found errors:
    • This can be a PATH issue or a result of using a system version of pip instead of the one related to your virtual environment. Always activate your virtual environment before using pip.
  3. Permission Errors:
    • If you’re getting permission-denied errors, it may be because you’re trying to install a package system-wide, which requires root privileges. Either use a virtual environment or use sudo with caution for system-wide installations.
  4. Cannot Upgrade System Python:
    • Avoid using sudo apt upgrade python3 to upgrade the system Python directly, as it can mess with system utilities that depend on a specific Python version. If you need a newer version, consider using pyenv.
  5. Conflicting Python Versions:
    • If you have multiple versions of Python and they seem to conflict, tools like pyenv can help manage and switch between them seamlessly.

Remember, when encountering an issue, the error message is your friend. Often, a quick web search of the error message can lead to a solution.


Congratulations! You’ve successfully navigated the process of installing Python on Ubuntu, understanding its different versions, and diving into the world of virtual environments. With Python set up properly, a vast universe of programming possibilities awaits you, from web development and data analytics to artificial intelligence and automation.

As you embark on this exciting journey, always remember to keep your installation and packages updated. Engage with the vibrant Python community online for insights, solutions, and best practices. Here’s to your success in every Pythonic endeavor you take on!

Atiqur Rahman

I am MD. Atiqur Rahman graduated from BUET and is an AWS-certified solutions architect. I have successfully achieved 6 certifications from AWS including Cloud Practitioner, Solutions Architect, SysOps Administrator, and Developer Associate. I have more than 8 years of working experience as a DevOps engineer designing complex SAAS applications.

Leave a Reply