Introducing Python using pyenv on Ubuntu 20.04

This article summarizes the steps to deploy Python on Ubuntu 20.04 using pyenv.

Note

--Let's check if pyenv is needed in the first place. It's not easy to get Python that works as expected with pyenv. (I wandered the net for about an hour.) ――For those who just want Python, if you don't want to have a hard time building the environment, we recommend relying on the OS standard package (sudo apt install python3-pip for Ubuntu) or Anaconda. .. ――Personally, I think that pyenv can only be used by someone who can accept the trouble of searching the net for countermeasures when there is a problem in building an environment. It would be convenient if it could be introduced correctly ... --Some of the steps below depend on Ubuntu 20.04. It doesn't even support Ubuntu 18.04. ――For the time being, I have put a link to the page that describes the procedure for users other than Ubuntu 20.04.

environment

Installation procedure

1. Preparation of necessary commands

First, install the basic commands used to install pyenv itself and install Python through pyenv.

$ sudo apt install git wget curl

2. Installation of libraries that Python depends on (environment-dependent)

Next, install the libraries that Python depends on. When you install CPython with pyenv, it builds from the source code, so install all the necessary libraries. (This is a difficult point.)

If your OS is not Ubuntu 20.04, read 1. Getting Started — Python Developer's Guide.

On Ubuntu 20.04, in /etc/apt/sources.list

deb-src http://archive.ubuntu.com/ubuntu/ focal-updates main

And execute the following command.

$ sudo apt update
$ sudo apt build-dep python3.8

Note that it is apt build-dep, not apt install. apt build-dep is a command that installs all the packages needed to build from source code. Here we have installed the packages needed to build CPython 3.8. You need to choose a package that includes minor versions, such as python3.8 instead of python3, but it doesn't have to match the version of Python you want to use. (If the minor version is different, the dependent library will not change.)

3. Download pyenv

Download Pyenv by cloning the source code. The Python you are about to install will be placed in the cloned repository, and it will be in your PATH, so copy and paste the following command unless you have a specific reason.

$ git clone https://github.com/pyenv/pyenv.git ~/.pyenv

Check out the tag for the version of pyenv you want to use accordingly. (For the time being, I think you should use the tag of the latest release.) If it is the latest version as of 2020/12/27, it will be as follows.

cd ~/.pyenv
git checkout v1.2.21

4. Addition of environment variables (environment dependent)

In Ubuntu 20.04, the console is usually bash, so add the following to the .bashrc file.

PYENV_ROOT="${HOME}/.pyenv"
PATH="${PYENV_ROOT}/bin:${PATH}"
if command -v pyenv 1>/dev/null 2>&1; then
  eval "$(pyenv init -)"
fi

If your console is not bash, check the pyenv README.

5. Install Python

Install your favorite Python. I have CPython 3.9.0 installed.

$ pyenv install 3.9.0

It will take some time to build from the source code.

6. Operation check

After installation, please check the operation. If you don't have enough dependent libraries, you will be told that "_ctypes are missing" at the package import stage, so it is recommended to install the package you want to use and check until it is imported. I will.

Afterword

I wrote what I originally wrote in Personal Note with the intention of publishing it to Qiita. I hope it will be useful for those who are having trouble with pyenv.

Recommended Posts

Introducing Python using pyenv on Ubuntu 20.04
Introducing TensorFlow on Ubuntu + Python 2.7
Preparing python using vscode on ubuntu
Notes on installing Python using PyEnv
Install Python on CentOS using Pyenv
Install Python on CentOS using pyenv
Until building a Python development environment using pyenv on Ubuntu 20.04
Install pyenv and Python 3.6.8 on Ubuntu 18.04 LTS
build Python on Ubuntu
Install Python 3.3 on Ubuntu 12.04
Installing pyenv on ubuntu 16.04
Build python environment with pyenv on EC2 (ubuntu)
Build Python3.5 + matplotlib environment on Ubuntu 12 using Anaconda
Install Python 3.8.6 on macOS Big Sur using pyenv
Python development on Ubuntu on AWS EC2 (using JupyterLab)
Set up Python 3.4 on Ubuntu
Use matplotlib on Ubuntu 12 & Python
Put Python 3.x on Ubuntu
Change python version using pyenv
Install OpenCV on Ubuntu + python
Broadcast on LINE using python
Build a Python environment on your Mac using pyenv
Troublesome story when using Python3 with VScode on ubuntu
Build a Python development environment using pyenv on MacOS
Minimum memo when using Python on Mac (pyenv edition)
Install Python 3.8 on Ubuntu 18.04 (OS standard)
Python 3.4 pyvenv fails on Ubuntu 14.04 LTS
[Ubuntu] [Python] Object tracking using dlib
Building a Python environment on Ubuntu
Notes on using MeCab from Python
Study on Tokyo Rent Using Python (3-2)
Using a serial console on Ubuntu 20.04
Notes on using rstrip with python.
Install Python 3.9 on Ubuntu 20.04 (OS standard?)
Install confluent-kafka for Python on Ubuntu
Study on Tokyo Rent Using Python (3-3)
Install Python 2.7 on Ubuntu 20.04 (OS standard?)
Build Python 3.8 + Pipenv environment on Ubuntu 18.04
Run Yocto on Ubuntu using QEMU.
(Beginner) Notes on using pyenv on Mac
OpenCV3 & Python3 environment construction on Ubuntu
Ansible playbook for setting up Python preferences using pyenv on Mac
How to build a Python environment using Virtualenv on Ubuntu 18.04 LTS
[Pyenv] Building a python environment with ubuntu 16.04
Notes for using OpenCV on Windows10 Python 3.8.3.
[Ubuntu 18.04] Python environment construction with pyenv + pipenv
Execute Python code on C ++ (using Boost.Python)
Detect "brightness" using python on Raspberry Pi 3!
Build Python3 and OpenCV environment on Ubuntu 18.04
Python virtual environment and packages on Ubuntu
Introducing MAMP + Python + SQLAlchemy (Mysql-Python) on Mac
[Ubuntu] [Python] Facial organ detection using dlib
Put Anaconda on your Mac using Pyenv
Install python library on Lambda using [/ tmp]
Steps to install Python environment on Ubuntu
Notes on building Python and pyenv on Mac
Created Ubuntu, Python, OpenCV environment on Docker
Run servomotor on Raspberry Pi 3 using python
Study on Tokyo Rent Using Python (3-1 of 3)
Detect temperature using python on Raspberry Pi 3!
Video processing using Python + OpenCV on Mac