LoginSignup
0
1

More than 3 years have passed since last update.

pythonをソースコードからUbuntuにインストールする

Posted at

前置き

pythonをコンパイルする方法について色々記事はあふれているものの、
前提条件の違いでうまくいかず・・・

Ubuntu16.04のクリーンインストール状態から、
インストール完了までをまとめた。これでだいたいうまくいくはず。

環境

Ubuntu16.04 LTS server (64bit)をインストール直後の状態

ゴール

最新のpython3.9.2のインストール完了

環境の最新化

まず、パッケージリストを最新化します。


osoper@ubuntu1604:~$ sudo apt-get update

そして一括アップグレードします

osoper@ubuntu1604:~$ sudo apt-get upgrad

コンパイル環境を整える

pythonのソースコードのコンパイルに必要なパッケージを導入する


osoper@ubuntu1604:~$ sudo apt install build-essential \                         
> libbz2-dev libdb-dev \                                                        
> libreadline-dev libffi-dev \                                                  
> libgdbm-dev liblzma-dev \                                                     
> libncursesw5-dev libsqlite3-dev \                                             
> libssl-dev \                                                                  
> zlib1g-dev uuid-dev tk-dev                                                    

ソースコードの取得


osoper@ubuntu1604:~$ curl -O https://www.python.org/ftp/python/3.9.2/Python-3.9.2.tgz
% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
Dload  Upload   Total   Spent    Left  Speed
100 24.2M  100 24.2M    0     0  7701k      0  0:00:03  0:00:03 --:--:-- 7703k
osoper@ubuntu1604:~$ ls -l
合計 24808
-rw-rw-r-- 1 osoper osoper 25399571  2月 27 09:31 Python-3.9.2.tgz

ソースの解凍


osoper@ubuntu1604:~$ tar zxf Python-3.9.2.tgz
osoper@ubuntu1604:~$ ls -l
合計 24812                                                                      
drwxr-xr-x 17 osoper osoper     4096  2月 19 22:34 Python-3.9.2
-rw-rw-r--  1 osoper osoper 25399571  2月 27 09:31 Python-3.9.2.tgz

configure

コンパイルにはconfigure → make → make innstallと実施します。
コンパイルするファイルはたくさんあるので、一つ一つやると面倒です。
configureでMakefileというコンパイル計画のファイルを作って、
makeコマンドで計画にしたがって、コンパイルとインストールをすると思ってください。

configureコマンドはpythonのソースに含まれています。
makeはUbuntuのパッケージに含まれています。


osoper@ubuntu1604:~$ cd Python-3.9.2
osoper@ubuntu1604:~/Python-3.9.2$ ./configure --with-ensurepip=install
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking for python3.9... no
checking for python3... python3

(中略)

checking for --with-builtin-hashlib-hashes... md5,sha1,sha256,sha512,sha3,blake2
configure: creating ./config.status
config.status: creating Makefile.pre
config.status: creating Misc/python.pc
config.status: creating Misc/python-embed.pc
config.status: creating Misc/python-config.sh
config.status: creating Modules/ld_so_aix
config.status: creating pyconfig.h
creating Modules/Setup.local
creating Makefile


If you want a release build with all stable optimizations active (PGO, etc),
please run ./configure --enable-optimizations

「./configure --help」と打つことでオプションの詳細を確認可能です。
今回はpipのインストールオプションのみ含めました。(--with-ensurepip=install)
この場合、/usr/local/binにインストールされます。

コマンドが終わったところで、作成されたファイルを確認


osoper@ubuntu1604:~/Python-3.9.2$ ls -ltr
合計 2008
-rw-r--r--  1 osoper osoper 109891  2月 19 21:31 setup.py
-rw-r--r--  1 osoper osoper  45567  2月 19 21:31 pyconfig.h.in
-rw-r--r--  1 osoper osoper     82  2月 19 21:31 netlify.toml
drwxr-xr-x  2 osoper osoper   4096  2月 19 21:31 m4
-rwxr-xr-x  1 osoper osoper  15368  2月 19 21:31 install-sh
-rw-r--r--  1 osoper osoper 173556  2月 19 21:31 configure.ac
-rwxr-xr-x  1 osoper osoper 513139  2月 19 21:31 configure
-rwxr-xr-x  1 osoper osoper  36251  2月 19 21:31 config.sub
-rwxr-xr-x  1 osoper osoper  44166  2月 19 21:31 config.guess
-rw-r--r--  1 osoper osoper  13421  2月 19 21:31 aclocal.m4
drwxr-xr-x 23 osoper osoper   4096  2月 19 21:31 Tools
-rw-r--r--  1 osoper osoper  10132  2月 19 21:31 README.rst
drwxr-xr-x  3 osoper osoper   4096  2月 19 21:31 Python
drwxr-xr-x  2 osoper osoper   4096  2月 19 21:31 Programs
drwxr-xr-x  2 osoper osoper   4096  2月 19 21:31 PCbuild
drwxr-xr-x  6 osoper osoper   4096  2月 19 21:31 PC
drwxr-xr-x  4 osoper osoper   4096  2月 19 21:31 Objects
-rw-r--r--  1 osoper osoper  70514  2月 19 21:31 Makefile.pre.in
drwxr-xr-x  8 osoper osoper   4096  2月 19 21:31 Mac
drwxr-xr-x 34 osoper osoper   4096  2月 19 21:31 Lib
-rw-r--r--  1 osoper osoper  13925  2月 19 21:31 LICENSE
drwxr-xr-x  4 osoper osoper   4096  2月 19 21:31 Include
drwxr-xr-x  2 osoper osoper   4096  2月 19 21:31 Grammar
-rw-r--r--  1 osoper osoper    630  2月 19 21:31 CODE_OF_CONDUCT.md
drwxr-xr-x  4 osoper osoper   4096  2月 19 22:34 Parser
drwxr-xr-x 18 osoper osoper   4096  2月 19 22:34 Doc
-rwxrwxr-x  1 osoper osoper  42486  2月 27 09:42 config.status
-rw-rw-r--  1 osoper osoper  70214  2月 27 09:42 Makefile.pre
drwxr-xr-x  2 osoper osoper   4096  2月 27 09:42 Misc
-rw-rw-r--  1 osoper osoper  47695  2月 27 09:42 pyconfig.h
-rw-rw-r--  1 osoper osoper  79572  2月 27 09:42 Makefile
drwxr-xr-x 14 osoper osoper   4096  2月 27 09:42 Modules
-rw-rw-r--  1 osoper osoper 664138  2月 27 09:42 config.log

