LoginSignup
3
2

More than 5 years have passed since last update.

【Ubuntu16.04】AnsibleでPython3.6のvenv環境を構築する

Posted at

はじめに

Ansibleを使ってPythonをインストールしたら3.5系が入ってしまった。
3.6系を入れたい。

$ python -V
Python 3.5.2

実行環境

  • Ubuntu16.04 LTS
  • Ansible2.5.3

参考情報

リポジトリを追加

公式ページの手順に従いリポジトリを追加する。

- name: Add Python3.6 repository from PPA
  apt_repository:
    repo: 'ppa:jonathonf/python-3.6'

apt-update

- name: Run the equivalent of "apt-get update" as a separate step
  become: yes
  apt:
    update_cache: yes

仮想環境のインストール

python3-venvではなく、python3.6-venvなので注意。

- name: install python36-virtualenv
  become: yes
  apt:
    name: python3.6-venv
    state: latest

apt-installする

明示的にpython3.6python3.6-devを指定する。

- name: Install the packages
  become: yes
  apt:
    name:
      - python-dev
      - python-pip
      - libxml2-dev
      - libxslt1-dev
      - zlib1g-dev
      - libffi-dev
      - libssl-dev
      - python3.6
      - python3.6-dev
    state: present

仮想環境の作成

venv_nameは任意の名称を入れる。

- name: create virtualenv
  shell: python3.6 -m venv venv_name
  args:
    chdir: /home/vagrant

最終的なplaybook

- hosts: all

  tasks:

  - name: Add Python3.6 repository from PPA
    apt_repository:
      repo: 'ppa:jonathonf/python-3.6'

  - name: Update all packages to the latest version
    become: yes
    apt:
      upgrade: dist

  - name: Run the equivalent of "apt-get update" as a separate step
    become: yes
    apt:
      update_cache: yes

  - name: Install the packages
    become: yes
    apt:
      name:
        - python-dev
        - python-pip
        - libxml2-dev
        - libxslt1-dev
        - zlib1g-dev
        - libffi-dev
        - libssl-dev
        - python3.6
        - python3.6-dev
      state: present

  - name: install python36-virtualenv
    become: yes
    apt:
      name: python3.6-venv
      state: latest

  - name: create virtualenv
    shell: python3.6 -m venv venv_name
    args:
      chdir: /home/vagrant

実行結果確認

無事にvenv環境でPython3.6の環境ができました!

$ . venv_name/bin/activate
(venv_name) $ python -V
Python 3.5.2
(venv_name) $ python3.6 -V
Python 3.6.5
3
2
0

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
3
2