How to Install Python on Debian 12

How to Install Python on Debian 12

Spread the love

Python is a versatile programming language that can run on almost any system architecture, from web development to machine learning, and can be used for applications in various fields. Besides its versatility, Python is also relatively easy for beginners to learn, making it one of the most popular programming languages. This tutorial will show you how to install Python on Debian 12.

Prerequisites

  • A Debian 12 VPS
  • SSH root access or a user with sudo privileges is required

Conventions

# – given commands should be executed with root privileges either directly as a root user or by use of sudo command
$ – given commands should be executed as a regular user

Login to VPS and Update the System

First of all, we need to log in to our Debian 12 VPS through SSH:

ssh root@IP_Address -p Port_number

Replace “root” with a user with sudo privileges or root if necessary. Replace “IP_Address” and “Port_Number” with your server’s IP address and SSH port number. Next, let’s make sure that we’re on Debian 12. You can do that like this:

# lsb_release -a

The command should return an output similar to this:

No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux 12 (bookworm)
Release: 12
Codename: bookworm

Before starting, you have to make sure that all Debian packages installed on the server are up to date. You can do this by running the following commands:

# apt update -y

Install Python from Source

Debian 12 ships Python 3.11 as the default version. You can verify this by executing the command below:

# apt show python3

The command will return an output like this:

Package: python3
Version: 3.11.2-1+b1
Priority: optional
Section: python
Source: python3-defaults (3.11.2-1)
Maintainer: Matthias Klose [email protected]
Installed-Size: 82.9 kB
Provides: python3-profiler, python3-supported-max (= 3.11), python3-supported-min (= 3.11)
Pre-Depends: python3-minimal (= 3.11.2-1+b1)
Depends: python3.11 (>= 3.11.2-1~), libpython3-stdlib (= 3.11.2-1+b1)
Suggests: python3-doc (>= 3.11.2-1+b1), python3-tk (>= 3.11.2-1~), python3-venv (>= 3.11.2-1+b1)
Replaces: python3-minimal (<< 3.1.2-2)
Homepage: https://www.python.org/
Tag: devel::interpreter, devel::lang:python, devel::library,
implemented-in::c, implemented-in::python, role::devel-lib,
role::program, role::shared-lib
Download-Size: 26.3 kB
APT-Manual-Installed: no
APT-Sources: http://deb.debian.org/debian bookworm/main amd64 Packages
Description: interactive high-level object-oriented language (default python3 version)
Python, the high-level, interactive object oriented language,
includes an extensive class library with lots of goodies for
network programming, system administration, sounds and graphics.
.
This package is a dependency package, which depends on Debian's default
Python 3 version (currently v3.11).

Ubuntu has deadsnake’s PPA, which allows us to install multiple Python versions on an Ubuntu machine. Although we can install it on our Debian 12 machine using the same Deadsnake’s PPA with a little modification, it is not recommended. So, to install another version of Python 3 on a Debian 12 system, we need to install it from the source. We will show you how to install it.

Install Dependencies

Let’s install the dependencies by invoking this command below.

# apt install openssl \
build-essential \
curl \
gcc \
libbz2-dev \
libev-dev \
libffi-dev \
libgdbm-dev \
liblzma-dev \
libncurses-dev \
libreadline-dev \
libsqlite3-dev \
libssl-dev \
make \
tk-dev \
wget \
git \
zlib1g-dev

Download and Install Python 3.12.3

For example, you want to install Python 3.12. So, you need to go to Python download page and get the link. In this example, you want to download Python 3.12.4.

Now, after getting the download link, let’s download it.

# cd /tmp
# wget https://www.python.org/ftp/python/3.12.3/Python-3.12.3.tgz

Then, extract the downloaded file and go to the directory Python-3.12.3

# tar zxf Python-3.12.3.tgz
# cd Python-3.12.3

Finally, we can install it.

# ./configure --prefix=/usr/local
# make
# make install

That’s it — you have Python 3.12 now. It’s installed at /usr/local/bin/python3.12. To install multiple versions, repeat the steps and specify the versions you want to install.

Install Python using pyenv

Before installing pyenv, we need to get the latest version of pyenv frm GitHub. Let’s execute this command below.

# git clone https://github.com/pyenv/pyenv.git ~/.pyenv

Now, we need to edit our ~/.bashrc file

# nano ~/.bashrc

and add these lines to it.

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

Save the file and exit from the editor. To apply the changes, we can source the file.

# source ~/.bashrc

At this point, we can use pyenv. Run this command below to see the available Python versions to install.

# pyenv install -l

Let’s say we are going to install Python 3.10, we can execute this command:

# pyenv install 3.10

Wait until it finishes installing Python. Once completed, you can run this command to check the available version.

# pyenv versions
* system (set by /root/.pyenv/version)
3.10.14

As your screen shows, Python 3.10.14 has been installed but is not activated. The one listed there with an asterisk (*) is currently the active one. The asterisk indicates the currently active version and refers to the system-wide Python version.

To activate Python 3.10.14 globally, let’s run this command

# pyenv global 3.10.14

That’s it. Python 3.10.14 is active now. You can verify it by running the command

# python3 --version

It will return an output like this:

Python 3.10.14

Besides activating it globally, you can also use it locally. Use this command instead:

# pyenv local 3.10.14

You can also install a virtual environment when using pyenv. The command is similar to the one you usually use.

# python -m venv [VENV-NAME]

That’s it all. You have learned how to install Python on Debian 12.

Of course, you don’t have to install Python on Debian 12 yourself if you use one of our server management plans. This means that you can ask our admins to do the installation for you, sit back, and relax. Our admins will install and set up Python on Debian 12 immediately without any additional fee, along with any helpful configurations and optimizations we can do for you. Managing Python is not just about the installation, though. We can also help you optimize your Python installation if you have an active service with us.

If you liked this post on installing Python on Debian 12, please share it with your friends on social media or leave a comment below. Thanks.

Leave a Reply

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