Install Python on Linux
Checking the Python installation in Linux
Before taking decisive action it is desirable to check whether
Python is installed in your Linux distribution by default.
Run the commands
python --version
Python 2.7.16
python3 --version
Python 3.7.3
As you can see, in my
Debian
Python 2.7.16 and Python 3.7.3 are installed out of the box
If you do not have Python by default, go to the next step.
Before installing Python I recommend installing the libraries for
working with ssl.
Otherwise, in the future you may encounter the error
SSL module is not available
Installing with the package manager
The simplest way is to use the package manager.
On
Debian
,
Ubuntu
and other .deb distributions it is apt
On
CentOS
,
Rocky
,
RedHat and other .rpm distributions it is yum
Run
sudo apt update -y
sudo apt-get install -y python3
Or
sudo yum update -y
sudo yum install -y python3
If something does not work, go to the next step
Download Python
Repository:
https://www.python.org/ftp/python/
An example of downloading with wget
wget https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tgz
Download and install Python 2.7.9
To install Python from the downloaded archive you need a compiler C for example gcc
Repository:
https://www.python.org/ftp/python/
If you need to install a second Python, the latest version 2.7.9
wget https://www.python.org/ftp/python/2.7.9/Python-2.7.9.tgz
--2021-01-11 12:17:43-- https://www.python.org/ftp/python/2.7.9/Python-2.7.9.tgz Resolving www.python.org (www.python.org)... 151.101.84.223, 2a04:4e42:14::223 Connecting to www.python.org (www.python.org)|151.101.84.223|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 16657930 (16M) [application/octet-stream] Saving to: ‘Python-2.7.9.tgz’ Python-2.7.9.tgz 100%[=============================================================>] 15.89M 3.63MB/s in 4.4s 2021-01-11 12:17:48 (3.61 MB/s) - ‘Python-2.7.9.tgz’ saved [16657930/16657930]
The archive can be unpacked with the command
tar xvzf Python-2.7.9.tgz
Then you need to go into the unpacked directory and run configure make install
cd Python-2.7.9
./configure
make
sudo make install
An example of a Make file for installing a working environment
.PHONY: preinstall-env preinstall-env: @sudo apt update @sudo apt upgrade @sudo apt-get install -y build-essential libssl-dev zlib1g-dev \ libbz2-dev libreadline-dev libsqlite3-dev libffi-dev \ wget llvm \ libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev \ liblzma-dev curl git #@curl https://pyenv.run | bash curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash @echo "" >> ~/.bashrc @echo 'eval "$$(pyenv virtualenv-init -)"' >> /home/$$(whoami)/.bashrc @echo "" >> ~/.bashrc @echo 'eval "$$(pyenv init -)"' >> ~/.bashrc @export PYENV_ROOT="/home/$$(whoami)/.pyenv" @export PATH="$PYENV_ROOT/bin:$PATH"
Download and install Python 3
Before installing Python 3 I recommend installing the libraries libbz2-dev libffi-dev libssl-dev
sudo apt-get install -y libffi-dev libbz2-dev libffi-dev libssl-dev
They are far from always needed, but if one of them is needed and missing, Python will have to be reinstalled
Read more about the libraries that may be useful below in the paragraph
Makefile
Read more about the utility
wget
in the article
«Downloading from the internet in Linux»
Below you can study examples of downloading and installing specific versions of Python.
Download and install Python 3.11.3
wget https://www.python.org/ftp/python/3.11.3/Python-3.11.3.tgz ; tar xvzf Python-3.11.3.tgz ; cd Python-3.11.3 ; ./configure ; sudo make install
Perhaps after installation it will be useful to add a new one to the current profile alias
alias python="/usr/local/bin/python3.11"
Download and install Python 3.9.1
wget https://www.python.org/ftp/python/3.9.1/Python-3.9.1.tgz ; tar xvzf Python-3.9.1.tgz ; cd Python-3.9.1 ; ./configure ; sudo make install
Installing Python 3.9.13 on CentOS7
sudo yum -y install gcc zlib-devel zlib bzip2-devel libffi-devel openssl-devel wget make
wget https://www.python.org/ftp/python/3.9.13/Python-3.9.13.tgz ; tar xvzf Python-3.9.13.tgz ; cd Python-3.9.13 ; ./configure ; sudo make install
pyenv: installing several different versions
You can read about installing different versions of python on one system and managing these versions and the virtual environments in them in the article pyenv
Installing Pip
sudo apt update
sudo apt install python3-pip
pip3 --version
pip 18.1 from /usr/lib/python3/dist-packages/pip (python 3.7)
About pip
The libraries are in /home/andrei/.local/lib
ls -la /home/andrei/.local/lib
total 0
drwx------ 0 andrei andrei 512 Mar 20 16:41 .
drwx------ 0 andrei andrei 512 Mar 19 13:31 ..
drwx------ 0 andrei andrei 512 Mar 19 13:19 python2.7
drwx------ 0 andrei andrei 512 Mar 20 15:01 python3.5
An example of a Make file for installing a working environment
.PHONY: preinstall-env preinstall-env: @sudo apt -y update @sudo apt -y upgrade @sudo apt-get install -y build-essential libssl-dev zlib1g-dev \ libbz2-dev libreadline-dev libsqlite3-dev libffi-dev \ wget llvm \ libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev \ liblzma-dev curl git @curl https://pyenv.run | bash # substitute for # curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash @echo "# Pyenv Config" >> ~/.bashrc @echo 'export PYENV_ROOT="$$HOME/.pyenv"' >> /home/$$(whoami)/.bashrc @echo 'export PATH="$$PYENV_ROOT/bin:$$PATH"' >> /home/$$(whoami)/.bashrc @echo 'eval "$$(pyenv init --path)"' >> /home/$$(whoami)/.bashrc @echo 'eval "$$(pyenv virtualenv-init -)"' >> /home/$$(whoami)/.bashrc
In .bashrc it will be written
# PyEnv Configuration export PYENV_ROOT="$HOME/.pyenv" export PATH="$PYENV_ROOT/bin:$PATH" eval "$(pyenv init --path)" eval "$(pyenv virtualenv-init -)"
The shell can be restarted by running
exec "$SHELL"
Article author: Andrei Olegovich