What's the easiest way to get Vim with Python 3 support?

It seems on Debian-based systems (at least) you can't have your cake and eat it too. It's either Python 2 or Python 3. Due to how the Python libraries are built, you can only use one variant within a Vim session. You can build with both, but if Python 2 is called in Vim, then Python 3 cannot be called in the same session, and vice versa. On Arch Linux as well, Vim is only compiled with one of Python 2 (vim, gvim) or Python 3 (vim-python3, gvim-python3).


Before 16.04

To rebuild the Vim that the repositories provide:

sudo apt-get build-dep vim
apt-get source vim
cd vim-*  # it will be vim-7.4.something

Edit debian/rules and replace:

ALLINTERPFLAGS+=--enable-pythoninterp --with-python-config-dir=$(shell python-config --configdir)
ALLINTERPFLAGS+=--disable-python3interp

With:

ALLINTERPFLAGS+=--enable-pythoninterp=dynamic --with-python-config-dir=$(shell python-config --configdir)
ALLINTERPFLAGS+=--enable-python3interp=dynamic --with-python3-config-dir=$(shell python3-config --configdir)

Then run:

dpkg-buildpackage -us -uc

Have lunch. (Or tea if you used -j $(nproc).)

Now, multiple .deb files will have been created in the parent directory. To see which:

cd ..
ls vim*.deb

Along with the particular variant you want to install (vim, vim-gnome, vim-gtk, etc.), you'll have to install vim-common_*.deb, vim-runtime_*.deb, and for the GUI versions, vim-gui-common_*.deb. For example, with vim-gnome, and the current version of vim in the repositories:

sudo dpkg -i vim-gnome_7.4.052-1ubuntu3_amd64.deb vim-common_7.4.052-1ubuntu3_amd64.deb vim-gui-common_7.4.052-1ubuntu3_all.deb vim-runtime_7.4.052-1ubuntu3_all.deb

Then:

$ vim --version | grep python
+cryptv          +linebreak       +python/dyn      +viminfo
+cscope          +lispindent      +python3/dyn     +vreplace

The pi-rho/dev PPA builds Vim in this fashion, so you can use the PPA instead of manually building it.


16.04

As of 16.04, Ubuntu builds Vim with Python 3 support. Python 2 support is provided by the -py2 packages (vim-nox-py2, vim-gnome-py2, etc.).


Related:

  • How do I get Vim to be able to run both python and python3 on a Linux system in the same session? on Vi and Vim.