python|March 03, 2021|1 min read

How to Install Python from command line and Docker on Linux

TL;DR

Install Python on Linux using pyenv from the command line with all required dependencies, and create a Dockerfile for containerized Python environments on CentOS 7.

How to Install Python from command line and Docker on Linux

Introduction

We will see how we can install Python from command line using pyenv, and we will also create a Dockerfile.

Install Python from command line

I will be working on Centos-7 machine.

Install dependencies and update

yum -y update
yum -y install git \
    libffi-devel libffi libssh2-devel autoconf automake libtool \
    libxml2-devel libxslt-devel libjpeg-devel zlib-devel \
    make cmake gcc python-devel python-setuptools \
    bzip2-devel sqlite-devel wget openssl

Install Pyenv

PYENV_INSTALLER_URL=https://raw.githubusercontent.com/pyenv/pyenv-installer/master/bin/pyenv-installer
PYTHON_CONFIGURE_OPTS="--enable-shared"
RUN umask 022
curl -s -S -L "$PYENV_INSTALLER_URL" -o /usr/bin/pyenv-installer
chmod 0755 /usr/bin/pyenv-installer
/usr/bin/pyenv-installer
export PATH="/root/.pyenv/bin:$PATH"
eval "$(pyenv init -)"

Install Python 3.9.2

PYENV_VERSION=3.9.2 pyenv install $PYENV_VERSION pyenv global $PYENV_VERSION

eval ”$(pyenv init -)” python —version


## Complete Dockerfile for Installing Python 3.9.2

FROM centos:7 RUN yum makecache fast && yum -y update RUN yum -y install git
libffi-devel openssl-devel libffi libssh2-devel autoconf automake libtool
libxml2-devel libxslt-devel libjpeg-devel zlib-devel
make cmake gcc python-devel python-setuptools wget openssl
bzip2-devel sqlite-devel
&& yum clean all
&& rm -rf /var/cache/yum

ARG PYENV_VERSION=3.9.2 ENV PYENV_INSTALLER_URL=https://raw.githubusercontent.com/pyenv/pyenv-installer/master/bin/pyenv-installer ENV PYTHON_CONFIGURE_OPTS=“—enable-shared” ENV PATH=/root/.pyenv/bin:$PATH RUN umask 022
&& curl -s -S -L “$PYENV_INSTALLER_URL” -o /usr/bin/pyenv-installer
&& chmod 0755 /usr/bin/pyenv-installer
&& /usr/bin/pyenv-installer
&& eval ”$(pyenv init -)”
&& pyenv install $PYENV_VERSION
&& pyenv global $PYENV_VERSION

Related Posts

Python - How to apply patch to Python and Install Python via Pyenv

Python - How to apply patch to Python and Install Python via Pyenv

Introduction In this post, we will see how we can apply a patch to Python and…

How to Install Python from command line and Docker on Windows

How to Install Python from command line and Docker on Windows

Introduction We will see how we can install Python 3.7 on Windows without UI. i…

Python SMTP Email Code - How to Send HTML Email from Python Code with Authentication at SMTP Server

Python SMTP Email Code - How to Send HTML Email from Python Code with Authentication at SMTP Server

Introduction This post has the complete code to send email through smtp server…

Python - How to Maintain Quality Build Process Using Pylint and Unittest Coverage With Minimum Threshold Values

Python - How to Maintain Quality Build Process Using Pylint and Unittest Coverage With Minimum Threshold Values

Introduction It is very important to introduce few process so that your code and…

Python - How to Implement Timed-Function which gets Timeout After Specified Max Timeout Value

Python - How to Implement Timed-Function which gets Timeout After Specified Max Timeout Value

Introduction We often require to execute in timed manner, i.e. to specify a max…

How to Solve Circular Import Error in Python

How to Solve Circular Import Error in Python

Introduction To give some context, I have two python files. (Both in same folder…

Latest Posts

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Most developers use Claude Code like a search engine — ask a question, get an…

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Every office lobby has the same problem: a visitor walks in, nobody’s at the…

Server Security Best Practices — Complete Hardening Guide for Production Systems

Server Security Best Practices — Complete Hardening Guide for Production Systems

Every breach post-mortem tells the same story: an unpatched service, a…

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

If you’re a Senior Engineer (L5) preparing for Staff (L6+) roles at MAANG…

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF have been in the OWASP Top 10 for over a decade. They’re among the…

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

The OWASP Top 10 is the industry standard for web application security risks. If…