Skip to content
Home ยป Install a specific Python version in Linux

Install a specific Python version in Linux

  • by
  • 3 min read

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.

  • x means extract, which is what you want to to with the tarball.
  • z means gzip, which is the compression method.
  • v means verbose, which will print all the extracted files.
  • f tells tar that the next argument is the file to untar.

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.

  • The configure script takes the necessary preparations to build the software for your specific system.
  • The make command builds the software from the source code.
  • The make install command installs the software and puts all libraries and dependencies in their proper locations.
./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:

  • f means force, it enables overwriting any existing symlinks in the same location
  • s means symbolic, it specifies you want to create a symbolic link, not a hard link. Comparison over here.
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!

Leave a Reply

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