I would like to install Python 2.7.10 from source on CentOS using Ansible. This time it is Python, but if you use it, you can use it to install other libraries.
Here's what I want to do. / usr / local /
is specified as the installation destination, and / var / tmp /
is specified as the temporary save destination of the source.
yum install gcc
wget https://www.python.org/ftp/python/2.7.10/Python-2.7.10.tgz -P /var/tmp/
tar xvzf /var/tmp/Python-2.7.10.tgz -C /var/tmp/
cd /var/tmp/Python-2.7.10/
./configure CFLAGS=-fPIC --enable-shared --prefix=/usr/local/
make
make install
ln /usr/local/lib/libpython2.7.so.1.0 /lib64/libpython2.7.so.1.0
I'm also installing gcc
as it may cause an error when I run configure
. I installed it when I ran make install
, but I got an error when I ran the installed Python, so I put a symbolic link at the end.
Reference: [Python error handling: error while loading shared libraries: libpython2.7.so.1.0: cannot open shared object file: No such file or directory](http://kenzo0107.hatenablog.com/entry/2015/02/ 17/114506)
playbook
- hosts: all
become: yes
tasks:
- name: Install gcc
yum: name=gcc state=latest
- name: Create tmp directory
file: path=/var/tmp/Python-2.7.10 state=directory
- name: Download Python
get_url: >
url=https://www.python.org/ftp/python/2.7.10/Python-2.7.10.tgz
dest=/var/tmp/Python-2.7.10.tgz
creates=/usr/local/bin/python
environment:
LANG: C
LC_ALL: C
LC_MESSAGES: C
- name: Unarchive Python
unarchive: src=/var/tmp/Python-2.7.10.tgz dest=/var/tmp/ copy=no
- name: Install Python
command: >
{{ item }}
chdir=/var/tmp/Python-2.7.10/
creates=/usr/local/bin/python
with_items:
- './configure CFLAGS=-fPIC --enable-shared --prefix=/usr/local/'
- make
- make install
- name: Link Python lib
file: src=/usr/local/lib/libpython2.7.so.1.0 dest=/lib64/libpython2.7.so.1.0 state=link
Install gcc
As mentioned above, if there is no gcc
, an error will occur, so I installed it with yum
.
Create tmp directory
When I ran ʻUnarchive Python` after this, I needed the destination directory, so I created it here in advance.
Download Python
Download the Python source. If it already exists, it will not be downloaded.
Unarchive Python
Unzip tgz. If copy = yes
, it refers to the file of the host running Ansible. This time, I set copy = no
because I want to refer to the file on the server. When I did this, I ran into the error'No such file or directory'even though the file should exist, so I added ʻenvironment`.
Reference: No such file or directory in Ansible unarchive
Install Python
Here, we just use command
to execute configure
, make
, and make install
. At that time, chdir
is used to move to the directory where the source is located. I used these commands a lot, so I thought they were the default features of Ansible, but when I looked them up, I wasn't sure. If creates
is specified and / usr / local / bin / python
already exists, it will be skipped.
Link Python lib
A symbolic link is attached as a countermeasure for the error written at the beginning.
Recommended Posts