How to Install Python 3.6 3.7 on Ubuntu 16.04 / CentOS 7 / Debian 9 / macOS

2019年3月22日

Install Python 3.6 on Ubuntu 16.04

On Ubuntu 16.04, Python 2.7 and Python 3.5 are installed by default. To have the latest version of Python 3.6 installed on your Ubuntu 16.04 server follow these steps.

Step 1: Check the version of Python 3 on Ubuntu 16.04

$ python3 -V
Python 3.5.2

You can see here it is 3.5.2. The easiest way to Install Python 3.6 on Ubuntu 16.04 is from J Fernyhough’s Personal Package Archive (PPA).

Step 2: Ensure you have these packages installed before adding the PPA.

$ sudo apt-get install software-properties-common python-software-properties

Step 3: AddJ Fernyhough’s PPA:

You can now add the PPA.

$ sudo add-apt-repository ppa:jonathonf/python-3.6

You’ll get a message output like below:

Don't remove Python 3.5 from your system - it will break.
More info: https://launchpad.net/~jonathonf/+archive/ubuntu/python-3.6
Press [ENTER] to continue or ctrl-c to cancel adding it

gpg: keyring `/tmp/tmpd0im8dw_/secring.gpg' created
gpg: keyring `/tmp/tmpd0im8dw_/pubring.gpg' created
gpg: requesting key F06FC659 from hkp server keyserver.ubuntu.com
gpg: /tmp/tmpd0im8dw_/trustdb.gpg: trustdb created
gpg: key F06FC659: public key "Launchpad PPA for J Fernyhough" imported
gpg: Total number processed: 1
gpg: imported: 1 (RSA: 1)
OK

The command above should add a file to /etc/apt/souces.list.d directory.

$ cat /etc/apt/sources.list.d/jonathonf-ubuntu-python-3_6-xenial.list 
deb http://ppa.launchpad.net/jonathonf/python-3.6/ubuntu xenial main

Step 4: Update for the apt index:

Update apt index to reflect new packages on added PPA repository.

$ sudo apt-get update

Step 5: Install Python 3.6 on Ubuntu 16.04

You can install Python 3.6 on Ubuntu 16.04 using the command.

$ sudo apt-get install python3.6

Step 6: Verify Install of Python 3.6 on Ubuntu 16.04

Check the version of Python 3 present on your system.

$ python3 -V

Install Python 3.6 on CentOS 7

For CentOS 7, you can install Python 3.6 from IUS Community Project CentOS repository. IUS is a community project that provides RPM packages for newer versions of select software for Enterprise Linux distributions.

Step 1: Add IUS Community Project repository

Install repository rpm which will add repository content for you to /etc/yum.repos.d directory.

$ sudo yum install -y https://centos7.iuscommunity.org/ius-release.rpm

Step 2: Update yum index

Update yum index to get a list of all packages on the added repository.

$ sudo yum makecache fast

Step 3: Install Python 3.6 on CentOS 7

Run the following commands to Install Python 3.6 on CentOS 7.

$ sudo yum install -y python36u \
python36u-libs \
python36u-pip \
python36u-devel

Step 4: Confirm Installation of Python 3.6 on CentOS 7

Now check the version of Python 3 installed on your system.

$ python3.6 -V

Install Python 3.6 on Debian 9

Python 3.6 for Debian 9 is available in the testing repository. Add the following line to /etc/apt/sources.list file.

deb http://ftp.de.debian.org/debian testing main

Since the  Debian ‘testing’ repository can easily break your system if you update system packages, It is advisable to make stable repo a default when installing and updating packages. For this run:

# echo 'APT::Default-Release "stable";' | sudo tee -a /etc/apt/apt.conf.d/00local

Now get you apt package list update

$ sudo apt-get update

And finally, Install Python 3.6 on Debian 9

$ sudo apt-get -t testing install python3.6

You can confirm the version using:

$ python3 -V

Install Python 3.6 on macOS

To get Python 3.6 installed on your macOS machine, go to Python Download page and pull the latest release of Python 3.6 for mac. You can also use wget or similar commands to download the package directly without using a web browser.

$ wget https://www.python.org/ftp/python/3.6.5/python-3.6.5-macosx10.9.pkg

Install the package by double-clicking on its name.

Once installed, confirm the version of Python3 on your macOS.

$ python3 -V
Python 3.6.5

Boom!, all is set now.

Creating a virtualenv with Python 3.6

Now that we have Python 3.6 installed on your system, you can easily create a Virtualenv to use in your project with it. The example below will create a Virtualenv called myproject.

$ python3.6 -m venv  myproject
$ ls myproject 
bin include lib pyvenv.cfg

Activate then environment so that you can start installing packages with pip.

$ . myproject/bin/activate 
(myproject)

To install a package with pip, use:

$ pip install [package_name]
$ pip install -r requirements.txt

Example, to install youtube-dl, run:

$ pip install youtube-dl
Collecting youtube-dl
Downloading youtube_dl-2018.3.26.1-py2.py3-none-any.whl (1.7MB)
100% |████████████████████████████████| 1.7MB 280kB/s 
Installing collected packages: youtube-dl
Successfully installed youtube-dl-2018.3.26.1
(myproject)

没有评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注