How to install Python 2.7 on Ubuntu 24.04 Noble LTS Linux

Canonical has dropped support to Python version 2.x after the long-term Ubuntu 20.04 Focal, to make space for the latest Python 3 version. That’s the reason why we don’t have Python 2 to install on Ubuntu 24.04 using its official system repository. Furthermore, Python developers also don’t support version 2 anymore, however, still, if there is some old project of yours that needs Version 2 of Python then in this article we learn how to install it on Ubuntu 24.04 to run some old applications.

Installing Python 2.7 on Ubuntu 24.04 LTS Linux

Step 1. Open a command terminal

Access the Terminal on your Ubuntu because we need that to install packages, therefore you should have some familiarity with it and how the command line works. Once you are on Terminal, run the system update command:

sudo apt update && sudo apt upgrade

Step 2. Install Dependencies

As we know Python 2.7 is not available anymore through the default system repository of Ubuntu or even via the popular PPA Dead Snake repo, hence here in this article we will compile it using its source file. However, before that install some essential developers tools.

sudo apt install -y build-essential checkinstall libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev libffi-dev

Step 3: Download the Python 2.7 Source Code

Now, use the wget tool and download the source code for Python 2.7 from its official website, the latest version available for 2.7 is 2.7.18.

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

Extract the tarball:

tar -xvf Python-2.7.18.tgz

Step 4: Compile and Install Python 2.7 on Ubuntu 24.04

After extracting, first switch to the extracted directory, and then follow the given procedure to compile the source code.

cd Python-2.7.18
./configure --enable-optimizations

Compile and install Python:

The process of compiling will take some time, so sit relax, and let it complete.

make
sudo make install

Step 5: Verify the Installation

Once you have installed the “make” file, your Ubuntu 24.04 system will have the Python 2.7 installed. To confirm the same, simply check the version by running the given command:

python -V
Installing and Checking Python 2.7 version on Ubuntu 24.04

Step 6: Setting Up pip for Python 2.7

After installing Python 2.7 on Ubuntu 24.04 you may also require PIP for managing libraries and packages so to get it, here are the commands:

sudo apt install curl
curl https://bootstrap.pypa.io/pip/2.7/get-pip.py -o get-pip.py
sudo python2.7 get-pip.py

Verify the pip installation:

pip2.7 --version
PIP 2.7 installation Ubuntu 24.04

Step 7: Change the Default Python priority

If you have both Python 2.7 and 3 on your Ubuntu 24.04 Linux, the system by default would give priority to Python3 and this is why when we run “python" command it will give an error:

Command 'python' not found, did you mean:
command 'python 3' from deb python3
command 'python' from deb python-is-python3

Thus, we will change the priority and set Python2 at the top, so that it can be called by applications as the default version. Whereas Python3 will be second.

Set Alternatives:

sudo update-alternatives --install /usr/bin/python python /usr/local/lib/python2.7 1

sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 2

Now, check the default version:

python -V

In the future, if you want to set Python 3 as the default or first version in the priority list, simply update the alternatives list, using this command:

sudo update-alternatives --config python

You will get something like the below screenshot with the choice to set one as the priority. To select, enter the number given corresponding to each version path under the Selection column and hit the Enter key.

Set Python 2.7 or 3 as default on Ubuntu 24.04

Step 8: Uninstall Python 2.7 from Ubuntu 24.04

If you don’t need Python 2.7 on your system then here is the commands to remove it completely:

sudo rm -rf /usr/local/bin/python2.7
sudo rm -rf /usr/local/bin/python2.7-config
sudo rm -rf /usr/local/lib/python2.7
sudo rm -rf /usr/local/include/python2.7
sudo rm -rf /usr/local/share/man/man1/python2.7.1

Remove Symbolic Links, created during the installation:

sudo rm /usr/local/bin/python2
sudo rm /usr/local/bin/python

Update the update-alternatives System

sudo update-alternatives --remove python /usr/local/bin/python2.7

You may want to set another Python version as the default one:

sudo update-alternatives --config python

Remove Python 2.7 pip and Other Associated Tools

sudo rm /usr/local/bin/pip2.7
sudo rm -rf /usr/local/lib/python2.7/site-packages

Clean Up Remaining Files

rm -rf ~/.local/lib/python2.7
rm -rf ~/.local/bin/pip2
rm -rf ~/.cache/pip

Other Articles:

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.