Building a Python virtual environment

Introduction

When developing with Python, do not use the OS standard Python installed in System or the Python version installed via the OS package management tool. The reason is that development depends on those Pythons and their modules, and you can develop freely without worrying about dependencies.

Python installation destination / opt

According to Filesystem Hierarchy Standard (FHS), / usr / local is the area to install the software used locally when managing the system. I will. / opt is the area to install the necessary software as an accessory (add-on). In other words, according to FHS, / opt is appropriate.

Install directly on ubuntu

Environmental preparation

apt-get


# apt-Install the required packages with get
$ sudo apt-get install -y gcc make build-essential libssl-dev zlib1g-dev libbz2-dev \
    libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev \
    xz-utils tk-dev libffi-dev liblzma-dev python-openssl

Installation

python.org


#Go to the source download destination
$ cd $HOME/src

#Download the source file using wget
$ wget https://www.python.org/ftp/python/3.8.0/Python-3.8.0.tgz

#Use tar to unpack the source files and move to the directory containing the sources
$ tar zxvf Python-3.8.0.tgz && cd Python-3.8.0

#Specify the installation destination and set the compile environment
$ ./configure --prefix=/opt/python/versions/3.8.0

#Build python from source
$ make

#Install the compiled binary file in the specified directory
$ sudo -H make install

Install directly on MAC OS

Preparing the environment

MacOS


#Prepare the brew environment
$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

#If brew is already included, delete pyenv
$ brew uninstall pyenv

#Install readline
$ brew install readline xz [email protected]

#Install wget to download files
$ brew install wget

#Mac build environment xcode-Install using select (xcode must be installed)
$ sudo xcode-select --install

#Prepare header
$ sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target /

Installation

python.org


#Go to the source download destination
$ cd $HOME/src

#Download the source file using wget
$ wget https://www.python.org/ftp/python/3.8.0/Python-3.8.0.tgz

#Use tar to unpack the source files and move to the directory containing the sources
$ tar zxvf Python-3.8.0.tgz && cd Python-3.8.0

#Compile environment variable preparation
$ export CFLAGS="-I$(brew --prefix [email protected])/include"
$ export LDFLAGS="-L$(brew --prefix [email protected])/lib -L$(brew --prefix readline)/lib"
# $ export CC='/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc'
# $ export CPP='/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc -E'

#Specify the installation destination and set the compile environment
$ ./configure --prefix=/opt/python/versions/3.8.0 --with-openssl="(brew --prefix [email protected])"

#Set the compile environment by specifying the installation destination and the location of the openssl library.
#Depending on the environment, the location of openssl may not be set in the environment, so set it just in case
$ make

#Install the compiled binary file in the specified directory
$ sudo -H make install

When installing with pyenv

First, prepare the environment for Ubuntu or Mac

pyenv


#Set the installation destination
$ cd /opt

#Create a directory python and save the pyenv environment there
$ sudo git clone git://github.com/yyuu/pyenv.git ./python

#Create the required directories
$ sudo mkdir python/versions python/shims


# ---------------------------- #
#Specify a group to prepare the environment
# ---------------------------- #

##For MacOS
###Set staff group permissions to the created directory
###Change directory group permissions
$ sudo chown root:staff python/versions/ python/shims/

# or

##For Ubuntu
###Group creation
$ sudo groupadd pyenv
###Add yourself to the group
$ sudo usermod -aG pyenv ${USER} 
###Change directory group permissions
$ sudo chown root:pyenv python/versions/ python/shims/

# ---------------------------- #


#Grant write permission of the group to the created directory
$ sudo chmod 2775 python/versions/ python/shims/

#Specify the installation destination of python to be installed with pyenv
$ export PYENV_ROOT=/opt/python

#Initialize the pyenv environment
$ eval "$(/opt/python/bin/pyenv init -)"

#Install python with pyenv
$ /opt/python/bin/pyenv install -v 3.8.0

Virtual environment settings

python


$ /opt/python/versions/3.8.0/bin/python3 -m venv venv
$ source venv/bin/activate
(venv) $ python -V
Python 3.8.0

$ sudo /opt/python/versions/2.7.16/bin/python -m ensurepip
$ sudo /opt/python/versions/2.7.16/bin/pip install -U pip
$ sudo /opt/python/versions/2.7.16/bin/pip install virtualenv
$ /opt/python/versions/2.7.16/bin/virtualenv venv27
$ source venv27/bin/activate
(venv27) $ python -V
Python 2.7.16

Recommended Posts

Building a Python virtual environment
Building a Python virtual environment
Building a virtual environment with Python 3
[Mac] Building a virtual environment for Python
[Python] Web development preparation (building a virtual environment)
Building a Python environment on Mac
Building a Python environment on Ubuntu
Create a virtual environment with Python!
Building and enabling a python virtual environment, etc. (venv)
[Pyenv] Building a python environment with ubuntu 16.04
Building a Python3 environment with Amazon Linux2
Building a virtual environment using homebrew + pyenv-virtualenv
Let's create a virtual environment for Python
[Python] Create a virtual environment with Anaconda
Building a Python 3.6 environment with Windows + PowerShell
python virtual environment Pipenv
Creating a python virtual environment on Windows
virtual environment in python
Create a Python environment
Build a python virtual environment with pyenv
Building a Python development environment for AI development
Add a Python virtual environment to VSCode
Virtual environment with Python 3.6
From installing Ansible to building a Python environment in Vagrant's virtual environment
How to create a Python virtual environment (venv)
Building a python environment with virtualenv and direnv
Building a Python environment with WLS2 + Anaconda + PyCharm
Build a simple Python virtual environment without pyenv
Think about building a Python 3 environment in a Mac environment
[Venv] Create a python virtual environment on Ubuntu
Work in a virtual environment with Python virtualenv.
Building a Python environment on a Sakura VPS server
Create a Python virtual development environment on Windows
venv: Python virtual environment management
Build a Python environment offline
python standard virtual environment venv
Building a virtual environment for Mayavi dedicated to Python 3.6, Anaconda, Spyder users
Building a Python environment for pyenv, pyenv-virtualenv, Anaconda (Miniconda)
Python: Creating a virtual environment (venv), starting and stopping
Recommendation of building a portable Python environment with conda
Building a Docker working environment for R and Python
Build a python virtual environment with virtualenv and virtualenvwrapper
Build a Python virtual environment using venv (Django + MySQL ①)
Procedure for building a CDK environment on Windows (Python)
Build a python virtual environment with virtualenv and virtualenvwrapper
conda memorandum: Building a Python environment with supercomputer ITO
Building a Python environment for programming beginners (Mac OS)
Memo for building a machine learning environment using Python
Building a kubernetes environment with ansible 2
Virtual Environment Version Control Summary Python
Error when building mac python environment
Build python virtual environment with virtualenv
[Personal memo] Python virtual environment command memo
Create a Python environment on Mac (2017/4)
Create a python environment on centos
Build a python3 environment on CentOS7
Building a kubernetes environment with ansible 1
How to build a new python virtual environment on Ubuntu
Building a python environment for artificial intelligence (Chainer / TensorFlow / CSLAIER)
Building a Python environment on a Mac and using Jupyter lab
[Django3] Display a web page in Django3 + WSL + Python virtual environment