How to Install Python and Pip on Ubuntu 24.04

Updated on September 9, 2024
How to Install Python and Pip on Ubuntu 24.04 header image

Introduction

Python is a modern general-purpose programming language suitable for various development and scripting tasks. Pip is a package installer for Python that allows you to install and manage additional libraries that are not part of the standard Python library on a server.

This article explains how to install Python and Pip on an Ubuntu 24.04 server and manage application processes on your server. You will install the latest Python version using a PPA, manage multiple versions and use the Python virtual environment package virtualenv to create isolated development environments.

Prerequisites

Before you begin:

Install Python

Python is available in the default APT repositories on Ubuntu 24.04 but may not be the latest version. Personal Package Archives (PPAs) enable the installation of custom application packages on a server, and you can use the deadsnakes PPA to install the latest or specific Python version on your server. Follow the steps below to install the latest Python version using the PPA.

  1. Add the deadsnakes PPA to your server sources.

    console
    $ sudo add-apt-repository ppa:deadsnakes/ppa
    
  2. Update the server package index.

    console
    $ sudo apt update
    
  3. Install the latest Python version on your server.

    console
    $ sudo apt install python3
    

    Run the following command to install a specific version such as 3.10.

    console
    $ sudo apt install python3.10
    
  4. View the installed Python version on your server.

    console
    $ python3 --version
    

    Output:

    Python 3.12.3

Install Pip

  1. Run the following command to install Pip module.

    console
    $ sudo apt install -y python3-pip
    
  2. View the installed Pip version on your server.

    console
    $ pip3 --version
    

    Output:

    pip 24.0 from /usr/lib/python3/dist-packages/pip (python 3.12)

Test Python

  1. Access the Python Shell.

    console
    $ python3
    
  2. Enter the following code to test your Python installation.

    python
    >>> print("Hello, Python!")
    

    Output:

    Hello, Python!
  3. Exit the Python Shell.

    python
    >>> exit()
    

Install Multiple Python Versions

Python supports multiple versions you can install and use on your server using the update-alternatives utilities. This enables you to work with multiple project environments that match specific Python requirements on your server. Follow the steps below to install multiple Python versions for use on your server.

  1. Install another version of Python such as Python3.9.

    console
    $ sudo apt install python3.9
    
  2. Run the following update-alternatives commands to enable multiple versions in your Python binary /usr/bin/ location. Replace python3.12 with your active Python version and python3.9 with the newly installed version.

    console
    $ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.12 1
    
    $ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 2
    
  3. View all available Python versions you can switch between on your server.

    console
    $ sudo update-alternatives --config python3
    

    Output:

      Selection    Path                 Priority   Status
    ------------------------------------------------------------
    * 0            /usr/bin/python3.9    2         auto mode
      1            /usr/bin/python3.12   1         manual mode
      2            /usr/bin/python3.9    2         manual mode
    
    Press <enter> to keep the current choice[*], or type selection number: 

    Enter your desired Python version to activate in your server environment and press Enter to apply changes. When working with virtual environments, the selected version is only activated in your environment.

Create Virtual Environments

The Python virtual environment module virtualenv creates virtual environments that enable the management of dependencies for different Python projects to limit package conflicts on your server. Follow the steps below to create a Python virtual environment to use on your server.

  1. Install the Python virtualenv module.

    console
    $ sudo apt install -y python3-virtualenv
    
  2. Create a new sample virtual environment such as newenv.

    console
    $ virtualenv newenv
    

    Output:

    created virtual environment CPython3.10.x in 261ms
      creator CPython3Posix(dest=/home/user/project/venv, clear=False, no_vcs_ignore=False, global=False)
      ...
  3. Activate the virtual environment.

    console
    $ source newenv/bin/activate
    

    Verify that your terminal environment changes to the new Python virtual environment similar to the output below.

    (newenv) linuxuser@server:~# 
  4. Run the following command to deactivate the virtual environment.

    console
    $ deactivate
    

Conclusion

You have installed Python and Pip on your Ubuntu 24.04 Vultr server. Then, you enabled multiple versions and set up virtual environments to use Python with different projects on your server. For more information and usage options, please visit the Python documentation.