Site icon Roel Peters

Install a specific Python version in Linux

This blog post gives a very detailed explanation of how to install a specific Python version on a Linux machine. I’m fairly new to Linux myself. Writing this blog post gave me valuable information and history lessons on various Linux commands and programs. I hope it’s helpful for you, too.

Preparing the installation

First, get your installation up to date by using the apt update and apt upgrade commands. The first command will generate a list of all the installed packages and compares them to their latest versions out there. The second command actually upgrades all packages.

We’ll always use sudo to run commands as a superuser.

sudo apt update
sudo apt -y upgrade

With the following command, we’ll download several packages:

sudo apt install wget libssl-dev openssl make gcc zlib1g-dev -y

Downloading and untarring Python

Next, we navigate into the /opt directory. By convention, this directory “is reserved for all the software and add-on packages that are not part of the default installation.

cd /opt

Next, we use wget to download a specific Python version (as a Gzipped source tarball). Find your version over here.

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

After downloading our Python tarball, we’ll untar it.

Finally, we navigate into the untarred folder.

sudo tar -xzvf Python-3.9.2.tgz
cd Python-3.9.2

Installing Python

The following three lines of code will finally proceed with the installation of Python on your Linux system.

./configure
make
sudo make install

Finally, we create a a symbolic link using the ln command inside the /usr/bin folder. This folder contains all executable programs (like Python). We specify two parameters for ln:

sudo ln -fs /opt/Python-3.9.2/python /usr/bin/python3.9

If you want to specify another command than python3.9, you should replace it in the last argument of your ln command. The following command will pame Python available via the py command.

sudo ln -fs /opt/Python-3.9.2/python /usr/bin/py

Putting it all together

Putting it all together, we get the following list of commands:

sudo apt update
sudo apt -y upgrade
sudo apt install wget libssl-dev openssl make gcc zlib1g-dev -y

cd /opt
sudo wget https://www.python.org/ftp/python/3.9.2/Python-3.9.2.tgz
sudo tar xzvf Python-3.9.2.tgz

cd Python-3.9.2
./configure
make
sudo make install

sudo ln -fs /opt/Python-3.9.2/Python /usr/bin/python3.9

python3.9 --version

Great success!

Exit mobile version