LoginSignup
91
85

More than 5 years have passed since last update.

EC2サーバにPython3環境構築

Posted at

やりたいこと

AWSのEC2サーバ上で、pyenv + Python最新版が動く環境を作る

インスタンス情報

  • インスタンスタイプ t2.micro
  • Amazon Linux AMI 2017.03.1.20170812 x86_64 HVM

pyenvインストール

githubのリポジトリからクローンしてきます
gitが入ってない場合は先にyumでgitをインストールします

$ sudo yum install git -y

$ git clone https://github.com/yyuu/pyenv.git ~/.pyenv
Cloning into '/home/ec2-user/.pyenv'...
remote: Counting objects: 15477, done.
remote: Compressing objects: 100% (7/7), done.
remote: Total 15477 (delta 1), reused 6 (delta 1), pack-reused 15468
Receiving objects: 100% (15477/15477), 2.74 MiB | 2.15 MiB/s, done.
Resolving deltas: 100% (10593/10593), done.

リポジトリをクローンしてきたらパスを通してコマンドが打てるようにします

$ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
$ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(pyenv init -)"' >> ~/.bash_profile
$ source ~/.bash_profile

ちゃんとコマンドが通るか確認

$ pyenv -v
pyenv 1.1.4-4-g433e2d1

pythonインストール

必要な依存関係のインストール

$ sudo yum install gcc zlib-devel bzip2 bzip2-devel readline readline-devel sqlite sqlite-devel openssl openssl-devel -y

本体のインストール

$ pyenv install 3.6.2
Downloading Python-3.6.2.tar.xz...
-> https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tar.xz
Installing Python-3.6.2...
Installed Python-3.6.2 to /home/ec2-user/.pyenv/versions/3.6.2

バージョンをインストールしてきたものに切り替え

以下のコマンドでこのOSで使用するPythonのバージョンを宣言

$ pyenv global 3.6.2
$ pyenv rehash

ちゃんと切り替わったか確認

$ python --version
Python 3.6.2

成功です

91
85
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
91
85