AWS

[Pyenv] AWS EC2(Ubuntu 20.04LTS)에 Pyenv 설치

easysheep 2023. 10. 11. 13:38

1. 설치

# pyenv 를 ~/.pyenv경로에 설치
git clone https://github.com/pyenv/pyenv.git ~/.pyenv


# pyenv를 빠르게 사용하기 위한 옵션 설정 (이부분은 오류가 발생해도 됨)
cd ~/.pyenv && src/configure && make -C src


# bashrc에 경로 추가
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc




# 필요라이브러리 설치
sudo apt update
sudo apt install build-essential libssl-dev zlib1g-dev \
libbz2-dev libreadline-dev libsqlite3-dev curl \
libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev

# bash shell 재시작
source ~/.bashrc

#pyenv 제대로 설치 되었는 지 확인
pyenv

# virtualenv 플러그인 추가 설치
git clone https://github.com/pyenv/pyenv-virtualenv.git $(pyenv root)/plugins/pyenv-virtualenv

echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc
다음과 같이 쏼라쏼라 나와야함
pyenv 2.3.29
Usage: pyenv <command> [<args>]

Some useful pyenv commands are:
   --version   Display the version of pyenv
   commands    List all available pyenv commands
   exec        Run an executable with the selected Python version
   global      Set or show the global Python version(s)
   help        Display help for a command
   hooks       List hook scripts for a given pyenv command
   init        Configure the shell environment for pyenv
   install     Install a Python version using python-build
   latest      Print the latest installed or known version with the given prefix
   local       Set or show the local application-specific Python version(s)
   prefix      Display prefixes for Python versions
   rehash      Rehash pyenv shims (run this after installing executables)
   root        Display the root directory where versions and shims are kept
   shell       Set or show the shell-specific Python version
   shims       List existing pyenv shims
   uninstall   Uninstall Python versions
   version     Show the current Python version(s) and its origin
   version-file   Detect the file that sets the current pyenv version
   version-name   Show the current Python version
   version-origin   Explain how the current Python version is set
   versions    List all Python versions available to pyenv
   whence      List all Python versions that contain the given executable
   which       Display the full path to an executable

See `pyenv help <command>' for information on a specific command.
For full documentation, see: https://github.com/pyenv/pyenv#readme​

2. 사용법

# 다음의 코드로 설치하고 싶은 파이썬 버전 확인
pyenv install -l
# pyenv 를 위해 3.11.6	버전의 파이썬 설치
pyenv install 3.11.6

# 아래 사진과 같이 오류가 발생한다면 다음의 코드의 마지막 줄을 유의하여 다시 실행하여 보자
sudo apt update
sudo apt install build-essential libssl-dev zlib1g-dev \
libbz2-dev libreadline-dev libsqlite3-dev curl \
libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev

# 설치된 버전 확인
pyenv versions

# 가상환경 구성
# pyenv virtualenv [설치한 파이썬 버전] [이름]
pyenv virtualenv 3.11.6 stock

# 생성한 가상환경 확인
pyenv versions

# 가상 환경 실행
# pyenv activate <name>
pyenv activate stock
# 가상 환경 종료
pyenv deactivate

참고

https://github.com/pyenv/pyenv#installation