How To Install Python 3.11 on Ubuntu 20.04 | Easy Methods

Posted on

How To Install Python 3.11 on Ubuntu 20.04 | Easy Methods

How To Install Python 3.11 on Ubuntu 20.04 | Easy Methods

In this tutorial, we will demonstrate How To Install Python 3.11 on Ubuntu 20.04. Python is a high-level, interpreted, interactive, and object-oriented scripting language. Python is designed to be highly readable. It uses English keywords frequently where other languages use punctuation, and it has fewer syntactical constructions than other languages.

Currently, Python 3.11 is the newest major release of the Python programming language, and it contains many new features and optimizations. Follow the steps below to set up Python 3.11 on Ubuntu 20.04.

To complete this guide, you must log in to your server as a root or non-root user with sudo privileges.

Now follow the steps below to install Python 3.11 Latest version on Ubuntu 20.04 in two different ways:

Ubuntu 20.04 Python 3.11 Setup

The first method to install Python 3.11 on Ubuntu 20.04 is to use the APT repository.

Method 1. Set up Python on Ubuntu from the APT repository

First, update your local package index with the following command:

sudo apt update

Then, you need to install the dependencies for adding the PPA repository:

sudo apt install software-properties-common -y
Add PPA Repository

Next, use the following command to add the PPA dead snake to the APT repository:

sudo add-apt-repository ppa:deadsnakes/ppa

At this point, you can install Python 3.11 on Ubuntu 20.04 with the following command:

sudo apt install python3.11

You can verify your Python 3.11 installation by checking its version:

python3.11 --version
Ubuntu 20.04 Python 3.11

Method 2. Set up Python 3.11 on Ubuntu from the Source

Another way to install Python 3.11 on your server is to build from the source code. It’s recommended to use this method over the APT repository for greater control and potentially better performance.

First, update your local package index with the following command:

sudo apt update

Then, install the dependencies with the command below:

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

Now you need to visit the Python Downloads page and copy the link address of the Python 3.11.1 gzipped source tarball. Use the wget command to download it:

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

When your download is completed, extract your downloaded file with the following command:

sudo tar -xf Python-3.11.*.tgz

Switch to your extracted directory:

cd Python-3.11.*/
Build and Install Python 3.11

Next, run the configure script to check the required dependencies:

./configure --enable-optimizations

At this point, you can start your build process with the make command. But before this, you need to check the number of your cores with the command below:

nproc

Example output:

**Output**
2

Then, build your process with the command below:

make -j 2

Note: The (-j) corresponds to the number of cores in your system to speed up the build time.

When your build process is completed, use the following command to install Python 3.11 on Ubuntu 20.04:

sudo make altinstall

Verify your Python 3.11 installation by checking its version:

python3.11 --version
Ubuntu 20.04 Python 3.11 Setup

Create a Test Virtual Environment with Python 3.11

At this point, we want to create a test virtual environment to see that Python 3.11 is working correctly on Ubuntu 20.04.

First, create a project directory and switch to it with the command below:

mkdir ~/test_app && cd ~/test_app

Then, create a virtual environment with the following command, for the test name test_app_venv:

python3.11 -m venv test_app_venv

Note: The compiled installation included venv. However, if you installed using the APT package manager method, you may need to install the venv package if you encounter problems.

sudo apt install python3.11-dev python3.11-venv -y

Next, activate your virtual environment:

source test_app_venv/bin/activate

After starting the virtual environment, you will now be in the shell prompt terminal. You will notice the name of your environment will be prefixed.

By default, PIP3.11 should be installed, which is the most used package manager for Python.

Check for the PIP upgrades:

python3.11 -m pip install --upgrade pip

For example, you can install Apache-Airflow with the command below:

pip3.11 install apache-airflow

To remove the application you can use the following command:

pip3.11 uninstall apache-airflow

To exit the virtual environment, use the following command:

deactivate

