How to Install Python3 on Ubuntu [3 Best Methods]

Written by

Reviewed by

Last updated: June 6, 2024

Expert verified

SVG Image

TL;DR

To install Python 3 on Ubuntu, you can follow these steps:

  1. Open the Terminal by pressing Ctrl + Alt + T.
  2. Run the command sudo apt update to update the package lists.
  3. Run the command sudo apt upgrade to upgrade the installed packages.
  4. Install Python3 Ubuntu by running sudo apt install python3 in the Terminal.
  5. Verify the installation by running the python3 --version to see the Python version installed on your system.

Explore the guide below for a step-by-step guide on how to install Python3 on Ubuntu, configure it, and common errors with possible solutions.

Setting up Python 3 on Ubuntu might feel overwhelming, especially if you’re new to Linux, but don’t worryโ€”I’ve got you covered. This guide will walk you through how to install python3 in Ubuntu. You’ll learn how to set up environment variables, write your first Python script, and create virtual environments for your projects. By the end, you’ll have a solid foundation to start coding in Python on your Ubuntu machine. Let’s explore and get Python up and running smoothly so you can focus on creating amazing projects.

How to Install Python3 on Ubuntu

To install Python 3 on Ubuntu, first open the Terminal. Update your package lists with sudo apt update, then upgrade installed packages with sudo apt upgrade. Next, install Python 3 Ubuntu using sudo apt install python3. Verify the installation by running python3 --version.

For more recent versions, you can add the deadsnakes PPA with sudo add-apt-repository ppa:deadsnakes/ppa, update your package lists again, and install your desired Python version, such as sudo apt install python3.9. This will set up Python 3 on your Ubuntu system.

Here are the detailed steps to install python3 on Ubuntu using three different methods:

1. Installing Python Using Apt Package

  1. Open the Terminal by pressing Ctrl + Alt + T.
opening terminal 15
  1. Run the following command to update the package:
sudo apt update
  1. This command will update the package lists for upgrades and new package installations.
updating system packages 3
  1. After the update process completes, run the following command to upgrade:
sudo apt upgrade
  1. The command will upgrade the installed packages.
upgrading system packages 2
  1. Ubuntu provides Python 3 through its official repository. For installation, open the Terminal and execute the following command to install python Linux:
sudo apt install python3
  1. After the execution of the command, Python 3 will be installed along with the required dependencies.
installing python3 on ubuntu
  1. To check Python 3 is correctly installed on your system, run the following command: 
python3 --version
  1. If the installation was successful, the Terminal will display the Python version installed on your system.
verifying python3 installation

2. Installing Python 3 Using deadsnakes PPA

The deadsnakes PPA is a popular and reliable Personal Package Archive (PPA) that provides newer versions of Python than those available in the official Ubuntu repositories. Using this PPA allows you to easily install and manage multiple Python versions on your system. This method is particularly useful when you need a specific version of Python for compatibility with certain projects or libraries.

  1. To add the deadsnakes PPA to your system, use the following command: 
sudo add-apt-repository ppa:deadsnakes/ppa

Press Enter when prompted to add the PPA. This PPA is maintained by trusted developers and provides access to various Python versions.

adding deadsnake ppa to the system
  1. After adding the PPA, update your package list:
sudo apt update

It will ensure your system is aware of the new packages available from the deadsnakes PPA.

updating system package list 5
  1. Now, you can install the desired version of Python 3. 
sudo apt install python3.9

Replace 3.9 with the version you need.

installing desired version of python
  1. Confirm the installation by checking the Python version. 
python3.9 --version

This ensures that Python 3.9 is correctly installed on your system.

verifying the installation of python

3. Installing Python 3 from Source

Building Python from source gives you the flexibility to customize the installation and optimize performance for your specific system. This method is ideal for advanced users who need specific configurations or the latest version of Python that is not yet available in any repository.

  1. Download the desired version of Python source code from the official Python website. 
wget https://www.python.org/ftp/python/3.9.0/Python-3.9.0.tgz

Replace the URL with the version you want to download.

downloading python source code
  1. Extract the downloaded tarball to prepare for the build process.
tar -xvf Python-3.9.0.tgz
extracting the downloaded tarball
  1. Ensure you have the necessary build dependencies installed. 
sudo apt-get install build-essential zlib1g-dev

This step installs essential tools required for building Python.

installing essential tools to build python
  1. Navigate to the extracted directory and configure the build environment. 
cd Python-3.9.0
navigating to extracted directory
  1. Now run the command:
./configure --enable-optimizations

The –enable-optimizations flag optimizes the binary for better performance.

enabling optimization for better performance of binary
  1. Compile the source code. 
make -j 2

The -j 2 flag uses 2 CPU cores for the build process. Adjust this number based on the number of cores available on your system.

