This article summarizes the steps to deploy Python on Ubuntu 20.04 using pyenv.
--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.
First, install the basic commands used to install pyenv itself and install Python through pyenv.
$ sudo apt install git wget curl
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.)
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
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.
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.
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.
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