Alternative Methods to Install Python 3.11 on Ubuntu 20.04

While the PPA and source compilation methods are effective, let’s explore two alternative ways to install Python 3.11 on Ubuntu 20.04. These methods leverage containerization and package management for easier installation and dependency management.

Method 3: Using Docker

Docker provides a containerized environment, ensuring consistency across different systems. This method is particularly useful if you want to isolate your Python 3.11 environment from the rest of your system or if you need to replicate the environment on multiple machines.

  1. Install Docker: If you haven’t already, install Docker on your Ubuntu 20.04 system. Follow the official Docker documentation for installation instructions (https://docs.docker.com/engine/install/ubuntu/).

  2. Pull a Python 3.11 Docker Image: Docker Hub hosts a variety of pre-built Python images. Pull an official Python 3.11 image.

    docker pull python:3.11
  3. Run a Container: Start a new container based on the pulled image.

    docker run -it python:3.11 bash

    The -it flags allocate a pseudo-TTY connected to the container and keep STDIN open, allowing you to interact with the bash shell inside the container.

  4. Verify Installation: Inside the Docker container, verify the Python version.

    python3 --version

    This should output Python 3.11.x (where x is the specific patch version).

Code Example (Dockerfile):

For more customized setups, you can create your own Dockerfile:

FROM ubuntu:20.04

RUN apt-get update && apt-get install -y --no-install-recommends 
    software-properties-common 
    && add-apt-repository ppa:deadsnakes/ppa 
    && apt-get update && apt-get install -y python3.11 python3-pip

WORKDIR /app

COPY requirements.txt .
RUN pip3 install -r requirements.txt

COPY . .

CMD ["python3", "your_script.py"]

This Dockerfile does the following:

  • Starts from a base Ubuntu 20.04 image.
  • Adds the deadsnakes/ppa repository.
  • Installs Python 3.11 and pip.
  • Sets the working directory to /app.
  • Copies requirements.txt (if you have one) and installs the listed dependencies.
  • Copies your application code.
  • Sets the command to run your Python script.

To build and run this Dockerfile:

docker build -t my-python-app .
docker run my-python-app

Method 4: Using pyenv

pyenv is a version management tool that allows you to easily switch between multiple Python versions. This is excellent for development environments where you might need to work with different Python versions on the same machine.

  1. Install pyenv: Use apt to install pyenv‘s dependencies.

    sudo apt update
    sudo apt install -y make build-essential libssl-dev zlib1g-dev 
    libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncursesw5-dev 
    xz-utils tk-dev libffi-dev liblzma-dev python3-dev

    Then, install pyenv itself using curl:

    curl https://pyenv.run | bash
  2. Configure pyenv: Add the following lines to your ~/.bashrc file:

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

    Reload your ~/.bashrc file:

    source ~/.bashrc
  3. Install Python 3.11: Use pyenv to install Python 3.11.

    pyenv install 3.11.1  # Use the specific version you want
  4. Set the Global Python Version: Set Python 3.11 as the global Python version.

    pyenv global 3.11.1
  5. Verify Installation: Verify that Python 3.11 is now the active version.

    python --version

    This should output Python 3.11.x.

Code Example (Using a local .python-version file):

You can also specify the Python version on a per-project basis by creating a .python-version file in your project directory:

echo "3.11.1" > .python-version
pyenv local 3.11.1

Now, when you cd into that directory, pyenv will automatically switch to Python 3.11.1.

Conclusion

At this point, you have learned to Install Python 3.11 on Ubuntu 20.04 with four different methods: APT PPA Repository, building from source, using Docker, and utilizing pyenv. The Docker and pyenv methods offer additional flexibility and isolation for different development scenarios. Choose the method that best suits your specific needs and preferences for setting up your Python 3.11 environment. Each offers its own advantages for managing Python versions and dependencies on Ubuntu 20.04.
Hope you enjoy it.

Leave a Reply

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