compiling the source code
  1. After compilation, install Python using altinstall to avoid overwriting the default system Python.
sudo make altinstall
installing python using altinstall
  1. Verify the installation by checking the Python version.
python3.9 --version
verifying the installation of python through source code

How to Configure Python 3 on Ubuntu

Configuring Python 3 on Ubuntu is essential for ensuring optimal usage and leveraging its benefits. This section will cover two crucial aspects: setting up environment variables and installing Python packages and libraries. You can customize your Python environment and enhance its functionality by following the steps below.

1. Setting Up Environment Variables

To ensure Python functions properly on Ubuntu, you need to configure the environment variables. This allows for seamless execution and accessibility. Follow these steps to set up environment variables:

  1. Open the Terminal.
  2. Execute the following command to open the .bashrc file in a text editor:
nano ~/.bashrc
opening bashrc file in text editor
  1. Scroll to the bottom of the file and add the following lines:
export PATH="/usr/local/bin:$PATH"
export PATH="/usr/local/sbin:$PATH"
  1. It will set the environment variables for the proper functioning of python.
setting environment variables
  1. Save the changes by pressing Ctrl + O and exit the text editor by pressing Ctrl + X.
saving and exiting the file
  1. To apply the changes, run the following command:
source ~/.bashrc
applying changes

2. Installing Python Packages and Libraries

Python packages and libraries expand the capabilities of the language. With these libraries, you can supercharge your Python projects and tap into a vast array of functionality. To install additional packages using pip, the package installer for Python, follow these steps:

  1. Launch the Terminal window and run the following command to install pip:
sudo apt install python3-pip
  1. Once pip is installed, you can use it to install Python packages.
installing pip
  1. To install the python packages, run the following command:
pip3 install package-name

Replace package-name with the actual package name.

installing packages using pip

Creating Your First Python Program

Writing your first Python program is an exciting step towards learning this versatile and powerful programming language. I will walk you through creating a simple Python script, running it, and understanding the basics of Python syntax.

  1. Ensure that Python is installed on your system. Open your favorite text editor or Integrated Development Environment (IDE). 
opening text editor
  1. Create a new file and name it hello.py. This file will contain your first Python program. In the hello.py file, write the following code:
print("Hello, World!")
creating a simple python file

This simple program uses the print function to display the message “Hello, World!” on the screen.

  1. Save the hello.py file in your desired directory. Ensure you remember the directory path as you will need to navigate to it to run the script.
saving the file
  1. Open a terminal and navigate to the directory where you saved hello.py:
cd projects
navigating to directory containing python script
  1. Once you are in the correct directory, run the script using the Python interpreter:
python3 hello.py
running python script
  1. You should see the output: Hello, World!

Creating a Virtual Environment for Python 3

Using virtual environments is a best practice in Python development. Virtual environments allow you to create isolated environments for different projects, ensuring that dependencies and packages do not conflict. This is particularly useful when working on multiple projects that require different versions of the same packages.

  1. Before creating a virtual environment, you need to ensure the venv module is installed. This module is part of the Python standard library, but you may need to install it explicitly.
sudo apt install python3-venv
installing venv module for python environment
  1. Create a new directory for your project and navigate to it. Then, create a virtual environment within this directory.
mkdir myproject

cd myproject

python3 -m venv myenv

This command creates a virtual environment named myenv in the myproject directory.

creating virtual environment in a directory
  1. To start using the virtual environment, you need to activate it. This modifies your shell’s environment to use the virtual environment’s Python interpreter and packages.
source myenv/bin/activate

After activation, your shell prompt will change to indicate that you are now working within the virtual environment.

activating the python environment
  1. You can now install packages using pip within this isolated environment without affecting the global Python installation.
pip install package_name
installing package in the environment
  1. Once you are done working in the virtual environment, you can deactivate it to return to your global Python environment.
deactivate
deactivating the environment

5 Common Errors When Installing Python 3

Installing Python 3 on Ubuntu is a straightforward but challenging process. By understanding these errors, you’ll be able to overcome issues and install Python 3 on Ubuntu. In this section, I will highlight five common errors that users may encounter during installation and provide guidance on resolving them.

1. ๐Ÿ Command ‘python3’ Not Found

This error occurs when Python 3 is not installed correctly or the system path is not set up.

To resolve this, ensure you have installed Python 3 using sudo apt update and sudo apt install python3. Verify the installation with python3 --version. If the issue persists, check that /usr/bin/python3 is in your system’s PATH variable. Add it by editing your .bashrc file with nano ~/.bashrc, adding export PATH="/usr/bin:$PATH", and applying the changes with source ~/.bashrc.

2. ๐Ÿ”ง Dependency Issues During Installation

These issues arise when the system’s package list is outdated, or there are missing dependencies required for Python 3.

