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.
/ 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.
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
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
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 /
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
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
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