For enquiries call:

Phone

+1-469-442-0620

April flash sale-mobile

HomeBlogData ScienceHow to Install Python 3 on Ubuntu [Step-by-Step Guide]

How to Install Python 3 on Ubuntu [Step-by-Step Guide]

Published
20th Sep, 2023
Views
view count loader
Read it in
6 Mins
In this article
    How to Install Python 3 on Ubuntu [Step-by-Step Guide]

    Anyone aspiring to be a data scientist, machine learning engineer, or software developer must have thought about learning Python. The popularity of this programming language has grown exponentially in the past ten years. Even those unfamiliar with coding have probably heard about it. As per the developer survey by Stack Overflow in 2021, approximately 68% of software developers or data scientists who have worked on developing software using Python have expressed that they will continue doing so with Python.

    The same study found Python to be the most desired coding language among those who do not presently use it. The popularity of Python cannot be disputed. And if recent patterns are any guide, they'll probably continue to be in great demand for years. Python is a well-known, simple-to-learn programming language with a growing user base. Anyone looking to enter the IT business through coding should start with Python.

    Data science, machine learning, and game design are just a few of the fields where it is used. It is also incredibly versatile and straightforward to learn. In this article, We will assist you with instructions on installing Python on Ubuntu 18.04 or 20.04, but let's first introduce this well-known programming language.

    % of developers who are not developing with the language or technology but have expressed interest in developing with it. Picture from Stake Overflow survey.

    Developers percentage

    You can start with learning Python to solve data science problems. By visiting this link, find out the Data Science Coding Bootcamp cost to do this course. Let’s get started with your learning.

    What is Python?

    According to the Python organization, Python is an open source, interpreted, object-oriented, high-level, dynamically semantic programming language. Due to its high-level built-in data structures, dynamic typing, and dynamic binding, it is well suited for robust Application Development and as a scripting or glue language to connect existing components.

    Python's concise syntax focuses on readability and ease of use, which reduces program maintenance costs. Python's support for modules and packages promotes modularity and code reuse in programs. That is why Python frequently causes programmers to fall in love due to its enhanced productivity. The edit-test-debug cycle is rapid because there is no compilation phase.

    Python programs are easy to debug because a bug or incorrect input never causes a segmentation failure. Instead, whenever the interpreter finds an error, it throws an exception. The interpreter creates a stack trace if the application does not catch the exception. Utilizing the source-level debugger, setting breakpoints, evaluating arbitrary expressions, inspecting local and global variables, stepping through the code line by line, and other functions. Python's ability to introspect is demonstrated by the debugger developed in Python.

    Python is a fantastic programming language for automating tasks, and most Linux system comes with Python pre-installed. The Python package included with Ubuntu 18.04 is version 3.6.8 and version 3.8.10 in Ubuntu 20.04. In particular, this article's instructions will cover a step-by-step installation guide for the most recent stable version of Python, version 3.10.0.

    What Version Would Users Require?

    The Python programming language has two main versions: the older Python 2.x series and the more recent Python 3.x series. When Python 3.0 was published in December 2008, the Python 3.x series began, and Python 3.10.6 is the latest release version released on August 2, 2022. Python 2.7 has been released multiple times, and Python 2.7.18 was the last version released on April 20, 2020.

    Python 3's syntax is more uncomplicated than Python 2's. Many Python 2 libraries lack backward compatibility. Many libraries are developed in Python 3 for usage only with Python 3. Since 2020, Python 2 is no longer in use, so The best language to learn in 2022 is Python 3. Python 2 may be necessary for some situations; however, Python 3 is often the most widely used language. Python 3.10 is the most stable version available, so in this article, we will install the latest version of Python3 (3.10.x) on Ubuntu 20.04. or 18.04. Before going forward with the installation, let's understand key terms like pip, virtual environment, and path. 

    • Pip: The default package manager for Python is called pip. Additional packages not part of the Python standard library are available for installation and management. 
    • Virtual Environment: It is a Python environment that isolates the Python interpreter, libraries, and scripts from those installed in other virtual environments, as well as (by default) any libraries placed in a "system" Python, i.e., installed as part of your operating system.
    • Environment Variable: a variable whose value is set externally to the application via an operating system or microservice feature. 
    • Path is an environment variable you set before running the Python interpreter. 

    Prerequisites

    • Here are some prechecks before you install Python 3.10.x version on your system 
    • Make sure you have Ubuntu 18.04 or 20.04 running. 
    • Please ensure this is the version you need for your goals, then stick with the same. 
    • A user account should have su 
    • do privileges. 
    • Access to a terminal window (Ubuntu terminal shortcut: Ctrl+Alt+T) 
    • The environment should be configured to use Python 3.10.x 

    You can start with a Data Science online course to learn about solving data science problems leveraging Python and obtain a basic understanding of data science methodology.

    How to Install Python on Ubuntu?

    Download the latest version of Python 3.10, then visit the official website and navigate the "Downloads" section to get the most recent Python version for Ubuntu 18.04 or Ubuntu 20.04, which can be found on the website. Go ahead and download it. This article will guide you on installing Python 3.10 on Ubuntu 18.04 or Ubuntu 20.04.

    Methodology to install Python 3.10 Ubuntu 20.04 or 18.04: 

    • Using apt, Install Python 3.10 from the deadsnakes PPA
    • Manually install Python 3.10 from source code 

    Option 1: Install Python 3.10 on Ubuntu 18.04 or 20.04 using apt

    Python is installed by this approach using the apt package manager. There are fewer processes, but they are subject to revisions to third-party hosting software. On a third-party repository, new releases might not appear as rapidly. Python is often pre-installed on Ubuntu 18.04 or Ubuntu 20.04 factory editions. Enter the following to check the Python version: 

    Python --version 

    If the Version is lower than 3.10.x, or if Python is not installed, continue to the next steps:

    Step 1: To ensure that the system is updated and the necessary packages are installed, open a terminal window and type the following commands: 

    sudo apt update 

    Step 2: Install Supporting Software using below steps 

    You may add PPA (Personal Package Archive) repositories with the help of the software-properties-common package, which provides you more control over your package management. Use the following command to install the additional software: 

    sudo apt install software-properties-common -y 

    Step 3: Add the deadsnakes PPA

    Deadsnakes is an alternative PPA to the official Ubuntu repository. Enter the following to add the PPA: 

    sudo add-apt-repository ppa:deadsnakes/ppa 

    Press Enter to continue 

    Output: 

    Step 4: With the deadsnakes repository added to your Ubuntu 20.04 or 18.04 system, download Python 3.10 with the single command below. 

    sudo apt install Python3.10 

    Output (Dependency Tree) : 

    Step 5: Verify the installation by checking the installed version: 

    $ Python3.10 --version 3.10.6 

    Option 2:Install Python 3.10 From Source Code on Ubuntu 20.04 or 18.04

    Building Python 3.10 from source code is another way to have it installed on your Ubuntu 20.04 or 18.04 machine. Although you won't be able to continue to get updates, bug patches, and security upgrades through the APT package management, you are guaranteed to have the most recent version of Python with this installation technique. 

    Step 1: To ensure that the system is updated and the necessary packages are installed, open a terminal window and type the following commands: 

    sudo apt update 

    Step 2: Install the required dependencies to build Python 3.10 from the source 

    Additional software is required to compile a package from its source code. 

    To install the necessary Python packages, type the following: 

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

    Output (Dependency Tree) : 

    Step 3 : Download the latest version of Python 3.10 from the Python official release page 

    Now, download Python's most recent release version from its official release website/. Alternately, copy the URL for the Python 3.10 tarball and use Wget to download it, as shown below.

    wget https://www.Python.org/ftp/Python/3.10.5/Python-3.10.5.tgz

    Step 4: After complete download, proceed and extract and configure 

    tar -xf Python-3.10.*.tgz 

    Navigate into the extracted directory: 

    cd Python-3.10.*/ 

    Now run the configure script to check the required dependencies. The –-enable optimization flag optimizes the binary by running multiple tests 

    /configure --enable-optimizations 

    Output: 

    Step 5:  initiate the Python 3.10 build process as below. 

    make -j $(nproc) 

    To make the procedure speedier, keep in mind that the -j parameter should equal the number of cores on your system. Use the nproc command to determine how many cores are in your system. I have two cores available for this instance on my system. 

    This step can take up to 15-20 minutes to complete. 

    Output: 

    Step 6: Once the build process complete, Run the command below to install Python 3.10 on Ubuntu 20.04 or 18.04. 

    sudo make altinstall 

    Here, altinstall is used instead ofinstall to keep the default Python binary path in /usr/bin/Python

    Output : 

    Verify your installation: 

    $ Python3.10 --version 3.10.5 

    Using Different Versions of Python

    If you installed Python using the altinstall technique, your system is running two different Python versions simultaneously. Every installation uses a unique command. 

    When choosing between Python 3.7.x and Python 3.10.x that are both installed, utilize the second digit 

    Example:  

    $ Python3.7 --version 
    3.7.3 
    $ Python3.10 --version 
    3.10.6 

    Install Python Modules | Extensions on Ubuntu 20.04|18.04

    Python benefits from modules and extensions because they provide it with more functionality. On Ubuntu 20.04|18.04, modules can be installed using the Python Package manager (PIP). If not available, PIP must be set up on your system, as shown below. 

    sudo apt install Python3-pip

    Output: 

    Then, use the syntax below to install a Python module of your choice. 

    sudo pip install module-name 

    In this article, I will demonstrate how to install Python module pandas

    sudo pip install pandas 

    Output: 

    How to Check if Python Is Installed on Your System?

    Use below command to list out installed module:

    python -m pip list

    Output: 

    Setting Up Python3

    We have already discussed the Python3 setup steps above. Follow the above steps to move to the next step, setting up a virtual environment to run Python code in an isolated way. With virtual environments, you may create a separate area on your server for your Python projects, allowing each of them to have a unique set of dependencies that won't interfere with any others.

    Setting Up a Virtual Environment

    Setting Up a Virtual Environment, a programming environment can be set up to give users more control over Python projects and handling various package versions. Particularly when working with third-party software, this is crucial.

    There is no limit to the number of Python programming environments you can set up. Each environment on your server is essentially a folder or directory with a few scripts inside of it that causes it to function as an environment.

    There are a few different ways to create a programming environment in Python, but in this case, we'll use the virtualenv module from the standard Python 3 library. Type the following to install virtualenv.

    pip install virtualenv

    Let's either decide the directory we want to use for our Python development environments or use the mkdir command to create a new directory.

    mkdir environment 

    Navigate to directory environment.

    cd environment

    Create a virtual environment in the environment directory using the following command.

    Python3 -m virtualenv venv

    You must activate this environment before you can use it. To do so, use the command that invokes the activate script, as shown below.

    source venv/bin/activate 

    Your environment's name, in this case, my venv, will now appear before your command prompt. Your prefix may alter somewhat depending on the Debian Linux version you use. Still, the name of your environment in parentheses should be the first item you see on your line.

    (venv) (base) amit@amit:~$

    This prefix informs us that the environment my venv is now active, which means that any programs we write here will only use the parameters and components specific to this environment.

    After following these steps, your virtual environment is ready to use.

    Creating a “Hello, World” Program

    The virtual environment is now ready, so let's run a classic "Hello, World!" code. It will allow us to test our environment.

    To do this, we will open the Python terminal in a virtual environment by writing Python.

    (venv) (base) amit@amit:~$ Python 

    Now write print ("Hello, World!") in the Python terminal.

    (venv) (base) amit@amit:~$ Python 
    Python 3.7.3 (default, Mar 27 2019, 22:11:17)
    >>> print("Hello, World") 
    Hello, World 
    >>> quit() 
    (venv) (base) amit@amit:~$ 

    By using the command deactivate, you can exit the environment and go back to your default directory.

    What is the Most Efficient Way to Execute Python Code?

    A user can follow a few steps to execute Python code, most efficiently or incredibly fast:

    • Proper Data structure and algorithm; because each data structure has a notable effect on runtime, a developer should use sets and dictionaries instead of lists whenever possible.
    • Using built-in functions and libraries. Python's built-in functions are one of the best ways to speed up your code. You need to use Python's built-in functions if needed. These built-in features are well tested.
    • Prefer list comprehensions over loops: List comprehensions are an elegant and better way to create a new list based on the elements of an existing list in a single line of code.
    • You should avoid importing unnecessary modules and libraries until you need them. Instead of importing the entire library, you can specify the name of the module. Importing unnecessary libraries will slow down code performance.
    • Using multiple assignments: If you want to assign values to multiple variables, don't assign them row by row. Python has an elegant and better way to assign multiple variables.

    Example: 

    a = "John" 
    b = "Henry" 
    c = "Manchester" 

    Better way to assign these variables: 

    a,b,c="John","Henry""Manchester" 

    This assignment technique is clean and elegant compared to the previous assignments. 

    The above suggestions will surely help you to optimize the code in Python way. It will help you write clean and elegant code and make your code run faster, especially useful in competitive programming where time complexity is everything. 

    How to Uninstall Python from Ubuntu

    In case you want to uninstall Python from your Ubuntu 18.04|20.04 system. You need to know which Python version you are trying to uninstall.

    Let’s assume you want to uninstall Python 3.7. You can uninstall using the command:

    sudo apt autoremove Python3.7

    Take your business analysis skills to new heights with certified business analysis professional training from the comfort of your own home. Enroll now!

    Conclusion

    As a result, we have successfully learned how to install Python on Ubuntu and its various functionalities. We recommend running all versions simultaneously if you want to get the most out of Python. Using the most recent Python version while running older Python scripts and testing functionality for newer projects is advantageous. If you are sure that you only need one Python version, you may install Python 2 or Python 3. Hope this article will get you started with Python installation, package installation, virtual environment setup, and code optimization.

    You can start with KnowledgeHut’s Data Science Coding Bootcamp to learn about solving data science problems leveraging Python and obtain a basic understanding of data science methodologies like data preparation, modeling, and evaluation.

    Frequently Asked Questions (FAQs)

    1Does pip come with Python?

    It is pre-installed with the Python binary installers as of Python 3.4. Which means you need to install pip for version Python 3,4 or above. 

    2How do I run Python in Ubuntu terminal?

    Open the terminal by searching in application or press Ctrl + Alt + T to launch it. Use the cd command to move the terminal to the directory containing the script. To run the script, type Python nameofscript.py at the terminal. 

    3How do I make Python 3.8 default Ubuntu?

    You need to update your update-alternatives, then you will be able to set your default Python version. Use the following command to set Python3.8 as default: 

    sudo update-alternatives --set Python /usr/bin/Python3.8 
    4How do I install Python 3.9 7?

    Use the above steps mentioned in the article to install the Python 3.9.7 version on Ubuntu 18.04 or 20.04 

    5How do I run Python from the command line?

    You must open a command line on the windows system and input the word Python3 followed by the path of your script to run Python programs with the Python command. At the command line, enter Python nameofscript.py to execute the script. 

    Profile

    Amit Kishore

    Author

    Experienced R&D Data Scientist with a demonstrated history of working experience in predictive analytics, deep learning, and Business Intelligence. Hands-on experience leveraging machine learning, data modeling, cloud computing, business intelligence, deep learning, and statistical modeling to solve challenging business problems with value addition.Strong engineering professional with a master's and bachelor's in Industrial and Industrial Engineering and Management from the Indian Institute of Technology, Kharagpur.

    Share This Article
    Ready to Master the Skills that Drive Your Career?

    Avail your free 1:1 mentorship session.

    Select
    Your Message (Optional)

    Upcoming Data Science Batches & Dates

    NameDateFeeKnow more
    Course advisor icon
    Course Advisor
    Whatsapp/Chat icon