How to Install Python 3 on Ubuntu [9 Easy Steps]

Written by

Reviewed by

Last updated: June 25, 2023

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 Python 3 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.

Configuring Python 3 on Ubuntu is crucial for optimal usage. Setting up environment variables and installing Python packages. addressing common installation errors, including python3 command not found, dependency issues, permission denied, incorrect Python version, and environment variable setup.

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

Python, a powerful and versatile programming language, is widely used in various industries, from web development and data analysis to artificial intelligence and scientific research. With its clean syntax and extensive library support, Python 3 has gained immense popularity across various domains, from web development to data science and machine learning. If you’re an aspiring Python developer or a seasoned programmer, having Python 3 installed on your Ubuntu system is crucial. In this comprehensive guide, I will walk you through the step-by-step process to Python 3 on Ubuntu, configuring it, and common errors with possible solutions.

How to Install Python 3 on Ubuntu

To install Python 3 on Ubuntu, you’ll need a compatible version of Ubuntu (such as 22.04 LTS), sufficient free disk space, at least 1 GB of RAM, a modern x86-based processor, and an active internet connection for downloading and updating Python, and its libraries. Follow these steps:

  1. Open the Terminal by pressing Ctrl + Alt + T.
opening terminal 15
  1. Run the following command to update the package:
<strong>sudo apt update</strong>
  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:
<strong>sudo apt upgrade</strong>
  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 3 on Ubuntu:
<strong>sudo apt install python3</strong>
  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: 
<strong>python3 --version</strong>
  1. If the installation was successful, the Terminal will display the Python version installed on your system.
verifying python3 installation

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:
<strong>nano ~/.bashrc</strong>
opening bashrc file in text editor
  1. Scroll to the bottom of the file and add the following lines:
<strong>export PATH="/usr/local/bin:$PATH"</strong>
<strong>export PATH="/usr/local/sbin:$PATH"</strong>
  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:
<strong>source ~/.bashrc</strong>
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:
<strong>sudo apt install python3-pip</strong>
  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:
<strong>pip3 install package-name</strong>

Replace package-name with the actual package name.

installing packages using pip

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 better equipped to overcome obstacles 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.

  • Command python3 not found: If you receive an error stating that the python3 command is not found, it typically indicates that Python 3 is not installed on your system. To resolve this, follow the installation steps mentioned in this guide to install Python 3. Double-check that the installation succeeded by running python3 –version and ensure the command is recognized.
  •  ⚠️ Dependency issues during installation: Dependency issues can arise during the installation process, causing errors or incomplete installations. To address this, ensure that your system is up to date by running sudo apt update and sudo apt upgrade before attempting the installation. Double-check that you have a stable internet connection to download and install the necessary dependencies. Resolving any dependency-related issues should allow for a successful installation.
  •  🚫 Permission denied while installing packages: When installing Python packages using pip, you might encounter a “Permission denied” error. This occurs when the installation is attempted without administrative privileges. To resolve this, prepend the sudo command to your pip installation command, like sudo pip3 install package-name, and provide your password when prompted. This will grant the necessary permissions to install the package system-wide.
  •  🆘 Incorrect Python version installed: In some cases, users may mistakenly install an older version of Python or an incompatible version for their system. To avoid this error, follow the steps outlined in this guide to install Python 3 specifically. Double-check the installed version using the python3 –version and verify that it corresponds to the desired Python 3 version.
  • Failure to set up environment variables: If you encounter issues with environment variables after installing Python 3, it may lead to problems executing Python scripts or accessing Python globally. Double-check that you’ve correctly added the necessary lines to your .bashrc file, as the guide outlines. Also, ensure you’ve sourced the file using the command source ~/.bashrc for the changes to take effect. Properly configuring the environment variables is essential for the correct functioning of Python.

In a Nutshell

This article has provided a comprehensive step-by-step guide on how to install Python 3 on Ubuntu system and how to configure it. Additionally, I have also discussed common errors that may occur during the installation process, ensuring you are prepared to troubleshoot and overcome any challenges. 

As you continue your Python journey, consider exploring the following articles and topics to expand your knowledge, Best Practices for Python Development, Introduction to Python Virtual Environments, and Exploring Python Frameworks and Libraries. Remember, continuous learning and exploration will fuel your growth as a Python developer and open up exciting possibilities for your projects and career.

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 an existing Python 2 installation to Python 3, follow these steps. First, install Python 3 alongside Python 2 on your system, as outlined in this guide. This allows you to have both versions installed simultaneously. Next, update your codebase to ensure compatibility with Python 3 syntax and libraries. This may involve changing function calls, imports, and usage of language features deprecated or changed in Python 3. Test your code extensively to identify and resolve any compatibility issues. Creating a backup of your codebase before proceeding with the upgrade process is recommended.

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. 

Can I install multiple versions of Python 3 on Ubuntu?

Yes, it is possible to have multiple versions of Python 3 installed on your Ubuntu system. This allows you to work with different projects that require specific Python 3 versions or need to maintain compatibility with specific libraries or frameworks. Tools like pyenv and conda provide convenient solutions for managing multiple Python 3 versions. With pyenv, you can easily install and manage different Python versions per-project. It allows you to switch between versions effortlessly using a simple command, enabling you to work on multiple projects simultaneously. Similarly, conda is a package and environment manager that supports multiple Python versions, allowing you to create isolated environments with specific Python versions and dependencies for efficient project management.

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 Using Diff Command in 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