How to install a specific Python version on Linux
Hello, 👋
In this article I will show you how to install Python versions on Linux using the following methods: compiling from source, dead snakes ppa and pyenv.
To make things easier, if you want to follow along in an environment that you can break, you can create a local Kubernetes cluster using Minikube.
Next, I’m going to use the following yaml file to create an Ubuntu pod:
apiVersion: v1kind: Podmetadata: name: ubuntu labels: app: ubuntuspec: containers: - image: ubuntu command: - "sleep" - "604800" imagePullPolicy: IfNotPresent name: ubuntu restartPolicy: AlwaysSave the above yaml in a file ubuntu_pod.yaml and run:
kubectl apply -f ./ubuntu_pod.yamlTo get a shell on the Ubuntu pod, run:
kubectl exec -it ubuntu -- /bin/bashTo start from scratch, simply delete the pod with kubectl delete pod/ubuntu and then recreate it.
Compiling Python from source
Before compiling Python, you will need to setup the build environment, thankfully, it is straightforward.
Pyenv has great instructions on it: .
On Ubuntu, to build Python, install the following packages:
apt-get update; apt-get install make build-essential libssl-dev zlib1g-dev \libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm \libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-devThen, search the desired python version here and, for example to install Python 3.9, run:
wget https://www.python.org/ftp/python/3.9.9/Python-3.9.9.tgztar -xzf Python-3.9.9.tgzcd Python-3.9.9Then, run configure:
./configure --enable-optimizationsAnd finally run make install if you want to replace the default Python installation or make altinstall to install python under the binary name of python3.9
make altinstall
To test the installation run:
python3.9 --versionPython 3.9.9pip3.9 --versionpip 21.2.4 from /usr/local/lib/python3.9/site-packages/pip (python 3.9)Installing Python via a third party PPA deadsnakes
To install Python using the deadsnakes ppa run:
apt-get updateapt-get install software-properties-commonadd-apt-repository ppa:deadsnakes/ppaapt-get updateapt install python3.9 python3-pipThen, to test the installation run:
root@ubuntu:/# python3.9 --versionPython 3.9.10root@ubuntu:/# python3.9 -m pip --versionpip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.9)Installing Python via Pyenv
I already written an article on how to install Python using Pyenv, check it out if you wish.
/posts/2020/2020-06-27-pyenv-for-linux-users/
Thanks for reading! 📚