Makefileが存在することと、config.logが「configure: exit 0」で終わっていることを確認します。

make

シンプルにmakeとだけ、コマンドを打ちます。


osoper@ubuntu1604:~/Python-3.9.2$ make                                          
gcc -pthread -c -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall 
   -std=c99 -Wextra -Wno-unused-result -Wno-unused-parameter -Wno-missing-field-
initializers -Werror=implicit-function-declaration -fvisibility=hidden  -I./Incl
ude/internal  -I. -I./Include    -DPy_BUILD_CORE -o Programs/python.o ./Programs
/python.c                                                                       

(中略)

gcc -pthread     -Xlinker -export-dynamic -o Programs/_testembed Programs/_teste
mbed.o libpython3.9.a -lcrypt -lpthread -ldl  -lutil -lm   -lm
sed -e "s,@EXENAME@,/usr/local/bin/python3.9," < ./Misc/python-config.in >python
-config.py
LC_ALL=C sed -e 's,\$(\([A-Za-z0-9_]*\)),\$\{\1\},g' < Misc/python-config.sh >py
thon-config
osoper@ubuntu1604:~/Python-3.9.2$ echo $?
0

無事完了!!

make install

最後はインストール。ここだけsudoで実行するのを忘れずに!!


osoper@ubuntu1604:~/Python-3.9.2$ make installsudo
[sudo] osoper のパスワード:
if test "no-framework" = "no-framework" ; then \
    /usr/bin/install -c python /usr/local/bin/python3.9; \
else \
    /usr/bin/install -c -s Mac/pythonw /usr/local/bin/python3.9; \
fi
if test "3.9" != "3.9"; then \
    if test -f /usr/local/bin/python3.9 -o -h /usr/local/bin/python3.9; \
    then rm -f /usr/local/bin/python3.9; \
    fi; \

(中略)

Looking in links: /tmp/tmp2_tfnh2l
Processing /tmp/tmp2_tfnh2l/setuptools-49.2.1-py3-none-any.whl
Processing /tmp/tmp2_tfnh2l/pip-20.2.3-py2.py3-none-any.whl
Installing collected packages: setuptools, pip
Successfully installed pip-20.2.3 setuptools-49.2.1

Successfully installedと出て無事完了

確認

デフォルトの/usr/local/binにインストールされていることを確認


osoper@ubuntu1604:/usr/local/bin$ ls -l
合計 17276
lrwxrwxrwx 1 root root        8  2月 27 13:06 2to3 -> 2to3-3.9
-rwxr-xr-x 1 root root      101  2月 27 13:06 2to3-3.9
-rwxr-xr-x 1 root root      238  2月 27 13:06 easy_install-3.9
lrwxrwxrwx 1 root root        7  2月 27 13:06 idle3 -> idle3.9
-rwxr-xr-x 1 root root       99  2月 27 13:06 idle3.9
-rwxr-xr-x 1 root root      229  2月 27 13:06 pip3
-rwxr-xr-x 1 root root      229  2月 27 13:06 pip3.9
lrwxrwxrwx 1 root root        8  2月 27 13:06 pydoc3 -> pydoc3.9
-rwxr-xr-x 1 root root       84  2月 27 13:06 pydoc3.9
lrwxrwxrwx 1 root root        9  2月 27 13:06 python3 -> python3.9
lrwxrwxrwx 1 root root       16  2月 27 13:06 python3-config -> python3.9-config
-rwxr-xr-x 1 root root 17659504  2月 27 13:06 python3.9
-rwxr-xr-x 1 root root     3087  2月 27 13:06 python3.9-config

/usr/binのpythonの置き換え

/usr/binのpython3はデフォルトでインストールされている3.5にリンクされています。


osoper@ubuntu1604:/usr/local/bin$ python3 -V
Python 3.5.2

/usr/binのものが効いているようです。


osoper@ubuntu1604:/usr/bin$ ls -l python*
lrwxrwxrwx 1 root root       9  2月 26 20:22 python3 -> python3.5
-rwxr-xr-x 2 root root 4456208  1月 27 03:48 python3.5
-rwxr-xr-x 2 root root 4456208  1月 27 03:48 python3.5m
lrwxrwxrwx 1 root root      10  2月 26 20:22 python3m -> python3.5m

以下のコマンドでシンボリックリンクの置き換えをします。


osoper@ubuntu1604:~$ cd /usr/bin
osoper@ubuntu1604:/usr/bin$ sudo ln -nfs /usr/local/bin/python3.9 python3

確認して、完了!!


osoper@ubuntu1604:~$ ptyhopython3 -V
Python 3.9.2

参考

以下のサイトを参照しながら上記完了させました。
https://tech.chakapoko.com/python/dev/install-from-source.html
https://www.python.jp/install/ubuntu/index.html
https://qiita.com/kei0425/items/6b84938db2c22186fdbc

0
1
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
0
1