Before installing Python 3, update your package list and upgrade existing packages with sudo apt update and sudo apt upgrade to ensure all dependencies are met. If issues persist, use the deadsnakes PPA to get the latest Python versions and their dependencies by running sudo add-apt-repository ppa:deadsnakes/ppa, sudo apt update, and sudo apt install python3.x (replace x with the desired version number).

3. ๐Ÿšซ Permission Denied While Installing Packages

This error occurs when attempting to install Python packages without sufficient permissions.

To fix this, use sudo to install packages system-wide, such as sudo apt install python3-pip and sudo pip3 install package-name. If you prefer to avoid using sudo, consider using a virtual environment to manage packages locally by creating one with python3 -m venv myenv, activating it with source myenv/bin/activate, and installing packages with pip install package-name.

4. ๐Ÿ“‰ Incorrect Python Version Installed

This issue arises when an older or wrong version of Python is installed, possibly due to default repository limitations.

Specify the desired version explicitly by using the deadsnakes PPA. For example, to install Python 3.9, run sudo add-apt-repository ppa:deadsnakes/ppa, sudo apt update, and sudo apt install python3.9. Verify the installed version with python3.9 --version to ensure you have the correct version.

5. โš™๏ธ Failure to Set Up Environment Variables

Issues with environment variables can prevent Python scripts from running correctly.

To resolve this, add Python paths to your environment variables by editing the .bashrc file with nano ~/.bashrc and adding export PATH="/usr/local/bin:$PATH" and export PATH="/usr/local/sbin:$PATH". Save and exit, then apply the changes with source ~/.bashrc. This ensures that Python and its packages are accessible from any terminal session, allowing for seamless execution and accessibility.

Ubuntu Python3 Install: Final Thoughts

In this guide, I’ve explored how to install python 3 on Linux using the APT package manager, the deadsnakes PPA, and building from source. I have also covered configuring Python, writing your first script, and creating virtual environments. These steps ensure you have a solid foundation in Python installation, configuration, and basic usage.

For further learning, I recommend exploring the following articles:

  • Learn to check the version of the Python package to ensure compatibility and manage updates effectively.
  • Explore how to fix the “python: command not found” error to ensure your Python installation is correctly set up and accessible.
  • Discover how to fix the “You Have Held Broken Packages” error on Linux, which is crucial for resolving installation issues.

Frequently Asked Questions

Can I install Python 3 alongside Python 2 on Ubuntu?

Yes, you can install Python 3 alongside Python 2 on Ubuntu. Python 2 is often required for backward compatibility with legacy codebases. To install Python 3 alongside Python 2, follow the installation steps outlined in this guide without removing Python 2. This will ensure that both versions coexist on your system, allowing you to work with projects that require either version. You can use the command python3 to invoke Python 3 and python (or python2) to invoke Python 2, depending on your needs.

How can I upgrade an existing Python 2 installation to Python 3?

To upgrade from Python 2 to Python 3, install Python 3 using sudo apt install python3. Then, update your scripts and dependencies to be compatible with Python 3. Use python3 script.py to run your scripts. You can also install python3-pip to manage Python 3 packages separately from Python 2.

What if I encounter compatibility issues with my existing Python 2 codebase?

If you encounter compatibility issues when migrating from Python 2 to Python 3, several tools and libraries can assist in the transition. The 2to3 command-line utility is one such tool that automatically converts Python 2 code to Python 3 syntax. It analyzes your codebase and applies transformations to update the syntax, imports, and other necessary changes. Additionally, the Python community provides extensive documentation and resources to aid migration. The official Python website offers a comprehensive Porting Python 2 Code to Python 3 HOWTO guide with detailed explanations, tips, and examples for addressing common compatibility issues.ย 

Ojash

Author

Ojash is a skilled Linux expert and tech writer with over a decade of experience. He has extensive knowledge of Linux's file system, command-line interface, and software installations. Ojash is also an expert in shell scripting and automation, with experience in Bash, Python, and Perl. He has published numerous articles on Linux in various online publications, making him a valuable resource for both seasoned Linux users and beginners. Ojash is also an active member of the Linux community and participates in Linux forums.

Akshat

Reviewer

Akshat is a software engineer, product designer and the co-founder of Scrutify. He's an experienced Linux professional and the senior editor of this blog. He is also an open-source contributor to many projects on Github and has written several technical guides on Linux. Apart from that, heโ€™s also actively sharing his ideas and tutorials on Medium and Attirer. As the editor of this blog, Akshat brings his wealth of knowledge and experience to provide readers with valuable insights and advice on a wide range of Linux-related topics.

Share this article
Shareable URL
Prev Post

How to Compare Two Files Linux [3 Effective Methods]

Next Post

How to Ping Specific Port Linux [5 Easy Methods]

Leave a Reply

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

Read next