AWS EC2 Ubuntu server: Install Python

AWS上のUbuntuにPythonインストールして、Macからrsyncで自作のPythonススクリプトをUbuntuに渡すところまでのメモ。

Deploy my python scripts to AWS EC2 Ubuntu Server

  • Install python
  • Install python packages
  • Put my python scripts 
  • Test my python scripts
  • Set crontab

Install python

confirm existing python

    $ which python{,2,3}    
    /usr/bin/python3
    $ ls -la /usr/bin/python3
    lrwxrwxrwx 1 root root 10 Aug 18  2022 /usr/bin/python3 -> python3.10

update pakages

    sudo apt update
    sudo apt upgrade -y

install pip

    sudo apt install -y python3-pip

add apt repository

    sudo apt install -y software-properties-common
    sudo add-apt-repository ppa:deadsnakes/ppa

apt update

    sudo add-apt-repository ppa:deadsnakes/ppa

list python3

    sudo apt list python3.*

Install python3.9

    sudo apt install -y python3.9 python3.9-venv

set alternatives

    sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.10 120
    sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.9 110

show alternatives

    sudo update-alternatives --config python

exsample

    $ sudo update-alternatives --config python
    There are 2 choices for the alternative python (providing /usr/bin/python).

      Selection    Path                 Priority   Status
    ------------------------------------------------------------
    * 0            /usr/bin/python3.10   120       auto mode
      1            /usr/bin/python3.10   120       manual mode
      2            /usr/bin/python3.9    110       manual mode

    Press <enter> to keep the current choice[*], or type selection number: 

switch python version

    $ sudo update-alternatives --config python
    There are 2 choices for the alternative python (providing /usr/bin/python).

      Selection    Path                 Priority   Status
    ------------------------------------------------------------
      0            /usr/bin/python3.10   120       auto mode
      1            /usr/bin/python3.10   120       manual mode
    * 2            /usr/bin/python3.9    110       manual mode

    Press <enter> to keep the current choice[*], or type selection number: 
    $ 
    $ python3 -V
    Python 3.10.6
    $ 
    $ python -V
    Python 3.9.16

create venv

    mkdir -p ~/venv/python3.9
    cd ~/venv/python3.9/
    python -m venv myproject1

activate venv

    source ~/venv/python3.9/myproject1/bin/activate

example

    ubuntu@ip-172-XX-XX-XX:~/venv/python3.9$ source ~/venv/python3.9/myproject1/bin/activate
    (myproject1) ubuntu@ip-172-XX-XX-XX:~/venv/python3.9$ 
    (myproject1) ubuntu@ip-172-XX-XX-XX:~/venv/python3.9$ python
    Python 3.9.16 (main, Dec  7 2022, 01:12:08) 
    [GCC 11.3.0] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> 
    >>> 
    >>> print('hello')
    hello
    >>> exit()
    (myproject1) ubuntu@ip-172-XX-XX-XX:~/venv/python3.9$ 

Install python packages

get package information on my Mac

    pip freeze 

example

    (venv) mymac:upload-to-AWS$ pip freeze > requirements.txt

send package information to my EC2

    rsync -v (source) (destination)

example

  • send file from my Mac
  • 'ec2' is configured hostname in ~.ssh/config.

    (venv) mymac:upload-to-AWS$ rsync -v ./requirements.txt ec2:requirement.txt
    requirements.txt
    
    sent 3098 bytes  received 42 bytes  897.14 bytes/sec
    total size is 3001  speedup is 0.96
  • recieved file on my EC2.

    ubuntu@ip-XX-XX-XX:~$ ls -l
    total 8
    -rw-r--r-- 1 ubuntu ubuntu 3001 Mar 21 13:59 requirement.txt
    drwxrwxr-x 3 ubuntu ubuntu 4096 Mar 21 13:15 venv

install packages on my EC2

    source ~/venv/python3.9/myproject1/bin/activate
    pip install -r requirement.txt 

example

    ubuntu@ip-172-XX-XX-XX:~$ source ~/venv/python3.9/myproject1/bin/activate
    (myproject1) ubuntu@ip-172-XX-XX-XX:~$ ls -l
    total 8
    -rw-r--r-- 1 ubuntu ubuntu 3001 Mar 21 13:59 requirement.txt
    drwxrwxr-x 3 ubuntu ubuntu 4096 Mar 21 13:15 venv
    (myproject1) ubuntu@ip-172-XX-XX-XX:~$  
    (myproject1) ubuntu@ip-172-XX-XX-XX:~$ pip install -r requirement.txt 

Put my python scripts on EC2

example

make directory for my script on EC2.

    (myproject1) ubuntu@ip-172-XX-XX-XX:~$ cd venv/python3.9/myproject1/
    (myproject1) ubuntu@ip-172-XX-XX-XX:~/venv/python3.9/myproject1$ mkdir rec-count

send my script files from my Mac.

myproject1/rec-count/ (my_script).py

    sent 1223 bytes  received 42 bytes  361.43 bytes/sec
    total size is 1120  speedup is 0.89
    mymac:upload-to-AWS$ 

ensure recieved files on EC2.

    ubuntu@ip-172-XX-XX-XX:~/venv/python3.9/myproject1/rec-count$ ls -la
    total 24
    drwxrwxr-x 2 ubuntu ubuntu 4096 Mar 21 15:55 .
    drwxrwxr-x 8 ubuntu ubuntu 4096 Mar 21 15:38 ..
    -rw-r--r-- 1 ubuntu ubuntu 1120 Mar 21 15:55 (my_script).py

Next steps are...

  • Test my python scripts on EC2
  • Set crontab

over

Comments

Popular Posts