This guide will discuss how to install Python manually on a Linux machine. For your convenience, we will also discuss how to uninstall Python installed in this way.

Steps to Follow to Install Python Manually

First of all, we need to update package repositories and install dependencies.

Step 1: Update repositories

On Debian-based distributions, execute (modify the commands according to the distro you are running):

1
2
3
4
sudo apt update
sudo apt install build-essential zlib1g-dev \
libncurses5-dev libgdbm-dev libnss3-dev \
libssl-dev libreadline-dev libffi-dev curl

Step 2: Download the stable release of Python on its official website

In this step, go to https://www.python.org/downloads/source/ and download XZ compressed source tarball (.tar.xz) file. This file contains all the source files we can build to get the Python we want (I am downloading Python 3.10.5, so I get, Python-3.10.5.tar.xz file).

Step 3: Extract the tarball

Use the inbuilt extraction functionality to extract the tarball, or you can use the tar command in Python as follows

1
tar -xf Python-****.tar.xz

(In my case, I have to run tar -xf Python-3.10.5.tar.xz)

Step 3: Run the configuration This is accomplished by running the following command on Linux Terminal

1
cd Python-****/ && ./configure

That is, cd into the extracted directory and run configure file.

Step 4: Build the package

Since we want to install this Python version along with the preinstalled one, we will run:

1
sudo make altinstall

Now, Python 3.10 is installed, and we can wake it up by running

1
python3.10 

or

1
/usr/local/bin/python3.10

Remove Python install Manually

To do that, save the following commands on the bash file named “uninstall_python.sh” (you can give it any name, really) and execute it with sudo privileges.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
loc='/usr/local/'
py_version="$1"
rm -rf \
    $HOME/.local/lib/Python${py_version} \
    ${loc}bin/python${py_version} \
    ${loc}bin/python${py_version}-config \
    ${loc}bin/pip${py_version} \
    ${loc}bin/include/python${py_version} \
    ${loc}lib/libpython${py_version}.a \
    ${loc}lib/python${py_version} \
    ${loc}lib/pkgconfig/python-${py_version}.pc \
    ${loc}lib/libpython${py_version}m.a \
    ${loc}bin/python${py_version}m \
    ${loc}bin/2to3-${py_version} \
    ${loc}bin/python${py_version}m-config \
    ${loc}bin/idle${py_version} \
    ${loc}bin/pydoc${py_version} \
    ${loc}bin/pyvenv-${py_version} \
    ${loc}share/man/man1/python${py_version}.1 \
    ${loc}include/python${py_version}m \
    ${loc}bin/easy_install-${py_version}

Execute the bash script by running:

1
sudo bash <location of uninstall_python.sh> <python version to remove>

In my case, I will cd into the location of uninstall_python.sh and run the command.

1
sudo bash uninstall_python.sh 3.10